srfi-130- Cursor-based string library
This is an reduced version of
srfi-13 that supports cursors in
addition to indexes. The consistency with R7RS and recent srfis are
Gauche supports string cursors natively (see String cursors), so all built-in and srfi-13 string procedures that takes string indexes can als otake string cursors as well. You want to use this module explicitly, though, if you’re writing a portable code.
The following are built-in. See String cursors, for the details.
string-cursor? string-cursor-start string-cursor-end string-cursor-next strig-cursor-prev string-cursor-forward string-cursor-back string-cursor=? string-cursor<? string-cursor<=? string-cursor>? string-cursor>=? string-cursor-diff string-cursor->index string-index->cursor
The following procedures are simply aliases of the builtin version
string->list/cursors string->vector/cursors string-copy/cursors string-ref/cursor substring/cursors
The following procedures are defined in srfi-13. See String library, for the details. Note: Some of those procedures in srfi-13 accept only indexes. In srfi-130, both indexes and cursors are accepted. Our srfi-13 implementation shares the same procedure with srfi-130, so they accept both indexes and cursors but such code won’t be portable as srfi-13.
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-concatenate string-concatenate-reverse string-count string-filter string-reverse
The following procedures are the same as srfi-152, except that the arguments that takes integer indexes can also accept string cursors (see String library (reduced)):
string-replicate string-remove string-split
The following procedures are also defined in srfi-13, but srfi-130 redefines them to return a string cursor instead of an integer index. They are described below.
string-index string-index-right string-skip string-skip-right string-contains string-contains-right
Find the position of a character that satisfies pred
does not satisfy pred
string-skip-right), in a string.
While srfi-13’s procedures of the same names returns integer index, these returns a string cursor instead.
They always return a string cursor.
The returned cursor from
points the leftmost character that satisfies/does not satsify pred,
while the returned cursor from
string-index-right points the next (right) character of the rightmost
one that satisfies/does not satisfy pred.
If there’s no character that satisfies/does not satisfy pred,
string-skip returns a cursor at the end
of the string, while
returns a cursor at the beginning of the string.
(This is easier to understand if you think the cursor is between characters; if we scan a string from left to right, the cursor looks at the character on its right; if we scan from right to left, the cursor looks at the character on its left.)
The optional start/end can be string cursors or integer indexes to limit the region of string to search.
(define *s* "abc def ghi") (string-cursor->index *s* (string-index *s* char-whitespace?)) ⇒ 3 (string-cursor->index *s* (string-index *s* char-whitespace? 4)) ⇒ 7 (string-cursor->index *s* (string-index-right *s* char-whitespace?)) ⇒ 8 (string-cursor->index *s* (string-index-right *s* char-whitespace? 0 7)) ⇒ 4
Search for a substring needle in a string haystack.
The substring is searched from left to right (
or right to left (
While srfi-13’s procedures of the same names returns an integer index when a substring is found, these procedures returns a string curosr of haystack.
If haystack doesn’t countain needle, they return
Calls proc with each cursor of string, from left to right,
excluding the post-end cursor. This is the cursor version of
string-for-each-index in srfi-13 (see SRFI-13 String mapping).