diff -r f66a9d2069a1 lib/string/abstract_string.li --- a/lib/string/abstract_string.li Sun Aug 31 13:14:18 2008 +0200 +++ b/lib/string/abstract_string.li Sun Aug 31 13:31:45 2008 +0200 @@ -196,7 +196,11 @@ ? { valid_index i}; storage.item (i - 1).code ); - + + - contains ch:CHARACTER :BOOLEAN <- valid_index (index_of ch); + + - index_of ch:CHARACTER :INTEGER <- index_of ch since 1; + - index_of ch:CHARACTER since start_index:INTEGER :INTEGER <- // Index of first occurrence of `c' at or after `start_index', // result = count + 1, if none. @@ -212,6 +216,8 @@ result ); + - last_index_of ch:CHARACTER :INTEGER <- last_index_of ch since upper; + - last_index_of ch:CHARACTER since start_index:INTEGER :INTEGER <- // Index of first occurrence of `c' at or before `start_index', // 0 if none. @@ -227,7 +233,7 @@ ? {(result != 0) ->> {item result == ch}}; result ); - + - fast_index_of ch:CHARACTER :INTEGER <- // Gives the index of the first occurrence `ch' or // 0 if none. @@ -832,10 +838,86 @@ substring_index (other,1) ); - + + - replace pattern:ABSTRACT_STRING by replacement:ABSTRACT_STRING :STRING <- + ( + result :STRING; + + i, j :INTEGER; + result := STRING.create count; + i := lower; + j := substring_index (pattern, i); // first character of the pattern + { j = 0 }.until do { + result.append (substring i to (j-1)); // TODO: optimize + result.append replacement; + i := j + pattern.count; // first character after the pattern + j := substring_index (pattern, i); // first character of the pattern + }; + result.append (substring i to upper); // TODO: optimize + result + ); + // // Splitting a STRING: // + + - partition sep:ABSTRACT_STRING from start_index:INTEGER :(STRING, STRING, STRING) <- + [ + -? {sep != NULL}; + -? {valid_index start_index}; + ] + ( + i, j:INTEGER; + + head, sep, tail :STRING; + i := substring_index (sep, start_index); + (i = 0).if { + // Not found + head := to_string; + } else { + // Found + j := i + sep.count; + (i > lower).if { + head := substring lower to (i-1); + }; + sep := substring i to j; + (j < upper).if { + tail := substring (j+1) to upper; + }; + }; + head, sep, tail + ); + + - partition sep:ABSTRACT_STRING :(STRING, STRING, STRING) <- + ( + partition sep from lower + ); + + - last_partition sep:ABSTRACT_STRING from start_index:INTEGER :(STRING, STRING, STRING) <- + [ + -? {sep != NULL}; + -? {valid_index start_index}; + ] + ( + i, j:INTEGER; + + head, sep, tail :STRING; + i := last_substring_index (sep, start_index); + (i = 0).if { + // Not found + head := to_string; + } else { + // Found + j := i + sep.count; + (i > lower).if { + head := substring lower to (i-1); + }; + sep := substring i to j; + (j < upper).if { + tail := substring (j+1) to upper; + }; + }; + head, sep, tail + ); + + - last_partition sep:ABSTRACT_STRING :(STRING, STRING, STRING) <- + ( + last_partition sep from upper + ); - split:ARRAY[STRING] <- // Split the string into an array of words. Uses `is_separator' of diff -r f66a9d2069a1 lib2/string/abstract_string.li --- a/lib2/string/abstract_string.li Sun Aug 31 13:14:18 2008 +0200 +++ b/lib2/string/abstract_string.li Sun Aug 31 13:31:45 2008 +0200 @@ -916,6 +916,36 @@ partition sep from lower ); + - last_partition sep:ABSTRACT_STRING from start_index:INTEGER :(STRING, STRING, STRING) <- + [ + -? {sep != NULL}; + -? {valid_index start_index}; + ] + ( + i, j:INTEGER; + + head, sep, tail :STRING; + i := last_substring_index (sep, start_index); + (i = 0).if { + // Not found + head := to_string; + } else { + // Found + j := i + sep.count; + (i > lower).if { + head := substring lower to (i-1); + }; + sep := substring i to j; + (j < upper).if { + tail := substring (j+1) to upper; + }; + }; + head, sep, tail + ); + + - last_partition sep:ABSTRACT_STRING :(STRING, STRING, STRING) <- + ( + last_partition sep from upper + ); + - split_str sep:ABSTRACT_STRING :ARRAY[STRING] <- [ -? {sep != NULL};