Next: strings cons, Previous: strings convention, Up: strings [Index]
String views are syntactic sugar to allow substring specification with
minimum overhead; views make use of auxiliary syntaxes exported by the
library (vicare containers auxiliary-syntaxes)
and reexported by
the (vicare containers strings)
library. Many low level string functions
act on substrings specified with triplets of arguments:
To select the substring "hello"
from the string "ciao,
hello, salut"
we have to determine the half–open range of code points,
which is [6, 11), then apply a function to the arguments:
(%the-function "ciao, hello, salut" 6 11) ;; 0123456789012345678 ;; 0 1
while to select the whole string we can do:
(%the-function "ciao, hello, salut" 0 18)
or:
(let ((str "ciao, hello, salut")) (%the-function str 0 (string-length str)))
With the string views implemented by (vicare containers strings)
, the low
level function is wrapped by a high level syntax which can be invoked
as:
(the-function "ciao, hello, salut") ; select the whole string (the-function (view "ciao, hello, salut")) ; select the whole string (the-function (view "ciao, hello, salut" (start 6))) ; select the substring [6, 18) (the-function (view "ciao, hello, salut" (start 6) (past 11))) ; select the substring [6, 11) (the-function (view "ciao, hello, salut" (past 11))) ; select the substring [0, 11)
instead of a triplet of arguments, the string view is a single argument that can be:
?string
(view ?string)
The string itself or an unquoted list holding the string itself,
prefixed by the view
auxiliary syntax: It selects the whole
string.
(view ?string (start ?start-index))
An unquoted list holding the string and the start index, with auxiliary
syntaxes view
and start
: it selects the substring from the
start index to the end.
(view ?string (past ?past-index))
An unquoted list holding the string and the past index, with auxiliary
syntaxes view
and past
: it selects the substring from zero
to the selected past index.
(view ?string (start ?start-index) (past ?past-index))
An unquoted list holding the string, the start index and the past index,
with auxiliary syntaxes view
, start
and past
: it
selects the substring between the start and past indexes.
?string
, ?start-index
and
?past-index
can be arbitrary Scheme expressions. High
level macros accepting two or more substrings as arguments, support
string views for all of them. When we are concerned with the overhead
of string views, we can use the low level functions directly.
When the start and past index are negative, the view
syntax
normalises them as:
(if (negative? idx) (+ idx (string-length string)) idx)
so that negative indices are counted from the end of the string: -1 selects the ultimate character, -2 selected the penultimate character and so on.
In the following documentation: low level functions and high level macros are documented together; only the meaning of arguments to the low level function are described, the meaning of high level arguments is obvious.
Next: strings cons, Previous: strings convention, Up: strings [Index]