final class · System

StringBuilder

public final class StringBuilder
Kind
final class
Namespace
System
Assembly
System.Core
Access
public, final
Source file
StringBuilder.ant

Inheritance

Base classes
Any StringBuilder

Constructors

new()
#
new(i32 capacity)
#

Properties

i32 Length { get; }
#

read-only

How many bytes it holds.

Methods

Append(str text) : StringBuilder
#

Adds the text.

Append(str text, i32 start, i32 count) : StringBuilder
#

Adds count bytes of the text from start.

Append(u16 unit) : StringBuilder
#

Adds one character (a UTF-16 code unit, as character literals are), in UTF-8.

AppendCodePoint(u32 point) : StringBuilder
#

Adds one code point in UTF-8.

Append(i64 value) : StringBuilder
#

Adds the number as text (written straight into the buffer).

AppendLine(str text) : StringBuilder
#

Adds the text and a line break.

Clear()
#
Bytes() : u8[]
#

The buffer itself (its first Length bytes are the text; shared, not copied).

override ToString() : str
#

override

Private and internal members (4)

u8[] buffer
#

private

i32 length
#

private

Reserve(i32 more)
#

private

Grow(i32 more)
#

private

Inherited members (4)

Source

StringBuilder as Antril, decompiled from its Larva HIR.

System.StringBuilder.ant
// System.StringBuilder — System.Core (decompiled from Larva HIR)

into namespace System
{
	public final class StringBuilder
	{
		u8[] buffer;
		i32 length;

		Reserve(i32 more)
		{
			if (length + more > buffer.Length)
				Grow(more);
		}

		[NoInline]
		Grow(i32 more)
		{
			i32 capacity = buffer.Length * 2;
			if (capacity < length + more)
				capacity = length + more;
			u8[] bigger = new u8[capacity];
			loop (i32 i = 0..length)
				bigger[i] = buffer[i];
			buffer = bigger;
		}

		public:
			new()
			{
				buffer = new u8[16];
			}

			new(i32 capacity)
			{
				buffer = new u8[capacity < 1 ? 1 : capacity];
			}

			/// Adds the text.
			Append(str text) : StringBuilder
			{
				i32 count = text.Length;
				Reserve(count);
				text.CopyTo(0, buffer, length, count);
				length += count;
				return this;
			}

			/// Adds `count` bytes of the text from `start`.
			Append(str text, i32 start, i32 count) : StringBuilder
			{
				Reserve(count < 0 ? 0 : count);
				text.CopyTo(start, buffer, length, count);
				length += count;
				return this;
			}

			/// Adds one character (a UTF-16 code unit, as character literals are), in UTF-8.
			Append(u16 unit) : StringBuilder
			{
				if (unit < 128)
				{
					if (length == buffer.Length)
						Reserve(1);
					buffer[length] = (u8)unit;
					length++;
					return this;
				}
				return AppendCodePoint(unit);
			}

			/// Adds one code point in UTF-8.
			AppendCodePoint(u32 point) : StringBuilder
			{
				Reserve(4);
				if (point < 128)
				{
					buffer[length] = (u8)point;
					length++;
				}
				else if (point < 2048)
				{
					buffer[length] = (u8)(192 | point >> 6);
					buffer[length + 1] = (u8)(128 | point & 63);
					length += 2;
				}
				else if (point < 65536)
				{
					u32 c = point >= 55296 && point <= 57343 ? 65533 : point;
					buffer[length] = (u8)(224 | c >> 12);
					buffer[length + 1] = (u8)(128 | c >> 6 & 63);
					buffer[length + 2] = (u8)(128 | c & 63);
					length += 3;
				}
				else
				{
					buffer[length] = (u8)(240 | point >> 18);
					buffer[length + 1] = (u8)(128 | point >> 12 & 63);
					buffer[length + 2] = (u8)(128 | point >> 6 & 63);
					buffer[length + 3] = (u8)(128 | point & 63);
					length += 4;
				}
				return this;
			}

			/// Adds the number as text (written straight into the buffer).
			Append(i64 value) : StringBuilder
			{
				u64 magnitude = value < 0 ? (u64)-(value + 1) + 1 : (u64)value;
				i32 digits = Number.DigitCount(magnitude);
				i32 sign = value < 0 ? 1 : 0;
				Reserve(digits + sign);
				if (sign == 1)
					buffer[length] = 45;
				length += digits + sign;
				Number.WriteDigits(magnitude, buffer, length);
				return this;
			}

			/// Adds the text and a line break.
			AppendLine(str text) : StringBuilder
			{
				Append(text);
				return Append(10);
			}

			Clear()
			{
				length = 0;
			}

			/// The buffer itself (its first Length bytes are the text; shared, not copied).
			Bytes() : u8[] => buffer;

			override ToString() : str => new str(buffer, 0, length);

			/// How many bytes it holds.
			i32 Length
			{
				get => length;
			}
	}
}
Esc