Grow v0.1.0

Source
Grows the block to hold at least a given number of bytes.

Package: Text

Signature

func Grow(self, required: uint);

Parameters

NameTypeDescription
requireduintThe total number of bytes the block must hold.

Remarks

The size is a total, not an increment — it counts the bytes already written. Reserve is the same thing stated relative to Length, and is usually what you want.

A required that already fits is left alone: the block never shrinks here. Otherwise the capacity doubles from a 16 byte floor until it covers required, which is why it can end up larger than what was asked for, and why a run of Append calls costs an amortized constant rather than a reallocation each time.

Growing may move the block, so a pointer taken from Data beforehand must not be used after.

Example

import Text::StringBuilder;

func Main() -> int {
    var builder = StringBuilder::New();

    builder.Grow(20);
    builder.Capacity(); // 32 -- doubled from 16 until it covered 20
    builder.Length();   // 0 -- growing writes nothing

    builder.Grow(8);
    builder.Capacity(); // 32 still -- the block never shrinks here

    builder.Free();
    return 0;
}

See also