IntoString v0.1.0

Source
Hands the block over to a string, without copying it.

Package: Text

Signature

func IntoString(self) -> String;

Returns

A String owning the builder's block, which costs no copy. The block is Shrinked first, so the String owns one sized to its contents rather than to the builder's high-water mark.

The builder is left empty and owning nothing, so only the String has to be freed — a Free on the drained builder releases nothing and is not a double free. The builder stays usable and will take a fresh block on the next append.

Draining an empty builder yields the empty string.

This is how to take the result once the builder is finished with. Use ToString when the builder has to keep its contents.

Example

import Text::{ String, StringBuilder };

func Main() -> int {
    var builder = StringBuilder::WithCapacity(1024);
    builder.Append("Hello, ");
    builder.Append("Rux!");

    var greeting = builder.IntoString(); // "Hello, Rux!", the block handed over
    greeting.Length();                   // 11 -- and the block is 11 bytes, not 1024

    builder.IsEmpty();                   // true -- it owns nothing now
    greeting.Free();                     // the only Free needed
    return 0;
}

See also