Next: , Up: strings   [Index]


25.1 Introduction

Library organisation

The functions are split in a high level library, (vicare containers strings), and a low level one, (vicare containers strings low). The main difference between the two layers is that the high level library makes heavy usage of macros to implement the string views, which allow accessing substrings using a friendly syntax.

All the functions in the low level library are bound to identifiers starting with %; for example: string-prefix? is a high level macro, %string-prefix? is its low level function homologous.

The model for strings

This library relies on the string representation of the underlying Scheme implementation: Strings are sequences of “code points” or character encodings. Operations such as comparison or reversal are always done code point by code point.

It’s possible that a legal string might not be a sensible “text” sequence; for example, consider a string comprised entirely of zero–width Unicode accent characters with no preceding base character to modify: It is a legal string, albeit one that does not make sense when interpreted as a sequence of natural–language text. The routines in (vicare containers strings) do not handle these “text” concerns.

Direction of iteration

The library accesses the characters of a string in left–to–right or right–to–left order depending on the convenience of the algorithm.

Handling side effects

Care must be taken when using functions with side effects. One reason for this is that some of the supported Scheme implementations will collapse equal datum strings in the source code to the same string in the program. Mutating such a string in a point of the program, will make the change visible to other points in the program. For example:

(import (rnrs)
  (rnrs mutable-strings))

(define str1 "abcd")
(define str2 "abcd")

(string-set! str1 2 #\9)
(write str2)

when executed by Ypsilon will print "ab9d", while when executed by Mosh will print "abcd" (last verified with revisions checked out on Fri Jun 5, 2009). To avoid this problem completely, we can change the program in this way:

(import (rnrs)
  (rnrs mutable-strings))

(define str1 (string-copy "abcd"))
(define str2 (string-copy "abcd"))

(string-set! str1 2 #\9)
(write str2)

of course we may want to wrap into string-copy only the strings that are meant to be mutated.


Next: , Up: strings   [Index]