A growable string buffer for Carp.
(load "git@github.com:carpentry-org/strbuf@0.1.0")StringBuf provides amortized O(1) appends, unlike String.append which
allocates a new string on every call.
(let [sb (StringBuf.create)]
(do
(StringBuf.append-str &sb "HTTP/1.1 ")
(StringBuf.append-int &sb 200)
(StringBuf.append-str &sb " OK")
(StringBuf.append-crlf &sb)
(println* &(StringBuf.to-string &sb))
(StringBuf.delete sb)))to-string returns the contents and resets the buffer for reuse.
str copies the contents without resetting.
StringBuf.create/StringBuf.with-capacity— constructorsStringBuf.append-str— append a stringStringBuf.append-char— append a single characterStringBuf.append-bytes— append raw bytesStringBuf.append-int— append integer as decimalStringBuf.append-double— append double as stringStringBuf.append-crlf— append\r\nStringBuf.length— current byte countStringBuf.to-string— extract as String, reset bufferStringBuf.str— copy as String, keep bufferStringBuf.clear— reset without freeingStringBuf.delete— free the buffer
carp -x test/strbuf.carp
Have fun!