Next: srfi strings ratio r5rs, Previous: srfi strings ratio naming, Up: srfi strings ratio [Index]
Some Scheme implementations, e.g. Guile and T, provide ways to construct substrings that share storage with other strings. This facility is called “shared–text substrings”. Shared–text substrings can be used to eliminate the allocation and copying time and space required to produce substrings, which can be a tremendous savings for some applications, reducing a linear–time operation to constant time. Additionally, some algorithms rely on the sharing property of these substrings: the application assumes that if the underlying storage is mutated, then all strings sharing that storage will show the change. However, shared–text substrings are not a common feature; most Scheme implementations do not provide them.
SRFI-13 takes a middle ground with respect to shared–text substrings. In particular, a Scheme implementation does not need to have shared–text substrings in order to implement this SRFI.
There is an additional form of storage sharing enabled by some SRFI-13
procedures, even without the benefit of shared–text substrings. In
some cases, some SRFI-13 routines are allowed to return as a result
one of the strings that was passed in as a parameter. For example, when
constructing a substring with the substring/shared
procedure, if
the requested substring is the entire string, the procedure is permitted
simply to return the original value. That is:
(eq? s (substring/shared s 0 (string-length s))) ⇒ #t or #f
whereas the R5RS substring
function is required to allocate a
fresh copy:
(eq? s (substring s 0 (string-length s))) ⇒ #f
In keeping with SRFI-13’s general approach to sharing, compliant implementations are allowed, but not required, to provide this kind of sharing. Hence, procedures may not rely upon sharing in these cases.
Most procedures that permit results to share storage with inputs have equivalent procedures that require allocating fresh storage for results. If an application wishes to be sure a new, fresh string is allocated, then these “pure” procedures should be used.
Fresh copy guaranteed | Sharing permitted |
---|---|
string-copy | substring/shared |
string-copy | string-take string-take-right |
string-copy | string-drop string-drop-right |
string-concatenate | string-concatenate/shared |
string-append | string-append/shared |
string-concatenate-reverse | string-concatenate-reverse/shared |
string-pad string-pad-right | |
string-trim string-trim-right | |
string-trim-both | |
string-filter string-delete |
On the other hand, the functionality is present to allow one to write efficient code without shared–text substrings. You can write efficient code that works by passing around start/end ranges indexing into a string instead of simply building a shared–text substring. The API would be much simpler without this consideration; if we had cheap shared–text substrings, all the start/end index parameters would vanish. However, since SRFI-13 does not require implementations to provide shared–text substrings, the extended API is provided.
Next: srfi strings ratio r5rs, Previous: srfi strings ratio naming, Up: srfi strings ratio [Index]