15 Strings
A string is an object that represent a sequence of characters (for now, only ASCII is supported in klisp, in the future, full UNICODE will be supported). The external representation of strings consists of a leading “””, the characters of the string and a closing “””. Both double quote and backslash should be escaped to appear withing strings. Some other characters also have an escaped form for convenience. All of these are written with a leading slash (“\”). In klisp these are: double quote (“\””), backslash (“\\”), null (“\0”), alarm (“\a”), backspace (“\b”), tab (“\t”), newline (“\n”), return (“\r”), vertical tab (“\v”), and formfeed (“\f”). You can also use inline hex escapes to include arbitary unicode codepoints (only ASCII range supported for now). The syntax is “\x<hex codepoint>;”. New lines can be escaped to simplify the accomodation of literal strings in source code, to do this: use “\” followed by any ammount of intraline whitespace, a new line and another ammount of intraline whitespace. All of that intraline whitespace and the newline, together with the leading slash is discarded by the reader and doesn’t end up in the string being read.
A string has a length that is fixed at creation time, and as many characters, indexed from 0
to length-1
.
Strings may be mutable or immutable. If an attempt is made to mutate an immutable string, an error is signaled. Two immutable strings are “eq?” iff they are “equal?”. Two mutable strings are “eq?” if they were created by the same constructor call. Two mutable strings are “equal?” iff they are “string=?”. For now it is undefined if a mutable and an immutable strings that are “string=?” are “equal?” or not. The only exception is the empty string. There is only one empty string (all empty strings are “eq?” to each other) and it should be considered immutable. Even if an attempt is made to return a new empty string (like calling (string)
, the canonical immutable empty string is returned. The string type is encapsulated.
SOURCE NOTE: This section is still missing from the report. The features defined here were taken mostly from r7rs scheme. It is possible that in the future, klisp only admits immutable strings (like lua and java), and that operations for contructing strings are moved to a new type (like Java’s StringBuilder/StringBuffer). But for now, compatibility with r7rs was preferred/simpler.
The primitive type predicate for type string.
string?
returns true iff all the objects inobjects
are of type string.
— Applicative: mutable-string? (mutable-string? objects)
The primitive type predicates for types immutable string and mutable string. These return true iff all the objects in
objects
are of type immutable string or mutable string respectively.SOURCE NOTE: these aren’t provided in the Kernel report, but added for convenience. These can be implemented in standard kernel by using guards.
— Applicative: string<? (string<? . strings)
— Applicative: string<=? (string<=? . strings)
— Applicative: string>? (string>? . strings)
— Applicative: string>=? (string>=? . strings)
These predicates compare any number of strings by their lexicographic order.
— Applicative: string-ci<? (string-ci<? . strings)
— Applicative: string-ci<=? (string-ci<=? . strings)
— Applicative: string-ci>? (string-ci>? . strings)
— Applicative: string-ci>=? (string-ci>=? . strings)
These predicates convert the strings to lowercase and then compare them using their lexicographic order.
Applicative
make-string
constructs and returns a new mutable string of lengthk
. Ifchar
is specified, then all characters in the returned string arechar
, otherwise the content of the string is unspecified.
Applicative
string
contructs and return a new mutable string composed of the character arguments.
Applicative
string-length
returns the length ofstring
.
Applicative
string-ref
returns the character ofstring
at positionk
. Ifk
is out of bounds (i.e. less than0
or greater or equal than(string-length string)
) an error is signaled.
Applicative
string-set!
replaces the character with indexk
instring
with characterchar
. Ifk
is out of bounds, orstring
is immutable, an error is signaled.
Applicative
string-fill!
replaces all the characters instring
with characterchar
. Ifstring
is an immutable string, an error is signaled.
Both
k1
&k2-1
should be valid indexes instring
. Also it should be the case thatk1 <= k2
.Applicative
substring
constructs and returns a new mutable string with lengthk2 - k1
, with the characters fromstring
, starting at indexk1
(inclusive) and ending at indexk2
(exclusive).
Applicative
string-append
constructs and returns a new mutable string consisting of the concatenation of all its arguments.
Applicative
string-copy
constructs and returns a new mutable string with the same length and characters asstring
.
Applicative
string->immutable-string
constructs and returns a new immutable string with the same length and characters asstring
.
— Applicative: list->string (list->string chars)
— Applicative: string->vector (string->vector string)
— Applicative: vector->string (vector->string vchars)
— Applicative: string->bytevector (string->bytevector string)
— Applicative: bytevector->string (bytevector->string bvchars)
These applicatives convert between strings and list of characters, vectors of characters, and bytevectors of characters. The objects returned by these applicatives are always mutable.
— Applicative: string-downcase (string-downcase string)
— Applicative: string-titlecase (string-titlecase string)
— Applicative: string-foldcase (string-foldcase string)
These applicatives perform the respective case folding on the passed
string
and return a new mutable strings as a result. The originalstring
is not modified. For now in klisp only ASCII is implemented, and sostring-foldcase
is the same asstring-downcase
.