For Gauche 0.9.10


Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]

11.37 srfi-185 - Linear adjustable-length strings

Module: srfi-185

This module provides a linear-update version of string-append and string-replace. “Linear update” means the caller won’t access the first string argument, hence the implementation can reuse it to store the result, for the efficiency.

Note that, in Gauche, a string is just a pointer to an immutable string body. Mutation of a string is actually constructing a new string body and swapping the pointer, so it has no performance advantage to immutable versions. And in fact, we implement these without mutating their arguments.

This module also provides macros with the same name as srfi-118 procedures (see Simple adjustable-size strings), which set! the result to its first argument, so they can work as a drop-in replacement to srfi-118.

Function: string-append-linear! dst str-or-char …

[SRFI-185] {srfi-185} Returns a string which is a concatenation of a string dst and the arguments. The second argument an after can be a string or a character. The caller shouldn’t access dst after calling this procedure, for the implementation may destructively reuse the string passed to dst. Although Gauche won’t mutate dst, other implementations may, so portable code should adhere this restriction.

You can’t count on dst being mutated; you always have to use the returned string.

(string-append-linear! "abc" "def" #\g "hij")
  ⇒ "abcdefghij"
Function: stirng-replace-linear! dst dst-start dst-end src :optional src-start src-end

[SRFI-185] {srfi-185} Returns copy of a string dst except the portion of from dst-start (inclusive) to dst-end (exclusive), by a string src (from src-start to src-end). The caller shouldn’t access dst after calling this procedure, for the implementation may destructively reuse the string passed to dst. Although Gauche won’t mutate dst, other implementations may, so portable code should adhere this restriction.

Gauche allows string cursors, as well as integer character index, in dst-start, dst-end, src-start and src-end arguments. (See String cursors).

Macro: string-append! dst string-or-char …
Macro: string-replace! dst dst-start dst-end src :optional src-start src-end

[SRFI-185] {srfi-185} These macros expand into the following forms:

(set! dst (string-append-linear! dst string-or-char …))

(set! dst (string-replace-linear! dst dst-start dst-end src src-start src-end))

Hence they can be used for the code that expects dst to contain the result after calling these macros. Gauche supports generalized set!, so dst can be a procedure call with a setter defined.

(define x (list (string-copy "abc")))

(string-append! (car x) #\d #\e #\f)

x ⇒ ("abcdef")

The code that uses srfi-118 (see Simple adjustable-size strings) is likely to be replaced using these macros.


Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]