-
-
Notifications
You must be signed in to change notification settings - Fork 3
Default Parsers
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.
- Enums, including
[Flags]
string
Version
bool
char
DateTime
DateTimeOffset
byte
sbyte
short
ushort
int
uint
long
ulong
float
double
decimal
Guid
TimeSpan
Index
Range
Uri
nint
nuint
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.
For many of the built-in types, there are additional decisions that must be made to specify how they're parsed unambiguously.
Enums values are parsed as exact matches, but case insensitive.
In particular, the integer versions of an enum
will not parse successfully.
[Flags]
enums must have each flag separated by a comma and a space.
DateTime
and DateTimeOffset
are parsed in the invariant culture, with DateTimeStyles.None
All signed integer types (sbyte
, short
, int
, and long
) are parsed in the invariant culture, with NumberStyles.AllowLeadingSign
.
All unsigned integer types (byte
, ushort
, uint
, and ulong
) are parsed in the invariant culture, with NumberStyles.None
.
All floating point types (float
, double
, and decimal
) are parsed in the invariant culture, with NumberStyles.AllowLeadingSign
and NumberStyles.AllowDecimalPoint
.
Guid
can be in any of the N, D, B, P, or X formats.
TimeSpan
is parsed using TimeSpan.TryParse
, which accepts any string in the format [ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
.
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
is parsed by first parsing the value as a string, and then calling Uri.TryCreate
with that string and UriKind.RelativeOrAbsolute
.
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.
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.