[Grace-core] String methods

Kim Bruce kim at cs.pomona.edu
Thu Aug 14 13:18:32 PDT 2014


I just finished the first pass through the strings chapter of Programming with Grace.  Not surprisingly, there were a number of String
methods that I needed which are not currently implemented in the compiler as String methods.  I wrote them all as methods of a module "extraStringMethods" that is sitting in ProgrammingWithGrace/ch15-strings in the svn repository.  Here is a listing of those I wrote:

    // return first occurrence of pattern in source that occurs at or after position i
    indexOf(pattern: String) in (source: String) starting (i:Number) -> Number {

    // return first occurrence of pattern in source
    indexOf(pattern: String) in (source: String) -> Number {

    // return string like theString, but with all lower case letters replaced by the
    // corresponding upper case letters
    toUpper(theString: String) -> String {

    // return string like theString, but with all upper case letters replaced by the
    // corresponding lower case letters
    toLower(theString: String) -> String {

    // return true if s1 occurs before s2 in lexicographic order or if they are equal
    lessThanEqual(s1:String, s2: String) -> Boolean {

    // return true if s1 occurs before s2 in lexicographic order
    lessThan(s1:String, s2: String) -> Boolean {

    // return true if s1 occurs after s2 in lexicographic order
    greaterThan(s1:String,s2:String) -> Boolean {

    // return true if s1 occurs after s2 in lexicographic order or if they are equal
    greaterThanEqual(s1:String, s2: String) -> Boolean {

    // return string obtained from sourse by throwing away spaces at the beginning
    //  and end of a string.  If it is all spaces, return an empty string
    trim(source: String) -> String 

They are all fairly standard methods usually found as methods of String.  While they are stand-alone, the code in extraStringMethods.grace can be easily rewritten if someone were to add these methods to the String type.  The book currently assumes they are in that library module, but I'd be happy to see them added to the compiler (though the priority of this is behind the other features I currently can't do: images, gui components, etc.)

Kim






More information about the Grace-core mailing list