module watt.text.sink

Construct long strings of text efficiently.

The concatenation operator (~ and ~=) while convenient, is not efficient, as it likely causes an allocation each and every time it occurs.

The StringSink allocates memory in large chunks, reducing the overall amount of allocation that takes place. Using this if you are going to be building a lot of strings is recommended.

Code Map

//! Construct long strings of text efficiently.
module watt.text.sink;


//! A Sink is a delegate that accepts strings and concatenates them
//! efficiently.
alias Sink = core.rt.format.Sink;
//! A SinkArg is shorthand for the string argument to a Sink.
alias SinkArg = core.rt.format.SinkArg;

//! Helps construct long strings efficiently.
struct StringSink
{
public:
	//! Add str to this sink.
	fn sink(str: scope (SinkArg)) { }
	//! Get the contents of this sink as a string.
	fn toString() string { }
	//! Get the contents of this sink as a mutable array of characters.
	fn toChar() char[] { }
	//! Safely get the backing storage from the sink without copying.
	fn toSink(sink: scope (Sink)) { }
	//! Clear this sink.
	fn reset() { }
	fn length() size_t { }
}

struct StringsSink
{
public:
	enum MinSize;
	enum MaxSize;


public:
	alias Sink = void delegate(scope (SinkArg));
	alias SinkArg = scope (scope (T)[]);


public:
	fn length() size_t { }
	fn sink(type: T) { }
	fn append(arr: scope (scope (T)[])) { }
	fn append(s: SinkStruct) { }
	fn popLast() T { }
	fn getLast() T { }
	fn get(i: size_t) T { }
	fn set(i: size_t, n: T) { }
	fn setLast(i: T) { }
	fn toSink(sink: Sink) { }
	fn toArray() T[] { }
	fn borrowUnsafe() T[] { }
	fn reset() { }
}
alias Sink

A Sink is a delegate that accepts strings and concatenates them efficiently.

alias SinkArg

A SinkArg is shorthand for the string argument to a Sink.

struct StringSink

Helps construct long strings efficiently.

Example

ss: StringSink;
ss.sink("Hello, ");
ss.sink("world.");
assert(ss.toString() == "Hello, world.");
ss.reset();
assert(ss.toString() == "");
fn sink(str: scope (SinkArg))

Add str to this sink.

fn toString() string

Get the contents of this sink as a string.

fn toChar() char[]

Get the contents of this sink as a mutable array of characters.

fn toSink(sink: scope (Sink))

Safely get the backing storage from the sink without copying.

fn reset()

Clear this sink.