Skip to content
David Arno edited this page Feb 17, 2020 · 2 revisions

Option Parsers

Succinc<T>'s alternative to TryParse


Introduction to the option parsers

Succinc<T> offers an alternative to the standard bool/out-style TryParse methods offered by various primitive types, by returning an Option<T>, rather than a boolean.

Each parser returns an Option<T>, of None if the parse fails and of the Some<T> if it does. All of them are implemented as extension methods to the string type.

Boolean parser

Signature

public static Option<bool> TryParseBoolean(this string source)

Usage

var str1 = "true";
var str2 = "xxxx";
var possibleBool1 = str1.TryParseBoolean();
var possibleBool2 = str2.TryParseBoolean();

possibleBool1.HasValue // true
possibleBool1.Value    // true
possibleBool2.HasValue // false

Enum parsers

Signatures

public static Option<T> TryParseEnum<T>(this string source)
public static Option<T> TryParseEnumIgnoringCase<T>(this string source)

Usage

enum Colors { Red, Green, Blue }
var str1 = "Red";
var str2 = "green";
var str3 = "Yellow";
var possibleEnum1 = str1.TryParseEnum();
var possibleEnum2 = str2.TryParseEnumIgnoringCase();
var possibleEnum3 = str3.TryParseEnum();

possibleEnum1.HasValue // true
possibleEnum1.Value    // Red
possibleEnum2.HasValue // true
possibleEnum2.Value    // Green
possibleEnum3.HasValue // false

NOTE. Prior to v4 of Succinc<T>, the above two methods are generically constrained using where T : struct, IConvertible and, at runtime, any call to TryParseEnum or TryParseEnumIgnoringCase that doesn't use an enum would result in an ArgumentException being thrown. As of v4, the methods are now generically constrained using where T : struct, enum, so this shortcoming goes away.

Floating point number parsers

Signatures

public static Option<double> TryParseDouble(this string source)
public static Option<float> TryParseFloat(this string source)

Usage

var possibleFloat1 = "1.1".TryParseFloat();
var possibleFloat2 = "1.1.1".TryParseFloat();
var possibleDouble1 = "5.0E-324".TryParseDouble();
var possibleDouble2 = "8.0E400".TryParseDouble();

possibleFloat1.HasValue // true
possibleFloat1.Value    // 1.1
possibleFloat2.HasValue // false
possibleDouble1.HasValue // true
possibleDouble1.Value    // 5.0E-324
possibleDouble2.HasValue // false

Integer parsers

Signatures

public static Option<sbyte> TryParseSignedByte(this string source)
public static Option<byte> TryParseUnsignedByte(this string source)
public static Option<short> TryParseShort(this string source)
public static Option<ushort> TryParseUnsignedShort(this string source)
public static Option<int> TryParseInt(this string source)
public static Option<uint> TryParseUnsignedInt(this string source)
public static Option<long> TryParseLong(this string source)
public static Option<ulong> TryParseUnsignedLong(this string source)

Usage

var possibleSbyte1 = str1.TryParseSignedByte(127);
var possibleSbyte2 = str2.TryParseSignedByte(128);

possibleBool1.HasValue // true
possibleBool1.Value    // 127
possibleBool2.HasValue // false

var possibleByte1 = str1.TryParseUnsignedByte(255);
var possibleByte2 = str2.TryParseUnsignedByte(-1);

possibleBool1.HasValue // true
possibleBool1.Value    // 255
possibleBool2.HasValue // false
...

Not all parsers are shown above for brevity. Each operates as shown above though: returning the value if a valid integer within the range of the type specified, and None otherwise.

Clone this wiki locally