buffer - growing buffers of text and bytes.
Concatenating with + copies the whole string every time, so building a string piece by piece costs O(n^2). A buffer keeps the pieces apart and joins them once.
¶Builder = fn()source
Builder accumulates strings.
b = buffer.Builder()
b.Write("hello ")
b.Write("world")
b.String()
¶Write = fn(s)source
Write appends s to the buffer and returns the number of bytes written.
¶WriteByte = fn(c)source
WriteByte appends a single byte given as its integer value.
¶Len = fn()source
Len returns the number of bytes accumulated so far.
¶String = fn()source
String returns the concatenation of everything written.
The pieces are joined in pairs, and the pairs in pairs, until one is left. Adding them up one at a time would copy everything accumulated so far at every step, which is the O(n^2) this type exists to avoid - and was what it did. Halving the count each round makes it O(n log n) of copying, all of it done by the concatenation itself rather than a byte at a time here.
¶Reset = fn()source
Reset empties the buffer.
¶Buffer = fn()source
Buffer accumulates bytes. Read and Write make it usable wherever a stream of bytes is expected.
¶Write = fn(x)source
Write appends bytes, a string or a list of byte values.
¶WriteByte = fn(c)source
¶Len = fn()source
Len returns the number of bytes still to be read.
¶Read = fn(n)source
Read returns up to n bytes and consumes them, or all of them if n is null.
¶Bytes = fn()source
Bytes returns everything that hasn't been read, without consuming it.