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.

— Applicative: string? (string? . objects)

The primitive type predicate for type string. string? returns true iff all the objects in objects are of type string.

— Applicative: immutable-string? (immutable-string? objects)
— 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)
— 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)
— Applicative: string-ci>=? (string-ci>=? . strings)

These predicates convert the strings to lowercase and then compare them using their lexicographic order.

— Applicative: make-string (make-string k [char])

Applicative make-string constructs and returns a new mutable string of length k. If char is specified, then all characters in the returned string are char, otherwise the content of the string is unspecified.

— Applicative: string (string . chars)

Applicative string contructs and return a new mutable string composed of the character arguments.

— Applicative: string-length (string-length string)

Applicative string-length returns the length of string.

— Applicative: string-ref (string-ref string k)

Applicative string-ref returns the character of string at position k. If k is out of bounds (i.e. less than 0 or greater or equal than (string-length string)) an error is signaled.

— Applicative: string-set! (string-set! string k char)

Applicative string-set! replaces the character with index k in string with character char. If k is out of bounds, or string is immutable, an error is signaled.

— Applicative: string-fill! (string-fill! string char)

Applicative string-fill! replaces all the characters in string with character char. If string is an immutable string, an error is signaled.

— Applicative: substring (substring string k1 k2)

Both k1 & k2-1 should be valid indexes in string. Also it should be the case that k1 <= k2.

Applicative substring constructs and returns a new mutable string with length k2 - k1, with the characters from string, starting at index k1 (inclusive) and ending at index k2 (exclusive).

— Applicative: string-append (string-append . strings)

Applicative string-append constructs and returns a new mutable string consisting of the concatenation of all its arguments.

— Applicative: string-copy (string-copy string)

Applicative string-copy constructs and returns a new mutable string with the same length and characters as string.

— Applicative: string->immutable-string (string->immutable-string string)

Applicative string->immutable-string constructs and returns a new immutable string with the same length and characters as string.

— Applicative: string->list (string->list string)
— 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-upcase (string-upcase string)
— 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 original string is not modified. For now in klisp only ASCII is implemented, and so string-foldcase is the same as string-downcase.