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

external

(Fn [(Ref StringBuf a), (Ref (Array Byte) b)] ())

appends raw bytes to the buffer.

append-char

external

(Fn [(Ref StringBuf a), Char] ())

appends a single character to the buffer.

append-crlf

external

(Fn [(Ref StringBuf a)] ())

appends \r\n (HTTP line ending).

append-double

external

(Fn [(Ref StringBuf a), Double] ())

appends a double as its string representation.

append-int

external

(Fn [(Ref StringBuf a), Int] ())

appends an integer as its decimal string representation.

append-str

external

(Fn [(Ref StringBuf a), (Ref String b)] ())

appends a string to the buffer.

clear

external

(Fn [(Ref StringBuf a)] ())

resets the buffer to empty without freeing memory.

copy

external

(Fn [(Ref StringBuf a)] StringBuf)

create

external

(Fn [] StringBuf)

creates an empty StringBuf with default capacity (256 bytes).

delete

external

(Fn [StringBuf] ())

frees the buffer.

length

external

(Fn [(Ref StringBuf a)] Int)

returns the current length of the buffer in bytes.

str

external

(Fn [(Ref StringBuf a)] String)

returns a copy of the buffer contents as a String without modifying the buffer.

to-string

external

(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

external

(Fn [Int] StringBuf)

creates an empty StringBuf with the given initial capacity.