Module

pooka/string_tree

src/pooka/string_tree.pooka

Keep 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.

Pooka
assert string_tree.to_string(string_tree.from_string("oak")) == "oak"

API reference

class

StringTree

src/pooka/string_tree.pooka:18
class StringTree

An immutable tree. Only the private leaf and branch classes construct its values.

function

new

src/pooka/string_tree.pooka:33
new() -> StringTree

Start without any chunks.

Pooka
import pooka/string_tree

assert string_tree.is_empty(string_tree.new())
function

from_string

src/pooka/string_tree.pooka:44
from_string(value: String) -> StringTree
INFERREDfn(String) -> pooka/string_tree.StringTree

Keep a chunk without copying its contents.

Pooka
import pooka/string_tree

assert string_tree.to_string(string_tree.from_string("oak")) == "oak"
function

append

src/pooka/string_tree.pooka:53
append(tree: StringTree, value: String) -> StringTree

Attach one chunk in constant time.

Pooka
assert string_tree.to_string(string_tree.append(string_tree.new(), "oak")) == "oak"
function

append_tree

src/pooka/string_tree.pooka:66
append_tree(left: StringTree, right: StringTree) -> StringTree

Attach another tree without walking either side.

Pooka
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"
function

prepend

src/pooka/string_tree.pooka:78
prepend(tree: StringTree, value: String) -> StringTree

Place one chunk before the earlier tree.

Pooka
import pooka/string_tree

let tree = string_tree.from_string("owl")
assert string_tree.to_string(string_tree.prepend(tree, "little ")) == "little owl"
function

concat

src/pooka/string_tree.pooka:90
concat(trees: List(StringTree)) -> StringTree

Join trees in input order without rendering their chunks.

Pooka
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"
function

from_strings

src/pooka/string_tree.pooka:101
from_strings(values: List(String)) -> StringTree
INFERREDfn(List(String)) -> pooka/string_tree.StringTree

Keep each supplied chunk as a leaf.

Pooka
import pooka/string_tree

assert string_tree.to_string(string_tree.from_strings(["a", "b"])) == "ab"
function

to_string

src/pooka/string_tree.pooka:112
to_string(tree: StringTree) -> String
INFERREDfn(pooka/string_tree.StringTree) -> String

Render once, preserving every chunk's order.

Pooka
import pooka/string_tree

assert string_tree.to_string(string_tree.from_strings(["oak", "leaf"])) == "oakleaf"
function

byte_size

src/pooka/string_tree.pooka:123
byte_size(tree: StringTree) -> Int
INFERREDfn(pooka/string_tree.StringTree) -> Int

Count encoded bytes without joining the chunks.

Pooka
import pooka/string_tree

assert string_tree.byte_size(string_tree.from_string("é")) == 2
function

is_empty

src/pooka/string_tree.pooka:136
is_empty(tree: StringTree) -> Bool
INFERREDfn(pooka/string_tree.StringTree) -> Bool

Ask whether rendering would produce an empty value.

Pooka
import pooka/string_tree

assert string_tree.is_empty(string_tree.from_string(""))