Utf8TomlReader Struct

  • Obsolete: Types with embedded references are not supported in this version of your compiler.

A high-performance, forward-only reader of TOML documents encoded in UTF-8.

[Obsolete("Types with embedded references are not supported in this version of your compiler.", true)]
public ref struct Utf8TomlReader

Remarks

The reader validates the syntax of the document (TOML 1.1, which accepts every TOML 1.0 document) and reads it one token at a time, without allocating. It does not check the rules that need the whole document, such as duplicate keys or tables defined twice: TomlDocument checks them.

A dotted key is read as one Utf8TomlTokenType.PropertyName token per segment. A table header is read as a Utf8TomlTokenType.TableHeader or Utf8TomlTokenType.ArrayOfTablesHeader token followed by the segments of its key. The last segment of a key has Utf8TomlTokenFlags.LastKeySegment.

Constructors

NameDescription
Utf8TomlReader(ReadOnlySpan<byte>, Utf8TomlReaderOptions)Initializes a new reader over a complete UTF-8 TOML document.
Utf8TomlReader(ReadOnlySpan<byte>, bool, Utf8TomlReaderState)Initializes a new reader over a part of a UTF-8 TOML document.

Utf8TomlReader overloads

Utf8TomlReader(ReadOnlySpan<byte>, Utf8TomlReaderOptions)

Utf8TomlReader.cs

Initializes a new reader over a complete UTF-8 TOML document.

public Utf8TomlReader(ReadOnlySpan<byte> utf8Toml, Utf8TomlReaderOptions options = default)
Parameters
utf8Toml ReadOnlySpan<byte>
The UTF-8 bytes of the document. A UTF-8 byte order mark at the start is skipped.
options Utf8TomlReaderOptions
The options of the reader.

Utf8TomlReader(ReadOnlySpan<byte>, bool, Utf8TomlReaderState)

Utf8TomlReader.cs

Initializes a new reader over a part of a UTF-8 TOML document.

public Utf8TomlReader(ReadOnlySpan<byte> utf8Toml, bool isFinalBlock, Utf8TomlReaderState state)
Parameters
utf8Toml ReadOnlySpan<byte>
The UTF-8 bytes to read.
isFinalBlock bool
true when utf8Toml holds the end of the document.
state Utf8TomlReaderState
The state returned by Utf8TomlReader.CurrentState of the previous reader, or a new state for the first block.

Properties

NameDescription
BufferOffsetGets the offset in the whole document of the first byte of the input, when reading in several blocks.
BytesConsumedGets the number of bytes consumed: the input up to the end of the last token.
CurrentDepthGets the number of arrays and inline tables that contain the current token.
CurrentStateGets the state to continue reading the document in a new reader.
IsFinalBlockGets a value indicating whether the input holds the end of the document.
IsLastKeySegmentGets a value indicating whether the current key segment is the last one of its key.
LineNumberGets the 0-based line of the position reached by the reader.
OptionsGets the options of the reader.
TokenFlagsGets how the last token is written.
TokenSpanGets the bytes of the last token, including the delimiters of a string or a quoted key.
TokenStartIndexGets the index in the input of the first byte of the last token.
TokenTypeGets the type of the last token read.
ValueIsEscapedGets a value indicating whether Utf8TomlReader.ValueSpan contains escape sequences that Utf8TomlReader.GetString decodes.
ValueSpanGets the bytes of the value of the last token: the content of a string or quoted key without its delimiters, the text of a number, a date or a boolean, or the text of a comment after its #.

BufferOffset

Utf8TomlReader.cs

Gets the offset in the whole document of the first byte of the input, when reading in several blocks.

public readonly long BufferOffset { get; }

Property value

long

BytesConsumed

Utf8TomlReader.cs

Gets the number of bytes consumed: the input up to the end of the last token.

public readonly long BytesConsumed { get; }

Property value

long

CurrentDepth

Utf8TomlReader.cs

Gets the number of arrays and inline tables that contain the current token.

public readonly int CurrentDepth { get; }

Property value

int

CurrentState

Utf8TomlReader.cs

Gets the state to continue reading the document in a new reader.

public readonly Utf8TomlReaderState CurrentState { get; }

Property value

Utf8TomlReaderState

IsFinalBlock

Utf8TomlReader.cs

Gets a value indicating whether the input holds the end of the document.

public readonly bool IsFinalBlock { get; }

Property value

bool

IsLastKeySegment

Utf8TomlReader.cs

Gets a value indicating whether the current key segment is the last one of its key.

public readonly bool IsLastKeySegment { get; }

Property value

bool

Gets the 0-based line of the position reached by the reader.

public readonly int LineNumber { get; }

Property value

int

Gets the options of the reader.

public readonly Utf8TomlReaderOptions Options { get; }

Property value

Utf8TomlReaderOptions

Gets how the last token is written.

public readonly Utf8TomlTokenFlags TokenFlags { get; }

Property value

Utf8TomlTokenFlags

Gets the bytes of the last token, including the delimiters of a string or a quoted key.

public readonly ReadOnlySpan<byte> TokenSpan { get; }

Property value

ReadOnlySpan<byte>

TokenStartIndex

Utf8TomlReader.cs

Gets the index in the input of the first byte of the last token.

public readonly int TokenStartIndex { get; }

Property value

int

Gets the type of the last token read.

public readonly Utf8TomlTokenType TokenType { get; }

Property value

Utf8TomlTokenType

ValueIsEscaped

Utf8TomlReader.cs

Gets a value indicating whether Utf8TomlReader.ValueSpan contains escape sequences that Utf8TomlReader.GetString decodes.

public readonly bool ValueIsEscaped { get; }

Property value

bool

Gets the bytes of the value of the last token: the content of a string or quoted key without its delimiters, the text of a number, a date or a boolean, or the text of a comment after its #.

public readonly ReadOnlySpan<byte> ValueSpan { get; }

Property value

ReadOnlySpan<byte>

Methods

NameDescription
CopyString(Span<byte>)Copies the decoded UTF-8 value of the current string, key segment or comment to utf8Destination.
CopyString(Span<char>)Copies the decoded value of the current string, key segment or comment to destination.
GetBoolean()Gets the value of the current boolean.
GetDouble()Gets the value of the current float, or of the current integer converted to a Double.
GetInt64()Gets the value of the current integer.
GetString()Gets the decoded value of the current string, key segment or comment.
GetTomlDateTime()Gets the value of the current date or time.
Read()Reads the next token.
Skip()Skips the children of the current token: the value of a key, or the content of an array or an inline table.
TryGetDateOnly(DateOnly)Gets the value of the current local date.
TryGetDateTimeOffset(DateTimeOffset)Gets the value of the current offset date-time, local date-time or local date as a DateTimeOffset.
TryGetDecimal(decimal)Gets the value of the current number as a Decimal, parsed from its text to keep its digits.
TryGetDouble(double)Gets the value of the current float, or of the current integer converted to a Double.
TryGetInt64(long)Gets the value of the current integer.
TryGetTimeOnly(TimeOnly)Gets the value of the current local time.
TryGetTomlDateTime(TomlDateTime)Gets the value of the current date or time.
TrySkip()Skips the children of the current token, when the input contains all of them.
ValueTextEquals(ReadOnlySpan<byte>)Compares the decoded value of the current string or key segment to UTF-8 text, without allocating.
ValueTextEquals(ReadOnlySpan<char>)Compares the decoded value of the current string or key segment to text, without allocating.
ValueTextEquals(string?)Compares the decoded value of the current string or key segment to text, without allocating.

CopyString overloads

CopyString(Span<byte>)

Utf8TomlReader.Values.cs

Copies the decoded UTF-8 value of the current string, key segment or comment to utf8Destination.

public readonly int CopyString(Span<byte> utf8Destination)
Parameters
utf8Destination Span<byte>
Returns

int The number of bytes written.

Remarks

The value is never longer than Utf8TomlReader.ValueSpan.

CopyString(Span<char>)

Utf8TomlReader.Values.cs

Copies the decoded value of the current string, key segment or comment to destination.

public readonly int CopyString(Span<char> destination)
Parameters
destination Span<char>
Returns

int The number of characters written.

Remarks

The value has at most as many characters as Utf8TomlReader.ValueSpan has bytes.

Gets the value of the current boolean.

public readonly bool GetBoolean()

Returns

bool

Exceptions

InvalidOperationException
The current token is not a boolean.

Gets the value of the current float, or of the current integer converted to a Double.

public readonly double GetDouble()

Returns

double

Exceptions

InvalidOperationException
The current token is not a number.

Gets the value of the current integer.

public readonly long GetInt64()

Returns

long

Exceptions

InvalidOperationException
The current token is not an integer.

Gets the decoded value of the current string, key segment or comment.

public readonly string GetString()

Returns

string

Exceptions

InvalidOperationException
The current token is not a string, a key segment or a comment.

GetTomlDateTime()

Utf8TomlReader.Values.cs

Gets the value of the current date or time.

public readonly TomlDateTime GetTomlDateTime()

Returns

TomlDateTime

Exceptions

InvalidOperationException
The current token is not a date or a time.

Reads the next token.

public bool Read()

Returns

bool true when a token was read; false at the end of the document, or when the input of a non-final block has no complete token left.

Exceptions

TomlException
The document is not valid TOML.

Skips the children of the current token: the value of a key, or the content of an array or an inline table.

public void Skip()

Exceptions

InvalidOperationException
The input is not the final block. Use Utf8TomlReader.TrySkip.

TryGetDateOnly(DateOnly)

Utf8TomlReader.Values.cs

Gets the value of the current local date.

public readonly bool TryGetDateOnly(out DateOnly value)

Parameters

value DateOnly

Returns

bool

TryGetDateTimeOffset(DateTimeOffset)

Utf8TomlReader.Values.cs

Gets the value of the current offset date-time, local date-time or local date as a DateTimeOffset.

public readonly bool TryGetDateTimeOffset(out DateTimeOffset value)

Parameters

value DateTimeOffset

Returns

bool

TryGetDecimal(decimal)

Utf8TomlReader.Values.cs

Gets the value of the current number as a Decimal, parsed from its text to keep its digits.

public readonly bool TryGetDecimal(out decimal value)

Parameters

value decimal

Returns

bool

TryGetDouble(double)

Utf8TomlReader.Values.cs

Gets the value of the current float, or of the current integer converted to a Double.

public readonly bool TryGetDouble(out double value)

Parameters

value double

Returns

bool

TryGetInt64(long)

Utf8TomlReader.Values.cs

Gets the value of the current integer.

public readonly bool TryGetInt64(out long value)

Parameters

value long

Returns

bool

TryGetTimeOnly(TimeOnly)

Utf8TomlReader.Values.cs

Gets the value of the current local time.

public readonly bool TryGetTimeOnly(out TimeOnly value)

Parameters

value TimeOnly

Returns

bool

TryGetTomlDateTime(TomlDateTime)

Utf8TomlReader.Values.cs

Gets the value of the current date or time.

public readonly bool TryGetTomlDateTime(out TomlDateTime value)

Parameters

value TomlDateTime

Returns

bool

Skips the children of the current token, when the input contains all of them.

public bool TrySkip()

Returns

bool true when the children were skipped; otherwise false, and the reader is unchanged.

ValueTextEquals overloads

ValueTextEquals(ReadOnlySpan<byte>)

Utf8TomlReader.Values.cs

Compares the decoded value of the current string or key segment to UTF-8 text, without allocating.

public readonly bool ValueTextEquals(ReadOnlySpan<byte> utf8Text)
Parameters
utf8Text ReadOnlySpan<byte>
Returns

bool

ValueTextEquals(ReadOnlySpan<char>)

Utf8TomlReader.Values.cs

Compares the decoded value of the current string or key segment to text, without allocating.

public readonly bool ValueTextEquals(ReadOnlySpan<char> text)
Parameters
text ReadOnlySpan<char>
Returns

bool

ValueTextEquals(string?)

Utf8TomlReader.Values.cs

Compares the decoded value of the current string or key segment to text, without allocating.

public readonly bool ValueTextEquals(string? text)
Parameters
text string?
Returns

bool