GitXplorerGitXplorer
a

TryParsers

public
10 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
4589c6731af543ece6e1a2f7c980f96fea62cc51

Package tags should be semi-colon delimited

aatifaziz committed 7 years ago
Unverified
8fcb187cb69cc2d05a1e0d171666a1a4fe18ad7f

Standardise packaging options

aatifaziz committed 7 years ago
Unverified
d4312fc7f0d743a8325b95a9203803eace55f239

Use C# 7 for less syntactic ceremony

aatifaziz committed 7 years ago
Unverified
227024079f8e8a62e4be0d56a733ba49d4597ec1

Add CI configs

aatifaziz committed 7 years ago
Unverified
bda4a0ea24d2d9f7474335e7b3266f66dc0f78f9

Delete trailing spaces

aatifaziz committed 7 years ago
Unverified
2c3647fdcc8a1dccc42bdcb248953b5d64b2455c

Remove license header for assembly attributes

aatifaziz committed 7 years ago

README

The README file for this repository.

TryParsers

TryParse is a .NET Standard library that provides methods for attempting parsing of basic values without the need for output parameters, as most TryParse method do in .NET.

It is available as two NuGet packages:

The .NET Standard has a number of static TryParse methods on types like Int32, Int64, Double, DateTime and so on, that make their use in LINQ query expressions impossible. That's because they return their result in two parts: one in the return value (a Boolean indicating whether the parse was successful) and another in an output parameter (the parsed value).

TryParsers is a tiny library, which can also be embedded as one or two C# files, that turns TryParse methods into functions that return their result entirely in the return value. For example, for an Int32, TryParsers's version returns a nullable Int32 or simply int? in C#. If the parsing succeeds, the return value will be the parsed Int32 value otherwise it will be null.

You can now write error-tolerant code using LINQ like this:

var nums =
    from input in new[] { "O", "l", "2", "3", "4", "S", "6", "7", "B", "9" }
    select TryParse.Int32(input) into num
    where num != null
    select num.Value;

Console.WriteLine(string.Join(",", nums);

// Output: 2,3,4,6,7,9

Note that while out variables that were introduced in C# 7 help to use standard TryParse methods in most expressions, they are forbidden in LINQ query expressions (at least at the time of the release of this library).

The .NET Standard still has many types that support parsing or initializing from a string representation of a value but which throw exceptions on failure. TryParsers also comes with a generic method called Make for such cases that enables building a TryParse-like method on top of existing parsers. For example, the MailAddress constructor throws FormatException when it fails to parse or recognize a valid e-mail address. Using Make, however, you can create a TryParse-like method on top of MailAddress:

var addresses =
    from parser in new[] {
        TryParse.Make(s => new MailAddress(s), (FormatException e) => null)
    }
    from s in new[] {
        "john.doe@example.com",
        "john-dot-doe-at-example-dot-com",
        "john.doe@localhost",
    }
    select parser(s) into address
    where address != null
    select address;

Console.WriteLine(string.Join("; ", addresses));

// Output:
// john.doe@example.com; john.doe@localhost

Note that Make above will still throw if an exception other than FormatException is thrown by the MailAddress constructor. Below is a more advanced of example using Make to distinguish between FormatException and OverflowException (throwing in all other cases):

var results =
    from parser in new[] {
        TryParse.Make(s => (object) sbyte.Parse(s, NumberStyles.None),
                        (FormatException e)   => e,
                        (OverflowException e) => e)
    }
    from s in new[] {  "l",  "2",   "4",  "B",    "16",
                      "32", "64", "128", "256", "-512"  }
    select parser(s) into e
    select e is OverflowException
         ? "#OVERFLOW"
         : e is FormatException
         ? "#ERROR"
         : e;

Console.WriteLine(string.Join(",", results));

// Output:
// #ERROR,2,4,#ERROR,16,32,64,#OVERFLOW,#OVERFLOW,#ERROR

Download and install TryParsers from NuGet: