srfi.185
- Linear adjustable-length strings ¶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 srfi.118
- Simple adjustable-size strings), which set!
the result to its first argument, so they can work as a drop-in
replacement to srfi.118
.
[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"
[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).
[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 srfi.118
- Simple adjustable-size strings) is
likely to be replaced using these macros.