Shrink v0.1.0

Source
Drops the capacity the builder is not using.

Package: Text

Signature

func Shrink(self);

Remarks

Resizes the block down to exactly Length bytes, so the builder holds no more memory than its contents need. The contents are kept. A builder that is already exactly full is left alone, and one that is empty releases its block outright, as Free would.

IntoString leans on this, which is why the String it hands out owns a block sized to its contents rather than to the builder's high-water mark.

The resize 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::WithCapacity(1024);
    builder.Append("Rux");

    builder.Capacity(); // 1024
    builder.Shrink();
    builder.Capacity(); // 3 -- the 1021 unused bytes are handed back
    builder.Length();   // 3 -- the contents are kept

    builder.Free();
    return 0;
}

See also

  • StringBuilder — the builder type
  • Grow — the way back up
  • IntoString — shrinks before it hands the block over
  • Free — release the block instead of trimming it