Library

std/string_builder

Efficient string construction over a list of parts.

1 file lib/std/string_builder/string_builder.vader 5 exports
sb :: StringBuilder {}
sb.append("Hello")
sb.append_char('!')
sb.to_string()   // "Hello!"

Accumulates string references in a string[] : each append is an O(1) reference push, no byte copy. The bytes are moved exactly once, in bulk, at to_string : one Buffer sized to the total byte length, each part copied in via the write_string primitive, interned in a single pass via intern_string. This is the lean, off-runtime equivalent of the former vader_string_concat_all host intrinsic: finalisation, not every append, pays for the copy.

The byte primitives (new_byte_buffer / write_string / intern_string) live in std/core (the prelude) and are reached with no import : the allocation is an @intrinsic free fn, the others @intrinsic impl members resolved through the impl registry. bytes / bytes_to_string / utf8_encode are likewise ambient std/core primitives. So the builder itself imports nothing; only its tests import std/testing.

Types

StringBuilder :: struct

String accumulator with O(1) append. Holds the appended fragments by reference ; to_string (and ${sb} interpolation) concatenates them into a single interned string in one pass.

Implements Display to_string :: fn(self) -> string

Functions

byte_len :: fn(self: StringBuilder) -> i64

Byte length of what the builder holds: the byte_len of its to_string, without assembling it. It walks the parts, which keeps append a bare push: asking after every append makes a loop quadratic.

Parameters

selfStringBuilder builder receiver.

Returns

i64

Example

expand
sb :: StringBuilder {}
sb.append("été")
sb.byte_len()   // 5
sb.append("été")
sb.byte_len()   // 5
append :: fn(self: StringBuilder, s: string) -> void

Append a string: an O(1) reference push, no byte copy.

Parameters

selfStringBuilder builder receiver.
sstring UTF-8 string to append.
append_char :: fn(self: StringBuilder, c: char) -> void

Append a single Unicode codepoint, UTF-8-encoded then interned as a part.

Parameters

selfStringBuilder builder receiver.
cchar codepoint to encode.
append_repeated :: fn(self: StringBuilder, c: char, count: i32) -> void

Append count copies of c. The codepoint is UTF-8-encoded and interned once, then pushed count times (each push shares the one interned atom). Negative or zero counts are a no-op.

Parameters

selfStringBuilder builder receiver.
cchar codepoint to repeat.
counti32 number of copies to append (≤ 0 → no-op).

Example

expand
sb :: StringBuilder {}
level :: 2
sb.append_repeated(' ', level * 2)   // indentation
sb.append_repeated('-', 40)          // divider line
sb.append_repeated(' ', level * 2)   // indentation
sb.append_repeated('-', 40)          // divider line
Written by vader doc. Icons: Codicons, CC BY 4.0. Fonts: Atkinson Hyperlegible, JetBrains Mono, OFL.