Module string/string_builder
Mutable UTF-8 string builder for efficient incremental construction.
Types
Mutable buffer for building a String incrementally.
Use StringBuilder when you need to construct a string from many parts,
appending bytes or strings in a loop, before converting to an immutable
String with to_string().
Example
sb := StringBuilder.new();
sb.write_str("Hello");
sb.write_str(", ");
sb.write_string(`world`);
sb.write_byte(u8(33)); // '!'
result := sb.to_string();
assert(result == `Hello, world!`, "built string");
Fields
| Name | Type | Description |
|---|---|---|
_buf | ArrayList(u8) |
impl(StringBuilder, ...)
new : (StringBuilder) fn() -> StringBuilderCreate a new, empty StringBuilder.
Returns: StringBuilder
with_capacity : (StringBuilder) fn(capacity : usize) -> StringBuilderCreate a StringBuilder pre-allocated for capacity bytes.
Parameters
| Name | Type | Notes |
|---|---|---|
capacity | usize |
Returns: StringBuilder
len : (StringBuilder) fn(self : StringBuilder) -> usizeReturns the current number of bytes in the buffer.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder |
Returns: usize
is_empty : (StringBuilder) fn(self : StringBuilder) -> boolwrite_str : (StringBuilder) fn(self : StringBuilder, s : str) -> unitAppend a str (raw byte slice) to the buffer.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
s | str |
Returns: unit
write_string : (StringBuilder) fn(self : StringBuilder, s : String) -> unitwrite_byte : (StringBuilder) fn(self : StringBuilder, b : u8) -> unitwrite_rune : (StringBuilder) fn(self : StringBuilder, r : rune) -> unitAppend a single Unicode code point, encoded as UTF-8.
Example
sb := StringBuilder.new();
sb.write_rune(rune(0x1F600)); // 😀
sb.write_rune(rune(0x41)); // 'A'
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
r | rune |
Returns: unit
write_line : (StringBuilder) fn(self : StringBuilder, s : String) -> unitAppend a String followed by a newline byte (\n).
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
s | String |
Returns: unit
to_string : (StringBuilder) fn(self : StringBuilder) -> StringConsume the builder and return the accumulated String.
The builder is left empty after this call.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder |
Returns: String
clear : (StringBuilder) fn(self : StringBuilder) -> unit