SpanDecoderExtensions

Namespace: System · class

Helper methods to help decode the content of spans

Methods

GetStringOrCopy

static string GetStringOrCopy(ReadOnlyMemory<char> memory)

Returns the underlying string from a System.ReadOnlyMemory<Char> if it spans the entire string, or a copy if it is smaller.

  • memory — Read-only memory containing a block of characters.

Returns: Either the original string instance if the literal spans the entire string; otherwise, a newly allocated string.

This method has some overhead and should only be used if there is a high probability that the segment is usually exposing the entire string.

If not, this will always end up allocating a new string anyway, and will be slower than simply calling literal.ToString()

.

PrettyPrint

static string PrettyPrint(ReadOnlySpan<byte> span)

Helper method that dumps the span as a string (if it contains only printable ascii chars) or a hex array if it contains non-printable chars. It should only be used for logging and troubleshooting !

Returns: Returns either "'abc'", "<00 42 7F>", or "". Returns "''" for the empty span

static string PrettyPrint(Span<byte> span)

Helper method that dumps the span as a string (if it contains only printable ascii chars) or a hex array if it contains non-printable chars. It should only be used for logging and troubleshooting !

Returns: Returns either "'abc'", "<00 42 7F>", or "". Returns "''" for the empty span

static string PrettyPrint(ReadOnlySpan<byte> span, int maxLen)

Formats the span as a human-friendly string (if it contains only printable ascii chars) or a hex array if it contains non-printable chars. It should only be used for logging and troubleshooting !

  • span — Span to format
  • maxLen — Truncate the span if it exceeds this size

Returns: Returns either "'abc'", "<00 42 7F>", or "". Returns "''" for the empty span

static string PrettyPrint(Span<byte> span, int maxLen)

Formats the span as a human-friendly string (if it contains only printable ascii chars) or a hex array if it contains non-printable chars. It should only be used for logging and troubleshooting !

  • span — Span to format
  • maxLen — Truncate the span if it exceeds this size

Returns: Returns either "'abc'", "<00 42 7F>", or "". Returns "''" for the empty span

ToBase64

static string ToBase64(ReadOnlySpan<byte> span)

Converts a span using Base64 encoding

static string ToBase64(Span<byte> span)

Converts a span using Base64 encoding

ToBool

static bool ToBool(ReadOnlySpan<byte> span)

Converts a span into a boolean.

Returns: False if the span is empty, or is equal to the byte 0; otherwise, true.

static bool ToBool(Span<byte> span)

Converts a span into a boolean.

Returns: False if the span is empty, or is equal to the byte 0; otherwise, true.

ToByte

static byte ToByte(ReadOnlySpan<byte> span)

Converts a span into a byte

Returns: Value of the first and only byte of the span, or 0 if the span is empty.

static byte ToByte(Span<byte> span)

Converts a span into a byte

Returns: Value of the first and only byte of the span, or 0 if the span is empty.

ToByteString

static string ToByteString(ReadOnlySpan<byte> span)

Stringifies a span containing only ASCII chars

Returns: ASCII string, or string.Empty if the span is empty

static string ToByteString(Span<byte> span)

Stringifies a span containing only ASCII chars

Returns: ASCII string, or string.Empty if the span is empty

ToDecimal

static decimal ToDecimal(ReadOnlySpan<byte> span)

Converts a span into a 128-bit IEEE floating point.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

static decimal ToDecimal(Span<byte> span)

Converts a span into a 128-bit IEEE floating point.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

ToDouble

static double ToDouble(ReadOnlySpan<byte> span)

Converts a span into a 64-bit IEEE floating point.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

static double ToDouble(Span<byte> span)

Converts a span into a 64-bit IEEE floating point.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

ToDoubleBE

static double ToDoubleBE(ReadOnlySpan<byte> span)

Converts a span into a 64-bit IEEE floating point (in network order).

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

static double ToDoubleBE(Span<byte> span)

Converts a span into a 64-bit IEEE floating point (in network order).

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

ToGuid

static Guid ToGuid(ReadOnlySpan<byte> span)

Converts a span into a Guid.

Returns: Native Guid decoded from the span.

The span can either be a 16-byte RFC4122 GUID, or an ASCII string of 36 chars

static Guid ToGuid(Span<byte> span)

Converts a span into a Guid.

Returns: Native Guid decoded from the span.

The span can either be a 16-byte RFC4122 GUID, or an ASCII string of 36 chars

ToHexaString

static string ToHexaString(ReadOnlySpan<byte> span, bool lowerCase = false)

Converts a span into a string with each byte encoded into hexadecimal (lowercase)

  • span — Span to encode
  • lowerCase — If true, produces lowercase hexadecimal (a-f); otherwise, produces uppercase hexadecimal (A-F)

Returns: "0123456789abcdef"

static string ToHexaString(Span<byte> span, bool lowerCase = false)

Converts a span into a string with each byte encoded into hexadecimal (lowercase)

  • span — Span to encode
  • lowerCase — If true, produces lowercase hexadecimal (a-f); otherwise, produces uppercase hexadecimal (A-F)

Returns: "0123456789abcdef"

static string ToHexaString(ReadOnlySpan<byte> span, char sep, bool lowerCase = false)

Converts a span into a string with each byte encoded into hexadecimal (uppercase) separated by a char

  • span — Span to encode
  • sep — Character used to separate the hexadecimal pairs (ex: ' ')
  • lowerCase — If true, produces lowercase hexadecimal (a-f); otherwise, produces uppercase hexadecimal (A-F)

Returns: "01 23 45 67 89 ab cd ef"

static string ToHexaString(Span<byte> span, char sep, bool lowerCase = false)

Converts a span into a string with each byte encoded into hexadecimal (uppercase) separated by a char

  • span — Span to encode
  • sep — Character used to separate the hexadecimal pairs (ex: ' ')
  • lowerCase — If true, produces lowercase hexadecimal (a-f); otherwise, produces uppercase hexadecimal (A-F)

Returns: "01 23 45 67 89 ab cd ef"

ToInt128

static Int128 ToInt128(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, signed 128-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 16 bytes

static Int128 ToInt128(Span<byte> span)

Converts a span into a little-endian encoded, signed 128-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 16 bytes

ToInt128BE

static Int128 ToInt128BE(ReadOnlySpan<byte> span)

Converts a span into a big-endian encoded, signed 128-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 16 bytes

static Int128 ToInt128BE(Span<byte> span)

Converts a span into a big-endian encoded, signed 128-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 16 bytes

ToInt16

static short ToInt16(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, signed 16-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 2 bytes

static short ToInt16(Span<byte> span)

Converts a span into a little-endian encoded, signed 16-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 2 bytes

ToInt16BE

static short ToInt16BE(ReadOnlySpan<byte> span)

Converts a span into a big-endian encoded, signed 16-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 2 bytes

static short ToInt16BE(Span<byte> span)

Converts a span into a big-endian encoded, signed 16-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 2 bytes

ToInt24

static int ToInt24(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, signed 24-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 3 bytes

static int ToInt24(Span<byte> span)

Converts a span into a little-endian encoded, signed 24-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 3 bytes

ToInt24BE

static int ToInt24BE(ReadOnlySpan<byte> span)

Converts a span into a big-endian encoded, signed 24-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 3 bytes

static int ToInt24BE(Span<byte> span)

Converts a span into a big-endian encoded, signed 24-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 3 bytes

ToInt32

static int ToInt32(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, signed 32-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 4 bytes

static int ToInt32(Span<byte> span)

Converts a span into a little-endian encoded, signed 32-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 4 bytes

ToInt32BE

static int ToInt32BE(ReadOnlySpan<byte> span)

Converts a span into a big-endian encoded, signed 32-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 4 bytes

static int ToInt32BE(Span<byte> span)

Converts a span into a big-endian encoded, signed 32-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 4 bytes

ToInt64

static long ToInt64(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, signed 64-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 8 bytes

static long ToInt64(Span<byte> span)

Converts a span into a little-endian encoded, signed 64-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 8 bytes

ToInt64BE

static long ToInt64BE(ReadOnlySpan<byte> span)

Converts a span into a big-endian encoded, signed 64-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 8 bytes

static long ToInt64BE(Span<byte> span)

Converts a span into a big-endian encoded, signed 64-bit integer.

Returns: 0 of the span is empty, a signed integer, or an error if the span has more than 8 bytes

ToSByte

static sbyte ToSByte(ReadOnlySpan<byte> span)

Converts a span into a signed byte (-128...+127)

Returns: Value of the first and only byte of the span, or 0 if the span is empty.

static sbyte ToSByte(Span<byte> span)

Converts a span into a signed byte (-128...+127)

Returns: Value of the first and only byte of the span, or 0 if the span is empty.

ToSingle

static float ToSingle(ReadOnlySpan<byte> span)

Converts a span into a 32-bit IEEE floating point.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

static float ToSingle(Span<byte> span)

Converts a span into a 32-bit IEEE floating point.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

ToSingleBE

static float ToSingleBE(ReadOnlySpan<byte> span)

Converts a span into a 32-bit IEEE floating point (in network order).

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

static float ToSingleBE(Span<byte> span)

Converts a span into a 32-bit IEEE floating point (in network order).

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

ToString

static string ToString(ReadOnlySpan<byte> span, string format)

Converts a span of bytes into a string, using the specified format

static string ToString(ReadOnlySpan<byte> span, string format, IFormatProvider provider)

Converts a span of bytes into a string, using the specified format

ToStringAnsi

static string ToStringAnsi(ReadOnlySpan<byte> span)

Stringifies a span containing characters in the operating system's current ANSI codepage

Returns: Decoded string, or string.Empty if the span is empty

Calling this method on a span that is not ANSI, or was generated with different codepage than the current process, will return a corrupted string! This method should ONLY be used to interop with the Win32 API or unmanaged libraries that require the ANSI codepage! You SHOULD NOT use this to expose data to other systems or locale (via sockets, files, ...) If you are decoding natural text, you should probably change the encoding at the source to be UTF-8! If you are decoding identifiers or keywords that are known to be ASCII only, you should use instead (safe). If these identifiers can contain 'special' bytes (like \xFF or \xFE), you should use instead (unsafe).

static string ToStringAnsi(Span<byte> span)

Stringifies a span containing characters in the operating system's current ANSI codepage

Returns: Decoded string, or string.Empty if the span is empty

Calling this method on a span that is not ANSI, or was generated with different codepage than the current process, will return a corrupted string! This method should ONLY be used to interop with the Win32 API or unmanaged libraries that require the ANSI codepage! You SHOULD NOT use this to expose data to other systems or locale (via sockets, files, ...) If you are decoding natural text, you should probably change the encoding at the source to be UTF-8! If you are decoding identifiers or keywords that are known to be ASCII only, you should use instead (safe). If these identifiers can contain 'special' bytes (like \xFF or \xFE), you should use instead (unsafe).

ToStringAscii

static string ToStringAscii(ReadOnlySpan<byte> span)

Stringifies a span containing 7-bit ASCII characters only

Returns: Decoded string, or string.Empty if the span is empty

This method should ONLY be used to decoded data that is GUARANTEED to be in the range 0..127. This method will THROW if any byte in the span has bit 7 set to 1 (ie: >= 0x80) If you are decoding identifiers or keywords with 'special' bytes (like \xFF or \xFE), you should use instead. If you are decoding natural text, or text from unknown origin, you should use or instead. If you are attempting to decode a string obtain from a Win32 or unmanaged library call, you should use instead.

static string ToStringAscii(Span<byte> span)

Stringifies a span containing 7-bit ASCII characters only

Returns: Decoded string, or string.Empty if the span is empty

This method should ONLY be used to decoded data that is GUARANTEED to be in the range 0..127. This method will THROW if any byte in the span has bit 7 set to 1 (ie: >= 0x80) If you are decoding identifiers or keywords with 'special' bytes (like \xFF or \xFE), you should use instead. If you are decoding natural text, or text from unknown origin, you should use or instead. If you are attempting to decode a string obtain from a Win32 or unmanaged library call, you should use instead.

ToStringUnicode

static string ToStringUnicode(ReadOnlySpan<byte> span)

Stringifies a span containing either 7-bit ASCII, or UTF-8 characters

Returns: Decoded string, or string.Empty if the span is empty. The encoding will be automatically detected

This should only be used for spans encoded using UTF8 or ASCII. This is NOT compatible with spans encoded using ANSI ( encoding) or with a specific encoding or code page. This method will NOT automatically remove the UTF-8 BOM if present (use if you need this)

static string ToStringUnicode(Span<byte> span)

Stringifies a span containing either 7-bit ASCII, or UTF-8 characters

Returns: Decoded string, or string.Empty if the span is empty. The encoding will be automatically detected

This should only be used for spans encoded using UTF8 or ASCII. This is NOT compatible with spans encoded using ANSI ( encoding) or with a specific encoding or code page. This method will NOT automatically remove the UTF-8 BOM if present (use if you need this)

ToStringUtf8

static string ToStringUtf8(ReadOnlySpan<byte> span)

Decodes a span that is known to contain UTF-8 characters, with an optional UTF-8 BOM

Returns: Decoded string, or string.Empty if the span is empty

This method will THROW if the span does not contain valid UTF-8 sequences. This method will remove any UTF-8 BOM if present. If you need to keep the BOM as the first character of the string, use

static string ToStringUtf8(Span<byte> span)

Decodes a span that is known to contain UTF-8 characters, with an optional UTF-8 BOM

Returns: Decoded string, or string.Empty if the span is empty

This method will THROW if the span does not contain valid UTF-8 sequences. This method will remove any UTF-8 BOM if present. If you need to keep the BOM as the first character of the string, use

ToUInt128

static UInt128 ToUInt128(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 128-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 16 bytes

static UInt128 ToUInt128(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 128-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 16 bytes

ToUInt128BE

static UInt128 ToUInt128BE(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 128-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 16 bytes

static UInt128 ToUInt128BE(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 128-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 16 bytes

ToUInt16

static ushort ToUInt16(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 16-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 2 bytes

static ushort ToUInt16(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 16-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 2 bytes

ToUInt16BE

static ushort ToUInt16BE(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 16-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 2 bytes

static ushort ToUInt16BE(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 16-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 2 bytes

ToUInt24

static uint ToUInt24(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 24-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 3 bytes

static uint ToUInt24(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 24-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 3 bytes

ToUInt24BE

static uint ToUInt24BE(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 24-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 3 bytes

static uint ToUInt24BE(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 24-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 3 bytes

ToUInt32

static uint ToUInt32(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 32-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

static uint ToUInt32(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 32-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

ToUInt32BE

static uint ToUInt32BE(ReadOnlySpan<byte> span)

Converts a span into a big-endian encoded, unsigned 32-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

static uint ToUInt32BE(Span<byte> span)

Converts a span into a big-endian encoded, unsigned 32-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 4 bytes

ToUInt64

static ulong ToUInt64(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 64-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

static ulong ToUInt64(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 64-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

ToUInt64BE

static ulong ToUInt64BE(ReadOnlySpan<byte> span)

Converts a span into a little-endian encoded, unsigned 64-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

static ulong ToUInt64BE(Span<byte> span)

Converts a span into a little-endian encoded, unsigned 64-bit integer.

Returns: 0 of the span is empty, an unsigned integer, or an error if the span has more than 8 bytes

ToUuid128

static Uuid128 ToUuid128(ReadOnlySpan<byte> span)

Converts a span into a 128-bit UUID.

Returns: Uuid decoded from the span.

The span can either be a 16-byte RFC4122 GUID, or an ASCII string of 36 chars

static Uuid128 ToUuid128(Span<byte> span)

Converts a span into a 128-bit UUID.

Returns: Uuid decoded from the span.

The span can either be a 16-byte RFC4122 GUID, or an ASCII string of 36 chars

ToUuid48

static Uuid48 ToUuid48(ReadOnlySpan<byte> span)

Converts a span into a 48-bit UUID.

Returns: Uuid48 decoded from the span.

The span can either be an 6-byte array, or an ASCII string of 12, 13 or 15 chars

static Uuid48 ToUuid48(Span<byte> span)

Converts a span into a 48-bit UUID.

Returns: Uuid48 decoded from the span.

The span can either be an 8-byte array, or an ASCII string of 16, 17 or 19 chars

ToUuid64

static Uuid64 ToUuid64(ReadOnlySpan<byte> span)

Converts a span into a 64-bit UUID.

Returns: Uuid64 decoded from the span.

The span can either be an 8-byte array, or an ASCII string of 16, 17 or 19 chars

static Uuid64 ToUuid64(Span<byte> span)

Converts a span into a 64-bit UUID.

Returns: Uuid64 decoded from the span.

The span can either be an 8-byte array, or an ASCII string of 16, 17 or 19 chars

ToUuid80

static Uuid80 ToUuid80(ReadOnlySpan<byte> span)

Converts a span into an 80-bit UUID.

Returns: Uuid80 decoded from the span.

The span can either be an 10-byte array, or an ASCII string of 20, 22 or 24 chars

static Uuid80 ToUuid80(Span<byte> span)

Converts a span into an 80-bit UUID.

Returns: Uuid80 decoded from the span.

The span can either be an 10-byte array, or an ASCII string of 20, 22 or 24 chars

ToUuid96

static Uuid96 ToUuid96(ReadOnlySpan<byte> span)

Converts a span into a 96-bit UUID.

Returns: Uuid96 decoded from the span.

The span can either be an 12-byte array, or an ASCII string of 24, 26 or 28 chars

static Uuid96 ToUuid96(Span<byte> span)

Converts a span into a 96-bit UUID.

Returns: Uuid96 decoded from the span.

The span can either be an 12-byte array, or an ASCII string of 24, 26 or 28 chars

TryAppendAndAdvance

static bool TryAppendAndAdvance<T>(ref Span<T> buffer, T item)

Copies an item at the start of buffer, and replace the variable with the tail of the buffer

The buffer variable is passed by reference and is modified in-place!

Example: bool TryFormat(Span destination, out int charsWritten) { var buffer = destination; if (!buffer.TryAppendAndAdvance('"')) goto too_small; if (!TryEscapeLiteral(tmp, out int len, this.SomeValue)) goto too_small; buffer = buffer[len..]; if (!buffer.TryAppendAndAdvance('"')) goto too_small; charsWritten = destination.Length - buffer.Length; return true; too_small: charsWritten = 0; return false; }

static bool TryAppendAndAdvance<T>(ref Span<T> buffer, ReadOnlySpan<T> items)

Copies items at the start of buffer, and replace the variable with the tail of the buffer

The buffer variable is passed by reference and is modified in-place!

Example: bool TryFormat(Span destination, out int charsWritten) { var buffer = destination; if (!buffer.TryAppendAndAdvance("[ ")) goto too_small; if (!this.SomeValue.TryFormat(tmp, out int len)) goto too_small; buffer = buffer[len..]; if (!buffer.TryAppendAndAdvance(" ]")) goto too_small; charsWritten = destination.Length - buffer.Length; return true; too_small: charsWritten = 0; return false; }

TryAppendFormattableAndAdvance

static bool TryAppendFormattableAndAdvance<T>(ref Span<char> buffer, in T item, ReadOnlySpan<char> format = null, IFormatProvider provider = null)

Formats a ISpanFormattable value at the start of buffer, and replace the variable with the tail of the buffer

The buffer variable is passed by reference and is modified in-place!

Example: public readonly struct Point2D : ISpanFormattable { public readonly double X; public readonly double Y; // ... public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) { // => "(X, Y)" var buffer = destination; // open the parenthesis if (!buffer.TryAppendAndAdvance('(')) goto too_small; // writes X and advance the buffer (if it is large enough) if (!buffer.TryAppendFormattableAndAdvance(this.X, "R", provider) goto too_small; // write the comma separator if (!buffer.TryAppendAndAdvance(", ")) goto too_small; // writes X and advance the buffer (if it is large enough) if (!buffer.TryAppendFormattableAndAdvance(this.Y, "R", provider) goto too_small; // close the parenthesis if (!buffer.TryAppendAndAdvance(')')) goto too_small; charsWritten = destination.Length - buffer.Length; return true; too_small: charsWritten = 0; return false; } }

TryCopyTo

static bool TryCopyTo<T>(ReadOnlySpan<T> source, Span<T> destination, out int written)

Calls TryCopyTo and, if successful, sets the number of copied items in written

  • source — The span to copy items from
  • destination — The span to copy items into
  • written — Number of items copied, or 0 if destination is too small

Returns: If the destination span is shorter than the source span, this method return false and no data is written to the destination.

This helper method is very useful when implementing ISpanFormattable.

static bool TryCopyTo(ReadOnlySpan<char> source, Span<char> destination, out int written)

Calls TryCopyTo and, if successful, sets the number of copied items in written

  • source — The string to copy from
  • destination — The span to copy items into
  • written — Number of characters copied, or 0 if destination is too small

Returns: If the destination span is shorter than the source span, this method return false and no data is written to the destination.

This helper method is very useful when implementing ISpanFormattable.

TryGetString

static bool TryGetString(ReadOnlyMemory<char> memory, out string text)

Attempt to return the original string, if it is the same size as the segment.

  • memory — Read-only memory containing a block of characters.
  • text — When the method returns true, the original string.

Returns: true if the memory spans the entire string; otherwise, false.