GitXplorerGitXplorer
a

TryParsers

public
10 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
e10f2ecbe28551f4e4f1ddb1ed0e7a4e0068a5c5

Update README wrt to C# 7 out variables

aatifaziz committed 7 years ago
Unverified
d4c9de0a62eaf0b3a2cd932336816380ebab5f4f

Replace .NET Framework mentions with .NET Standard

aatifaziz committed 7 years ago
Unverified
ed118ad7ccfb30f982d4896c8b146141ba50e51f

README title + intro

aatifaziz committed 7 years ago
Unverified
9dbcca04156e78bdf2991acba7cfdef940d565ac

Wrap paragraphs in README

aatifaziz committed 7 years ago
Unverified
906f606e47d3090c9162c464906e898de7d9c4cd

Use named footnote links in README

aatifaziz committed 7 years ago
Unverified
7edc29522b62ab7aaf4e396a696259ad2574eee3

Use fenced code blocks in README

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: