-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #745 from polyadic/generic-parse-or-none
Implement Generic ParseOrNone extensions.
- Loading branch information
Showing
6 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Funcky.Test/Extensions/ParseExtensions/ParseExtensionsTest.Generic.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#if GENERIC_PARSEABLE | ||
using System.Globalization; | ||
|
||
namespace Funcky.Test.Extensions.ParseExtensions; | ||
|
||
public sealed partial class ParseExtensionsTest | ||
{ | ||
[Theory] | ||
[MemberData(nameof(ParseableDoubleNumbers))] | ||
public void ParseGenericStringReturnsTheExpectedDouble(Option<double> expected, string input) | ||
{ | ||
Assert.Equal(expected, input.ParseNumberOrNone<double>(NumberStyles.Number, null)); | ||
Assert.Equal(expected, input.ParseOrNone<double>(null)); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ParseableDoubleNumbers))] | ||
public void ParseGenericSpanReturnsTheExpectedDouble(Option<double> expected, string input) | ||
{ | ||
Assert.Equal(expected, input.AsSpan().ParseNumberOrNone<double>(NumberStyles.Number, null)); | ||
Assert.Equal(expected, input.AsSpan().ParseOrNone<double>(null)); | ||
} | ||
|
||
public static TheoryData<Option<double>, string> ParseableDoubleNumbers() | ||
=> new() | ||
{ | ||
{ Option.Some(1.0), "1.0" }, | ||
{ Option.Some(3.145), "3.145" }, | ||
{ Option.Some(0.5), ".5" }, | ||
{ Option.Some(1.0), "1.0" }, | ||
{ Option.Some(42.0), "42" }, | ||
{ Option<double>.None, string.Empty }, | ||
{ Option<double>.None, "no-number" }, | ||
}; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Funcky/Extensions/ParseExtensions/ParseExtensions.GenericNumber.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#if GENERIC_MATH | ||
using System.Globalization; | ||
using System.Numerics; | ||
|
||
namespace Funcky.Extensions; | ||
public static partial class ParseExtensions | ||
{ | ||
public static Option<TNumber> ParseNumberOrNone<TNumber>(this string value, NumberStyles style, IFormatProvider? provider) | ||
where TNumber : INumberBase<TNumber> | ||
=> TNumber.TryParse(value, style, provider, out var result) | ||
? result | ||
: Option<TNumber>.None; | ||
|
||
public static Option<TNumber> ParseNumberOrNone<TNumber>(this ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider) | ||
where TNumber : INumberBase<TNumber> | ||
=> TNumber.TryParse(value, style, provider, out var result) | ||
? result | ||
: Option<TNumber>.None; | ||
} | ||
#endif |
18 changes: 18 additions & 0 deletions
18
Funcky/Extensions/ParseExtensions/ParseExtensions.GenericParseable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if GENERIC_PARSEABLE | ||
namespace Funcky.Extensions; | ||
|
||
public static partial class ParseExtensions | ||
{ | ||
public static Option<TParseable> ParseOrNone<TParseable>(this ReadOnlySpan<char> value, IFormatProvider? provider) | ||
where TParseable : ISpanParsable<TParseable> | ||
=> TParseable.TryParse(value, provider, out var result) | ||
? result | ||
: Option<TParseable>.None; | ||
|
||
public static Option<TParseable> ParseOrNone<TParseable>(this string? value, IFormatProvider? provider) | ||
where TParseable : IParsable<TParseable> | ||
=> TParseable.TryParse(value, provider, out var result) | ||
? result | ||
: Option<TParseable>.None; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters