Skip to content

Commit 2fee409

Browse files
authored
Support %B in scan functions (#446)
1 parent 3e32458 commit 2fee409

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/FSharpPlus/Parsing.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Parsing =
1818
open FSharpPlus
1919
open FSharpPlus.Internals.Prelude
2020

21-
let formatters = [|"%b"; "%d"; "%i"; "%s"; "%u"; "%x"; "%X"; "%o"; "%e"; "%E"; "%f"; "%F"; "%g"; "%G"; "%M"; "%c"; "%A"|]
21+
let formatters = [|"%A"; "%b"; "%B"; "%c"; "%d"; "%e"; "%E"; "%f"; "%F"; "%g"; "%G"; "%i"; "%M"; "%o"; "%O"; "%s"; "%u"; "%x"; "%X"|]
2222

2323
let getGroups (pf: PrintfFormat<_,_,_,_,_>) s =
2424
let formatStr = replace "%%" "%" pf.Value
@@ -34,8 +34,6 @@ module Parsing =
3434
|> Seq.toArray
3535
(getGroup s, getGroup pf.Value) ||> Array.zipShortest
3636

37-
38-
3937
let conv (destType: System.Type) (b: int) (s: string) =
4038
match destType with
4139
| t when t = typeof<byte> -> Convert.ToByte (s, b) |> box
@@ -51,12 +49,14 @@ module Parsing =
5149

5250
let inline parse (s: string, f: string) : 'r =
5351
match f with
52+
| "%B" -> conv typeof<'r> 2 s |> string |> parse
5453
| "%o" -> conv typeof<'r> 8 s |> string |> parse
5554
| "%x" | "%X" -> conv typeof<'r> 16 s |> string |> parse
5655
| _ -> parse s
5756

5857
let inline tryParse (s: string, f: string) : 'r option =
5958
match f with
59+
| "%B" -> Option.protect (conv typeof<'r> 2) s |> Option.map string |> Option.bind tryParse
6060
| "%o" -> Option.protect (conv typeof<'r> 8) s |> Option.map string |> Option.bind tryParse
6161
| "%x" | "%X" -> Option.protect (conv typeof<'r> 16) s |> Option.map string |> Option.bind tryParse
6262
| _ -> tryParse s

tests/FSharpPlus.Tests/General.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,10 +2188,13 @@ module Parsing =
21882188

21892189
let x = trySscanf "%X %x" "13 43"
21902190
let o = trySscanf "%o" "10"
2191+
let b = trySscanf (PrintfFormat<int -> string, unit, string, string, int> "%B") "101"
2192+
let a = trySscanf (PrintfFormat<int -> int -> int -> int -> string, unit, string, string, int * int * int * int> "%B %o %x %X") "100 100 100 100"
21912193

21922194
areEqual (Some (19, 67)) x
21932195
areEqual (Some 8) o
2194-
2196+
areEqual (Some 5) b
2197+
areEqual (Some (4, 64, 256, 256)) a
21952198

21962199
module Conversions =
21972200
let test =

0 commit comments

Comments
 (0)