FastStringBuilder

Namespace: SnowBank.Buffers.Text · struct

Implements: ISpanBufferWriter<char>, IDisposable

Span-based implementation of a StringBuilder, with reduced safety checks

Remarks

All formatting uses the invariant culture.

Constructors

FastStringBuilder

FastStringBuilder(Span<char> initialBuffer)

Constructs a builder using a pre-allocated buffer

  • initialBuffer — Pre-allocated buffer (usually on the stack)

FastStringBuilder(int initialCapacity)

Constructs a builder that will rent a buffer with the specified initial capacity

  • initialCapacity — Initial minimum capacity for the buffer

Properties

Capacity

int Capacity { get; }

Gets the maximum number of characters that can be contained in the memory allocated by the current instance.

Item

char Item { get; }

Length

int Length { get; set; }

Gets or sets the length of the current StringBuilder object.

Returns: The length of this instance.

RawChars

Span<char> RawChars { get; }

Returns the underlying storage of the builder.

Methods

Advance

void Advance(int count)

Advances the cursor by the specified number of characters, after a call to GetSpan

  • count — Number of characters effectively written to the buffer

Append

void Append(char c)

Appends a character at the end of the buffer

void Append(string s)

Appends a string at the end of the buffer

void Append(ref InvariantInterpolatedStringHandler handler)

Appends an interpolated string at the end of the buffer

void Append(ReadOnlySpan<char> value)

Appends a span of characters at the end of the buffer

void Append(bool value)

Appends a boolean literal ("true" or "false") to the end of the buffer

void Append(int value)

Appends a base 10 integer to the end of the buffer

void Append(uint value)

Appends a base 10 integer to the end of the buffer

void Append(long value)

Appends a base 10 integer to the end of the buffer

void Append(ulong value)

Appends a base 10 integer to the end of the buffer

void Append(double value)

Appends a base 10 floating point number to the end of the buffer

void Append(float value)

Appends a base 10 floating point number to the end of the buffer

void Append(decimal value)

Appends a base 10 floating point number to the end of the buffer

void Append<T>(T value)

Appends a formattable value to the end of the buffer

void Append(IFormatProvider provider, ref DefaultInterpolatedStringHandler handler)

Appends an interpolated string at the end of the buffer

void Append(char c, int count)

Appends a repeated character at the end of the buffer

void Append(Char* value, int length)

Appends the contents of an unmanaged string at the end of the buffer

void Append<T>(T value, string format)

Appends a formattable value to the end of the buffer

void Append(int value, string format, IFormatProvider provider = null)

Appends a base 10 integer to the end of the buffer

void Append(uint value, string format, IFormatProvider provider = null)

Appends a base 10 integer to the end of the buffer

void Append(long value, string format, IFormatProvider provider = null)

Appends a base 10 integer to the end of the buffer

void Append(ulong value, string format, IFormatProvider provider = null)

Appends a base 10 integer to the end of the buffer

void Append(double value, string format, IFormatProvider provider = null)

Appends a base 10 decimal number to the end of the buffer

void Append(float value, string format, IFormatProvider provider = null)

Appends a base 10 decimal number to the end of the buffer

void Append<T>(T value, string format, IFormatProvider provider)

Appends a formattable value to the end of the buffer

AppendFormat

void AppendFormat(string format, ReadOnlySpan<object> args)

Appends a formatting string at the end of the buffer

AppendFormatted

void AppendFormatted<TFormattable>(TFormattable value)

Appends a value that implements IFormattable or ISpanFormattable at the end of the buffer

void AppendFormatted<TFormattable>(TFormattable value, string format, IFormatProvider provider = null)

Appends a value that implements IFormattable or ISpanFormattable at the end of the buffer

AppendHex

void AppendHex(int value, int digits, bool lowerCase = false)

Appends a base 16 integer, using a fixed number of digits, at the end of the buffer

  • value — Value to encode (in hexadecimal)
  • digits — Number of digits to use.
  • lowerCase — Set to true if you want to use lowercase digits.

If digits is less than 8, then the number may only be partially written to the buffer! Ex: AppendHex(0x1234, 2) will only write "34" to the buffer.

void AppendHex(long value, int digits, bool lowerCase = false)

Appends a base 16 integer, using a fixed number of digits, at the end of the buffer

  • value — Value to encode (in hexadecimal)
  • digits — Number of digits to use.
  • lowerCase — Set to true if you want to use lowercase digits.

If digits is less than 16, then the number may only be partially written to the buffer! Ex: AppendHex(0x1234, 2) will only write "34" to the buffer.

AppendLine

void AppendLine()

Appends an empty line at the end of the buffer

This method uses CRLF (\r\n) as the new line, independent of the current operating system.

void AppendLine(ref InvariantInterpolatedStringHandler handler)

Appends an interpolated string, followed by a line return, at the end of the buffer

This method uses CRLF (\r\n) as the new line, independent of the current operating system.

void AppendLine(string s)

Appends a string, followed by a line return, at the end of the buffer

This method uses CRLF (\r\n) as the new line, independent of the current operating system.

void AppendLine(ReadOnlySpan<char> s)

Appends a string, followed by a line return, at the end of the buffer

This method uses CRLF (\r\n) as the new line, independent of the current operating system.

void AppendLine(IFormatProvider provider, ref DefaultInterpolatedStringHandler handler)

Appends an interpolated string, followed by a line return, at the end of the buffer

This method uses CRLF (\r\n) as the new line, independent of the current operating system.

AsSpan

ReadOnlySpan<char> AsSpan()

Returns a span around the contents of the builder.

ReadOnlySpan<char> AsSpan(bool terminate)

Returns a span around the contents of the builder.

  • terminate — Ensures that the builder has a null char after Length

ReadOnlySpan<char> AsSpan(int start)

Returns a span around the contents of the builder, starting at the specified offset.

ReadOnlySpan<char> AsSpan(int start, int length)

Returns a span around parts of the contents of the builder.

Dispose

void Dispose()

Releases any rented buffer to the pool

EnsureCapacity

void EnsureCapacity(int capacity)

Gets a pinnable reference to the builder.

Does not ensure there is a null char after Length.

This overload is pattern matched in the C# 7.3+ compiler so you can omit the explicit method call, and write e.g. fixed (char* c = builder)

GetPinnableReference

char GetPinnableReference()

Gets a pinnable reference to the first character in the buffer.

char GetPinnableReference(bool terminate)

Gets a pinnable reference to the first character in the buffer.

  • terminate — Ensures that the builder has a null char after Length

GetSpan

Span<char> GetSpan(int length)

Allocates a new chunk of characters at the end of the buffer

  • length — Number of characters to allocate

Returns: Span that points to the newly allocated chunk

This method may resize the buffer, but will not advance the cursor. You must call after filling the buffer, to effectively "commit" the new content.

Insert

void Insert(int index, string s)

Inserts a string at the specified location

void Insert(int index, ReadOnlySpan<char> s)

Inserts a string at the specified location

void Insert(int index, char value, int count)

Inserts a character at the specified location

ToString

string ToString()

Returns a string with all the characters that have been written to this builder

The buffer is reset after this call, meaning that any rented buffer will be returned to the pool.

TryCopyTo

bool TryCopyTo(Span<char> destination, out int charsWritten)

Copies the content of this builder, if the destination is large enough