For Development HEAD DRAFTSearch (procedure/syntax/module):

11.29 srfi.152 - String library (reduced)

Module: srfi.152

This is an improved version of srfi.13. The spec is actually smaller than SRFI-13 (hence ’reduced’ in the title) by removing bells and whistles. The consistency with R7RS and recent srfis are also considered.

The following are built-in. See Strings, for the details.

string?            make-string        string             string-length
string->vector     string->list       vector->string     list->string
string-ref         string-set!        substring          string-copy
string=?           string<?           string<=?          string>?
string>=?          string-append      string-join        string-split
string-map         string-for-each    read-string        write-string
string-fill!

The following procedures are defined to use Unicode string case folding, and gauche.unicode module provides them. See Full string case conversion, for the details. Note that Gauche’s built-in versions uses character-wise case folding, which differs from string case folding on some characters (German eszett, for example).

string-ci=?   string-ci<?   string-ci<=?  string-ci>?   string-ci>=?

The following procedures are defined in SRFI-13. See srfi.13 - String library, for the details. Note: Some of those procedures in SRFI-13 that require predicate allows a char-set or a character to be passed instead (e.g. string-filter). In SRFI-152, only predicate is allowed. Our SRFI-152 implementation shares the same procedure with SRFI-13, so they accept the same arguments as SRFI-13’s, but such code won’t be portable as SRFI-152.

string-null?       string-any         string-every       string-tabulate
string-unfold      string-unfold-right      reverse-list->string
string-take        string-drop        string-take-right  string-drop-right
string-pad         string-pad-right   string-trim        string-trim-right
string-trim-both   string-replace     string-prefix-length string-suffix-length
string-prefix?     string-suffix?     string-index       string-index-right
string-skip        string-skip-right  string-contains    string-concatenate
string-concatenate-reverse            string-fold        string-fold-right
string-count       string-filter      string-copy!

We describe procedures unique to this module below.

Function: string-remove pred string :optional start end

[SRFI.152]{srfi.152} Returns a substring of string between start and end, except characters that satisfy pred. In other words, it is (string-filter (complement pred) string start end).

This is called string-delete in srfi.13. Being changed to take only a predicate (but not a character), it is renamed for the consistency of other srfi (e.g. filter, remove and delete in SRFI-1.)

(string-remove char-whitespace?
               "Quick fox jumps over the lazy dog"
               3 22)
 ⇒ "ckfoxjumpsovert"
Function: string-replicate string from to :optional start end

[SRFI-152]{srfi.152} Extended substring. It is called xsubstring in SRFI-13, but renamed for the consistency.

Extract a substring of string between start and end, and conceptually create a bidirectional infinite string by repeating the substring to both direction. For example, suppose string is "abcde", start is 1, and end is 4. So we repeat the substring "bcd", with one b falling on the index zero:

... b  c  d  b  c  d  b  c  d  b  c  d  b ...
   -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6

Then we extract a substring between from and to out of this infinite string.

(string-replicate "abcde" 2 10 1 4)
  ⇒ "dbcdbcdb"
(string-replicate "abcde" -5 -3 1 4)
  ⇒ "cdbcdbcd"
Function: string-segment string k

[SRFI-152]{srfi.152} Splits string by every k characters and returns a list of those strings. The last string may be shorter than k.

(string-segment "abcdefghijklmn" 3)
 ⇒ ("abc" "def" "ghi" "jkl" "mn")

We have a similar procedure on lists, slices (see List accessors and modifiers).

Function: string-contains-right string1 string2 :optional start1 end1 start2 end2

[SRFI-152]{srfi.152} Like string-contains, looks for a needle string2 from a haystack string1, but if it is found, returns the start index of the last match, instead of the first match. The returned index is in string1. The optional arguments limit the range of a needle and a haystack. If a needle isn’t found, #f is returned.

An edge case: If a needle is empty (e.g. string2 is empty, or start2 = end2), it always matches right after the haystack, so end1 is returned.

(string-contains-right "Little Lisper" "Li")
  ⇒ 7
Function: string-take-while string pred :optional start end
Function: string-take-while-right string pred :optional start end

[SRFI-152]{srfi.152} Returns the longest prefix or suffix of string in which all characters satisfy pred.

Note: The order of pred and the source object is different from other take-while-style procedures, such as take-while (scheme.list - R7RS lists), ideque-take-while (scheme.ideque - R7RS immutable deques), and lseq-take-while (scheme.lseq - R7RS lazy sequences).

Function: string-drop-while string pred :optional start end
Function: string-drop-while-right string pred :optional start end

[SRFI-152]{srfi.152} Returns the longest prefix or suffix of string in which all characters does not satisfy pred.

Note: The order of pred and the source object is different from other drop-while-style procedures, such as drop-while (scheme.list - R7RS lists), ideque-drop-while (scheme.ideque - R7RS immutable deques), and lseq-drop-while (scheme.lseq - R7RS lazy sequences).

Function: string-span string pred :optional start end
Function: string-break string pred :optional start end

[SRFI-152]{srfi.152} Find the longest prefix of string between start and end in which all characters satisy / do not satisfy pred, and returns the prefix and the rest of substring as two values.

(string-break "foo@example.com" (cut eqv? <> #\@))
 ⇒ "foo" and "@example.com"

(string-break "foo@example.com" (cut eqv? <> #\@) 1 10)
 ⇒ "oo" "@exampl"

;; This is Gauche specific - a char-set can work as a predicate:
(string-span "VAR_1 = $VAR_2" #[\w])
 ⇒ "VAR_1" and " = $VAR_2"

Note: The order of pred and the source object is different from span and break in scheme.list (see scheme.list - R7RS lists).



For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT