StringBuilder

public struct StringBuilder { /* private fields */ }

Write-only buffer for efficient string construction. No COW, no RcBox, no isUnique checks — every append writes directly.

build() transfers ownership of the buffer into a new String without copying. The builder resets to empty and can be reused.

Examples

var b = StringBuilder(); b.append("hello"); b.append(char: ' '); b.append("world"); let s = b.build(); // "hello world", zero-copy

Representation

(ptr: Pointer[UInt8], len: Int64, cap: Int64).

Memory Model

Owns its buffer directly. build() donates the buffer to a String; the builder is left empty. deinit frees the buffer if build() was never called.

Properties

public var byteCount: Int64 { get }

Number of bytes written so far.

public var isEmpty: Bool { get }

True when nothing has been written.

Initializers

public init()

Creates an empty builder with no allocation.

public init(capacity: Int64)

Creates an empty builder with at least capacity bytes preallocated.

Methods

public mutating func append[__opaque_0](__opaque_0) where __opaque_0: Str

Appends the UTF-8 bytes of other to this builder. Accepts any type conforming to StrString, StringSlice, etc.

public mutating func append(char: Char)

Appends a single code point, encoding it as UTF-8.

internal mutating func appendByte(UInt8)

Appends a raw byte. Caller must ensure UTF-8 validity.

internal mutating func appendBytes(ptr: Pointer[UInt8], count: Int64)

Appends count bytes from ptr. Caller must ensure UTF-8 validity.

public mutating func build() -> String

Transfers the buffer into a new String without copying. The builder resets to empty and can be reused.

public mutating func clear()

Resets length to zero, keeping the allocated buffer for reuse.

ImplementsCloneable

Methods

public func clone() -> StringBuilder

Returns a copy with its own buffer.

Defined in lang/std/text/builder.ks