Module
pooka/string_tree
src/pooka/string_tree.pookaKeep the pieces until somebody asks for the whole parcel. Appending makes a branch in constant time. Rendering walks an explicit work list, preserving chunk order without recursive calls down a deep left spine. Text stays UTF-8; byte counts describe encoding, not visible characters.
assert string_tree.to_string(string_tree.from_string("oak")) == "oak"
API reference
class StringTree
An immutable tree. Only the private leaf and branch classes construct its values.
new() -> StringTree
fn() -> pooka/string_tree.StringTreeStart without any chunks.
import pooka/string_tree
assert string_tree.is_empty(string_tree.new())
from_string(value: String) -> StringTree
fn(String) -> pooka/string_tree.StringTreeKeep a chunk without copying its contents.
import pooka/string_tree
assert string_tree.to_string(string_tree.from_string("oak")) == "oak"
append(tree: StringTree, value: String) -> StringTree
fn(pooka/string_tree.StringTree, String) -> pooka/string_tree.StringTreeAttach one chunk in constant time.
assert string_tree.to_string(string_tree.append(string_tree.new(), "oak")) == "oak"
append_tree(left: StringTree, right: StringTree) -> StringTree
fn(pooka/string_tree.StringTree, pooka/string_tree.StringTree) -> pooka/string_tree.StringTreeAttach another tree without walking either side.
import pooka/string_tree
let left = string_tree.from_string("oak")
let right = string_tree.from_string(" and ash")
assert string_tree.to_string(string_tree.append_tree(left, right)) == "oak and ash"
prepend(tree: StringTree, value: String) -> StringTree
fn(pooka/string_tree.StringTree, String) -> pooka/string_tree.StringTreePlace one chunk before the earlier tree.
import pooka/string_tree
let tree = string_tree.from_string("owl")
assert string_tree.to_string(string_tree.prepend(tree, "little ")) == "little owl"
concat(trees: List(StringTree)) -> StringTree
fn(List(pooka/string_tree.StringTree)) -> pooka/string_tree.StringTreeJoin trees in input order without rendering their chunks.
import pooka/string_tree
let trees = [string_tree.from_string("a"), string_tree.from_string("b")]
assert string_tree.to_string(string_tree.concat(trees)) == "ab"
from_strings(values: List(String)) -> StringTree
fn(List(String)) -> pooka/string_tree.StringTreeKeep each supplied chunk as a leaf.
import pooka/string_tree
assert string_tree.to_string(string_tree.from_strings(["a", "b"])) == "ab"
to_string(tree: StringTree) -> String
fn(pooka/string_tree.StringTree) -> StringRender once, preserving every chunk's order.
import pooka/string_tree
assert string_tree.to_string(string_tree.from_strings(["oak", "leaf"])) == "oakleaf"
byte_size(tree: StringTree) -> Int
fn(pooka/string_tree.StringTree) -> IntCount encoded bytes without joining the chunks.
import pooka/string_tree
assert string_tree.byte_size(string_tree.from_string("é")) == 2
is_empty(tree: StringTree) -> Bool
fn(pooka/string_tree.StringTree) -> BoolAsk whether rendering would produce an empty value.
import pooka/string_tree
assert string_tree.is_empty(string_tree.from_string(""))