StringBuf
is a growable string buffer for efficient incremental
string building. Unlike String.append (which allocates a new string on
every call), StringBuf tracks capacity and grows geometrically, giving
amortized O(1) appends.
Installation
(load "git@github.com:carpentry-org/strbuf@0.1.0")
Usage
(let [sb (StringBuf.create)]
(do
(StringBuf.append-str &sb "hello")
(StringBuf.append-char &sb \space)
(StringBuf.append-str &sb "world")
(println* &(StringBuf.to-string &sb))
(StringBuf.delete sb)))
append-bytes
(Fn [(Ref StringBuf a), (Ref (Array Byte) b)] ())
appends raw bytes to the buffer.
append-double
(Fn [(Ref StringBuf a), Double] ())
appends a double as its string representation.
append-int
(Fn [(Ref StringBuf a), Int] ())
appends an integer as its decimal string representation.
str
(Fn [(Ref StringBuf a)] String)
returns a copy of the buffer contents as a String without modifying the buffer.
to-string
(Fn [(Ref StringBuf a)] String)
returns the buffer contents as an owned String and resets the buffer to empty. The buffer's allocation is kept for reuse.
with-capacity
(Fn [Int] StringBuf)
creates an empty StringBuf with the given initial capacity.