Skip to content

Commit 7968ec8

Browse files
committed
TryParse
1 parent 519bcad commit 7968ec8

3 files changed

Lines changed: 33 additions & 26 deletions

File tree

src/FSharpPlus.CSharp/Library.fs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ open System.Runtime.CompilerServices
33
open System
44
open System.Collections.Generic
55
open FSharpPlus
6+
open FSharpPlus.Internals
67
module internal Internals=
78
module Result=
89
/// Wraps a function, encapsulates any exception thrown within to a Result
@@ -14,8 +15,10 @@ module internal Internals=
1415
/// Stores the cast value in Ok if successful, otherwise stores the exception in Error
1516
let inline cast (o: obj) = protect unbox o
1617

18+
let inline tupleToOption x = match x with true, value -> Some value | _ -> None
1719

1820
open Internals
21+
open System.Globalization
1922

2023
[<Extension>]
2124
type Options =
@@ -149,23 +152,19 @@ type Options =
149152

150153
static member SomeUnit = Some()
151154

152-
static member ParseInt s : int32 option = tryParse s
155+
type NumberParser(numberstyles: NumberStyles, cultureInfo: CultureInfo)=
156+
new()=NumberParser(NumberStyles.Any, CultureInfo.InvariantCulture)
157+
member __.ParseDateTimeOffset s :DateTimeOffset option= tryParse s
158+
member __.TryParseDecimal (x:string)= Decimal.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<decimal>
159+
member __.TryParseFloat (x:string)= Single.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<float32>
160+
member __.TryParseDouble (x:string)= Double.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<float>
161+
member __.TryParseUint16 (x:string)= UInt16.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<uint16>
162+
member __.TryParseUint32 (x:string)= UInt32.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<uint32>
163+
member __.TryParseUint64 (x:string)= UInt64.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<uint64>
164+
member __.TryParseInt16 (x:string)= Int16.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<int16>
165+
member __.TryParseInt (x:string)= Int32.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<int>
166+
member __.TryParseInt64 (x:string)= Int64.TryParse (x, numberstyles, cultureInfo) |> tupleToOption : option<int64>
153167

154-
static member ParseDecimal s : Decimal option= tryParse s
155-
156-
static member ParseDouble s : Double option = tryParse s
157-
158-
static member ParseFloat s : Single option = tryParse s
159-
160-
static member ParseInt16 s : Int16 option= tryParse s
161-
162-
static member ParseInt64 s : Int64 option= tryParse s
163-
164-
static member ParseByte s : byte option= tryParse s
165-
166-
static member ParseDateTime s : DateTime option= tryParse s
167-
168-
static member ParseDateTimeOffset s :DateTimeOffset option= tryParse s
169168

170169
[<Extension>]
171170
type Choices =

tests/FSharpPlus.CSharp.Tests/OptionTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,29 @@ public void Dont() {
155155

156156
[Test]
157157
public void TryParseInt() {
158-
var a = Options.ParseInt("123");
158+
var parser = new NumberParser();
159+
var a = parser.TryParseInt("123");
159160
Assert.AreEqual(123, a.Value);
160161
}
161162

162-
/* [Test]
163+
[Test]
163164
public void TryParseDec() {
164-
var a = Option.ParseDecimal("123.44", NumberStyles.Any, CultureInfo.InvariantCulture);
165+
var parser = new NumberParser(NumberStyles.Any, CultureInfo.InvariantCulture);
166+
var a = parser.TryParseDecimal("123.44");
165167
Assert.AreEqual(123.44m, a.Value);
166-
}*/
168+
}
167169

168170
[Test]
169171
public void TryParseDouble() {
170-
var a = Options.ParseDouble("123E12");
172+
var parser = new NumberParser();
173+
var a = parser.TryParseDouble("123E12");
171174
Assert.AreEqual(123E12, a.Value);
172175
}
173176

174177
[Test]
175178
public void TryParseFloat() {
176-
var a = Options.ParseFloat("12");
179+
var parser = new NumberParser();
180+
var a = parser.TryParseFloat("12");
177181
Assert.AreEqual(12, a.Value);
178182
}
179183

tests/FSharpPlus.CSharp.Tests/ResultTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ public void Cast_Exception() {
6262

6363
[Test]
6464
public void ResultToOption() {
65+
var parser=new NumberParser();
6566
object a = 40;
6667
const string b = "60";
67-
var r = from i in Options.ParseInt(b)
68+
var r = from i in parser.TryParseInt(b)
6869
from j in Results.Cast<int>(a).ToOption()
6970
select i + j;
7071
Assert.AreEqual(100.Some(), r);
@@ -73,9 +74,10 @@ from j in Results.Cast<int>(a).ToOption()
7374

7475
[Test]
7576
public void OptionToResult() {
77+
var parser=new NumberParser();
7678
object a = 40;
7779
const string b = "60";
78-
var r = from i in Options.ParseInt(b).ToResult(new Exception())
80+
var r = from i in parser.TryParseInt(b).ToResult(new Exception())
7981
from j in Results.Cast<int>(a)
8082
select i + j;
8183
r.Match(i => Assert.AreEqual(100, i),
@@ -84,9 +86,10 @@ from j in Results.Cast<int>(a)
8486

8587
[Test]
8688
public void SelectSecond_OK() {
89+
var parser=new NumberParser();
8790
object a = 40;
8891
const string b = "60";
89-
var r = from i in Options.ParseInt(b).ToResult("Invalid value b")
92+
var r = from i in parser.TryParseInt(b).ToResult("Invalid value b")
9093
from j in Results.Cast<int>(a).SelectError(_ => "Invalid value a")
9194
select i + j;
9295
r.Match(i => Assert.AreEqual(100, i),
@@ -95,9 +98,10 @@ from j in Results.Cast<int>(a).SelectError(_ => "Invalid value a")
9598

9699
[Test]
97100
public void SelectSecond_Error() {
101+
var parser=new NumberParser();
98102
object a = 40;
99103
const string b = "xx";
100-
var r = from i in Options.ParseInt(b).ToResult("Invalid value b")
104+
var r = from i in parser.TryParseInt(b).ToResult("Invalid value b")
101105
from j in Results.Cast<int>(a).SelectError(_ => "Invalid value a")
102106
select i + j;
103107
r.Match(i => Assert.Fail("should not have succeeded with value {0}", i),

0 commit comments

Comments
 (0)