You are looking at historical revision 39135 of this page. It may differ significantly from its current revision.

This SRFI is derived from SRFI 13. The biggest difference is that it allows subsequences of strings to be specified by cursors as well as the traditional string indexes. In addition, it omits the comparison, case-mapping, and mutation operations of SRFI 13, as well as all procedures already present in [[https://srfi.schemers.org/srfi-130/srfi-130.html#R7RS][R7RS]].

For more information see: [[https://srfi.schemers.org/srfi-130/][SRFI-130: Cursor-based string library]]

When SRFI 13 was defined in 1999, it was intended to provide efficient string operations on both whole strings and substrings. At that time, it was normal for strings to be sequences of 8-bit characters, or in a few cases, characters of other fixed lengths. Consequently, many of the SRFI 13 procedures accept optional exact integer arguments for each of the string arguments, indexing the beginning and the end of the substring(s) to be operated on.

Unfortunately for this design, Unicode has become much more widely used, and it is now fairly common for implementations to store strings internally as UTF-8 or UTF-16 code unit sequences, which means that indexing operations are potentially O(n) rather than O(1). Using opaque cursors makes it possible to iterate much more efficiently through such strings compared to incrementing or decrementing indexes; however, for backward compatibility, the procedures defined in this SRFI accept either cursors or indexes. The results returned are always cursors: the use of indexes is preserved mainly for the sake of existing code and for implementer convenience.

The operations provided here are entirely independent of the character repertoire supported by the implementation. In particular, this means that the comparison and case conversion procedures of SRFI 13 are excluded. There is also no provision for [[http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-2.html#node_idx_54][R6RS normalization procedures]] or for a string->integer procedure that was proposed for SRFI 13 but not included. These may appear in future SRFIs. Furthermore, string mutation can be extremely expensive if the storage used for the string needs to be expanded, particularly if the implementation does not use an indirect pointer to it (as in Chicken), so this SRFI does not provide for it. The low-level procedures of SRFI 13 are specific to the sample implementation, and have been removed to make other implementations simpler and easier.

Many SRFI 13 procedures accept either a predicate, a single character, or a [[https://srfi.schemers.org/srfi-14/srfi-14.html][SRFI 14]] character set. In this SRFI, only support for predicates is required, though implementations may also support the other two alternatives. In that case, a single character is interpreted as a predicate which returns true if its argument is the same (in the sense of eqv?) to that character; a character set is interpreted as a predicate which returns true if its argument belongs to that character set. In SRFI 13, character sets are inherently more efficient than predicates [[https://srfi.schemers.org/srfi-13/mail-archive/msg00052.html][because testing them is fast and free of side effects]], though how fast character sets actually are if they support full Unicode is very implementation-dependent. The only procedure that absolutely requires character set support, string-tokenize, has been replaced here by the more usual string-split procedure provided by Perl, Python, Java, JavaScript, and other languages.

The search procedures in SRFI 13 return either an index or #f if the search fails. Their counterparts in this SRFI return cursors. Left-to-right searches return a cursor representing the leftmost matching character, or the post-end cursor if there is no match; right-to-left searches return a cursor representing the successor of the rightmost matching character, or the start cursor if there is no match. This convention was devised by Alan Watson and implemented in Chibi Scheme.

In short, this SRFI is intended to help move the practice of Scheme programming away from mutable strings, string indexes, and SRFI 13, while largely maintaining backward compatibility. It does not require any particular run-time efficiencies from its procedures.

It is an error to make any use of a cursor referring to a string after the string, or any string that shares storage with it, has been mutated by a procedure like string-set!, string-copy!, or string-fill!.

Given a string of length n, there are n + 1 valid cursors that refer to it: one for each character in the string, and one for the position just after the last character, known as the "post-end cursor". The cursor for the first (or zeroth) position in the string is known as the "start cursor". The post-end cursor is provided because when creating a string from cursors the second cursor argument is exclusive. It is an error if a cursor argument is not one of the valid cursors for the string argument. The index analogue of the post-end cursor is n.

#+BEGIN_EXAMPLE substring/cursors string-take string-take-right string-drop string-drop-right string-pad string-pad-right string-trim string-trim-right string-trim-both string-split string-filter string-remove #+END_EXAMPLE

In particular, if the result is the same (in the sense of string=?) as any of the arguments, any implementation of the above procedures may return the string argument without copying it. Other procedures such as string-copy/cursors, as well as all the [[https://srfi.schemers.org/srfi-130/srfi-130.html#R7RS][R7RS]] procedures, are not permitted to return shared results. If a shared value is returned, it may be mutable or immutable.

Procedures that have left/right directional variants use no suffix to specify left-to-right operation, -right to specify right-to-left operation, and -both to specify both. This is a general convention that has been established in other SRFIs; the value of a convention is proportional to the extent of its use.

would take one (f), two (f, x) or three (f, x, init-store) input parameters, and return two values, a boolean and an integer.

takes two required parameters (doc and dict[1]) and zero or more optional parameters (dict[2] ...).

(list->string lis) = (string-unfold null? car cdr lis)

(string-tabulate f size) = (string-unfold (lambda (i) (= i size)) f add1 0) #+END_SRC

The final string constructed does not share storage with either base or the value produced by make-final.

This combinator sometimes is called an "anamorphism."

It is equivalent to string-unfold, except that the results of mapper are assembled into the string in a right-to-left order, base is the optional rightmost portion of the constructed string, and make-final produces the leftmost portion of the constructed string.

#+BEGIN_SRC scheme (reverse-list->string '(#\a #\B #\c)) → "cBa" #+END_SRC

This is a common idiom in the epilog of string-processing loops that accumulate an answer in a reverse-order list. (See also string-concatenate-reverse for the "chunked" variant.)

The grammar argument is a symbol that determines how the delimiter is used, and defaults to 'infix.

; Infix grammar is ambiguous wrt empty list vs. empty string,

(string-join '() ":") => "" (string-join '("") ":") => ""

; but suffix & prefix grammars are not.

(string-join '() ":" 'suffix) => "" (string-join '("") ":" 'suffix) => ":" #+END_SRC

If these procedures produce the entire string, they may return either s or a copy of s; in some implementations, proper substrings may share memory with s.

(string-take-right "Beta rules" 5) => "rules" (string-drop-right "Beta rules" 5) => "Beta " #+END_SRC

If len <= end-start, the returned value is allowed to share storage with s, or be exactly s (if len = end-start).

#+BEGIN_SRC scheme (string-pad "325" 5) => " 325" (string-pad "71325" 5) => "71325" (string-pad "8871325" 5) => "71325" #+END_SRC

If no trimming occurs, these functions may return either s or a copy of s; in some implementations, proper substrings may share memory with s.

#+BEGIN_SRC scheme (string-trim-both " The outlook wasn't brilliant, \n\r")

   => "The outlook wasn't brilliant,"

#+END_SRC

The optional start/end cursors or indexes restrict the comparison to the indicated substrings of s1 and s2.

Is s1 a prefix/suffix of s2?

The start and end parameters specify the beginning and end cursors or indexes of the search; the search includes the start, but not the end. Be careful of "fencepost" considerations: when searching right-to-left, the first position considered is (string-cursor-prev end), whereas when searching left-to-right, the first index considered is start. That is, the start/end indexes describe the same half-open interval [start,end) in these procedures that they do in all the other SRFI 130 procedures.

The skip functions are similar, but use the complement of the criteria: they search for the first char that doesn't satisfy pred. E.g., to skip over initial whitespace, say

#+BEGIN_SRC scheme (substring/cursors s (string-skip s char-whitespace?)) #+END_SRC

Note that the result is always a cursor, even when start and end are indexes. Use string-cursor->index to convert the result to an index. Therefore, these four functions are not entirely compatible with their SRFI 13 counterparts, which return #f on failure.

These functions can be trivially composed with string-take and string-drop to produce take-while, drop-while, span, and break procedures without loss of efficiency.

Returns the cursor in s1 referring to the first character of the first/last instance of s2 as a substring, or #f if there is no match. The optional start/end indexes restrict the operation to the indicated substrings.

The returned cursor is in the range [start1,end1). A successful match must lie entirely in the [start1,end1) range of s1.

Note that the result is always a cursor, even when start1 and end1 are indexes. Use string-cursor->index to convert a cursor result to an index.

#+BEGIN_SRC scheme (string-contains "eek -- what a geek." "ee"

                12 18) ; Searches "a geek"
   => {Cursor 15}

#+END_SRC

The name of this procedure does not end with a question mark -- this is to indicate that it does not return a simple boolean (#t or #f). Rather, it returns either false (#f) or a cursor.

string-reverse returns the result string and does not alter its s parameter.

#+BEGIN_SRC scheme (string-reverse "Able was I ere I saw elba.")

   => ".able was I ere I saw elbA"

(string-reverse "Who stole the spoons?" 14 20)

   => "snoops"

#+END_SRC

Unicode note: Reversing a string simply reverses the sequence of code-points it contains. So a combining diacritic a coming after a base character b in string s would come out before b in the reversed result.

Note that the (apply string-append string-list) idiom is not robust for long lists of strings, as some Scheme implementations limit the number of arguments that may be passed to an n-ary procedure.

#+BEGIN_SRC scheme (string-concatenate (reverse string-list)) #+END_SRC

If the optional argument final-string is specified, it is consed onto the beginning of string-list before performing the list-reverse and string-concatenate operations.

If the optional argument end is given, only the characters up to but not including end in final-string are added to the result, thus producing

#+BEGIN_SRC scheme (string-concatenate

 (reverse (cons (substring final-string
                           (string-cursor-start final-string)
                           end)
                string-list)))

#+END_SRC

E.g.

#+BEGIN_SRC scheme (string-concatenate-reverse '(" must be" "Hello, I") " going.XXXX" 7)

 => "Hello, I must be going."

#+END_SRC

This procedure is useful in the construction of procedures that accumulate character data into lists of string buffers, and wish to convert the accumulated data into a single string when done.

The left-fold operator maps the kons procedure across the string from left to right

#+BEGIN_SRC scheme (... (kons s[2] (kons s[1] (kons s[0] knil)))) #+END_SRC

In other words, string-fold obeys the (tail) recursion

#+BEGIN_SRC scheme (string-fold kons knil s start end) =

   (string-fold kons (kons s[start] knil) start+1 end)

#+END_SRC

The right-fold operator maps the kons procedure across the string from right to left

#+BEGIN_SRC scheme (kons s[0] (... (kons s[end-3] (kons s[end-2] (kons s[end-1] knil))))) #+END_SRC

obeying the (tail) recursion

#+BEGIN_SRC scheme (string-fold-right kons knil s start end) =

   (string-fold-right kons (kons s[end-1] knil) start end-1)

#+END_SRC

;; Convert a string to a list of chars.

(string-fold-right cons '() s)

;; Count the number of lower-case characters in a string.

(string-fold (lambda (c count)

              (if (char-lower-case? c)
                  (+ count 1)
                  count))
            0
            s)
;; Double every backslash character in S.

(let* ((ans-len (string-fold (lambda (c sum)

                              (+ sum (if (char=? c #\\) 2 1)))
                            0 s))
      (ans (make-string ans-len)))
 (string-fold (lambda (c i)
                (let ((i (if (char=? c #\\)
                             (begin (string-set! ans i #\\) (+ i 1))
                             i)))
                  (string-set! ans i c)
                  (+ i 1)))
              0 s)
 ans)

#+END_SRC

#+BEGIN_SRC scheme (let ((s "abcde") (v '()))

 (string-for-each-cursor
   (lambda (cur) (set! v (cons (char->integer (string-ref/cursor s cur)) v)))
   s)
 v) => (101 100 99 98 97)

#+END_SRC

S is a string; start and end are optional arguments that demarcate a substring of s, defaulting to 0 and the length of s (i.e., the whole string). Replicate this substring up and down index space, in both the positive and negative directions. For example, if s = "abcdefg", start=3, and end=6, then we have the conceptual bidirectionally-infinite string

#+BEGIN_EXAMPLE ... d e f d e f d e f d e f d e f d e f d ... ... -9 -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7 +8 +9 ... #+END_EXAMPLE

string-replicate returns the substring of this string beginning at index from, and ending at to. Note that these arguments cannot be cursors. It is an error if from is greater than to.

#+BEGIN_SRC scheme (string-append (substring/cursors s1 (string-cursor-start s1) start1)

              (substring/cursors s2 start2 end2)
              (substring/cursors s1 end1 (string-cursor-end s1)))

#+END_SRC

That is, the segment of characters in s1 from start1 to end1 is replaced by the segment of characters in s2 from start2 to end2. If start1=end1, this simply splices the s2 characters into s1 at the specified index.

(string-replace "It's easy to code it up in Scheme." "lots of fun" 5 9) =>

   "It's lots of fun to code it up in Scheme."

(define (string-insert s i t) (string-replace s t i i))

(string-insert "It's easy to code it up in Scheme." 5 "really ") =>

   "It's really easy to code it up in Scheme."

#+END_SRC

Grammar is a symbol with the same meaning as in the string-join procedure. If it is infix, which is the default, processing is done as described above, except that an empty s produces the empty list; if it is strict-infix, an empty s signals an error. The values prefix and suffix cause a leading/trailing empty string in the result to be suppressed.

If limit is a non-negative exact integer, at most that many splits occur, and the remainder of string is returned as the final element of the list (thus, the result will have at most limit+1 elements). If limit is not specified or is #f, then as many splits as possible are made. It is an error if limit is any other value.

Use SRFI 115's regexp-split to split on a regular expression rather than a simple string.

If the string is unaltered by the filtering operation, these functions may return either s or a copy of s.

I have placed this source on the Net with an unencumbered, "open" copyright. The prefix/suffix and comparison routines in this code had (extremely distant) origins in MIT Scheme's string lib, and were substantially reworked by myself. Being derived from that code, they are covered by the MIT Scheme copyright, which is a generic BSD-style open-source copyright. See the source file for details.

The KMP string-search code was influenced by implementations written by Stephen Bevan, Brian Denheyer and Will Fitzgerald. However, this version was written from scratch by myself.

The remainder of the code was written by myself for scsh or for this SRFI; I have placed this code under the scsh copyright, which is also a generic BSD-style open-source copyright.

The code is written for portability and should be straightforward to port to any Scheme. The source comments contains detailed notes describing the non-R5RS dependencies.

The library is written for clarity and well-commented. Fast paths are provided for common cases. This is not to say that the implementation can't be tuned up for a specific Scheme implementation. There are notes in the comments addressing ways implementors can tune the reference implementation for performance.

In short, I've written the reference implementation to make it as painless as possible for an implementor -- or a regular programmer -- to adopt this library and get good results with it.

Another implementation, derived from Chibi Scheme's SRFI 130, is present in the foof subdirectory. This implementation is smaller but may be slower. It can be more easily adapted to Schemes that differentiate between indexes and cursors.

The design of this library benefited greatly from the feedback provided during the SRFI discussion phase. Among those contributing thoughtful commentary and suggestions, both on the mailing list and by private discussion, were Paolo Amoroso, Lars Arvestad, Alan Bawden, Jim Bender, Dan Bornstein, Per Bothner, Will Clinger, Brian Denheyer, Mikael Djurfeldt, Kent Dybvig, Sergei Egorov, Marc Feeley, Matthias Felleisen, Will Fitzgerald, Matthew Flatt, Arthur A. Gleckler, Ben Goetter, Sven Hartrumpf, Erik Hilsdale, Richard Kelsey, Oleg Kiselyov, Bengt Kleberg, Donovan Kolbly, Bruce Korb, Shriram Krishnamurthi, Bruce Lewis, Tom Lord, Brad Lucier, Dave Mason, David Rush, Klaus Schilling, Jonathan Sobel, Mike Sperber, Mikael Staldal, Vladimir Tsyshevsky, Donald Welsh, and Mike Wilson. I am grateful to them for their assistance.

I am also grateful to the authors, implementors and documentors of all the systems mentioned in the introduction. Aubrey Jaffer and Kent Pitman should be noted for their work in producing Web-accessible versions of the R5RS and Common Lisp spec, which was a tremendous aid.

This is not to imply that these individuals necessarily endorse the final results, of course.

During this document's long development period, great patience was exhibited by Mike Sperber, who is the editor for the SRFI, and by Hillary Sullivan, who is not.

The Common Lisp "HyperSpec," produced by Kent Pitman, is essentially the ANSI spec for Common Lisp: http://www.lispworks.com/documentation/HyperSpec/Front/index.htm.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.