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.2.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-bool
(Fn [(Ref StringBuf a), Bool] ())
appends a bool as \342\200\234true\342\200\235 or \342\200\234false\342\200\235.
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-float
(Fn [(Ref StringBuf a), Float] ())
appends a float as its string representation.
append-int
(Fn [(Ref StringBuf a), Int] ())
appends an integer as its decimal string representation.
append-long
(Fn [(Ref StringBuf a), Long] ())
appends a long 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.