std/string_builder
Efficient string construction over a list of parts.
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.
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
| self | StringBuilder | builder receiver. |
Returns
i64 Example
expand
sb :: StringBuilder {}
sb.append("été")
sb.byte_len() // 5sb.append("été")
sb.byte_len() // 5append :: fn(self: StringBuilder, s: string) -> void Append a string: an O(1) reference push, no byte copy.
Parameters
| self | StringBuilder | builder receiver. |
| s | string | 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
| self | StringBuilder | builder receiver. |
| c | char | 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
| self | StringBuilder | builder receiver. |
| c | char | codepoint to repeat. |
| count | i32 | 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 linesb.append_repeated(' ', level * 2) // indentation
sb.append_repeated('-', 40) // divider line