Skip to content

Default Parsers

kevin-montrose edited this page Apr 10, 2021 · 8 revisions

Default Parsers

Introduction

Cesil has instances of Parser pre-made for most built-in .NET types. These are used by the Default Type Describer, and can be accessed separately with Parser.GetDefault(TypeInfo).

Every type that has a default Parser also has a Default Formatter.

Supported Types

For value types, their Nullable equivalent is also supported.

As a consequence of nint and nuint support, the types IntPtr and UIntPtr are also supported - though use under the latter names is discouraged.

Additional Details

For many of the built-in types, there are additional decisions that must be made to specify how they're parsed unambiguously.

Enums

Enums values are parsed as exact matches, but case insensitive.

In particular, the integer versions of an enum will not parse successfully.

[Flags] Enums

[Flags] enums must have each flag separated by a comma and a space.

DateTime And DateTimeOffset

DateTime and DateTimeOffset are parsed in the invariant culture, with DateTimeStyles.None

Signed Integer Types

All signed integer types (sbyte, short, int, and long) are parsed in the invariant culture, with NumberStyles.AllowLeadingSign.

Unsigned Integer Types

All unsigned integer types (byte, ushort, uint, and ulong) are parsed in the invariant culture, with NumberStyles.None.

Floating Point Types

All floating point types (float, double, and decimal) are parsed in the invariant culture, with NumberStyles.AllowLeadingSign and NumberStyles.AllowDecimalPoint.

Guid

Guid can be in any of the N, D, B, P, or X formats.

TimeSpan

TimeSpan is parsed using TimeSpan.TryParse, which accepts any string in the format [ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws].

Index & Range

Index and Range are new types .NET Standard 2.1 / .NET Core 3.0.

Index must be formatted as [^]\d+, and Range as [(index)]..[(index)].

Uri

Uri is parsed by first parsing the value as a string, and then calling Uri.TryCreate with that string and UriKind.RelativeOrAbsolute.

Nullables vs Non-Nullables

If the cell being parsed is empty and the target type is a value type, Parser behavior differs based on whether the target type is nullable.

If nullable, an empty cell parses to null. If non-nullable, an empty cell is an error - and the parser returns false.

Native Integers

The nint and nuint types should be used with care, as the range of valid values differs based on whether code is executing as 32 or 64-bit. Accordingly Cesil may be unable to parse values that were formatted by a process with different bittedness despite have identical declarations in both processes.