Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/FSharpPlus/Parsing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Parsing =
open FSharpPlus
open FSharpPlus.Internals.Prelude

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

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



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

let inline parse (s: string, f: string) : 'r =
match f with
| "%B" -> conv typeof<'r> 2 s |> string |> parse
| "%o" -> conv typeof<'r> 8 s |> string |> parse
| "%x" | "%X" -> conv typeof<'r> 16 s |> string |> parse
| _ -> parse s

let inline tryParse (s: string, f: string) : 'r option =
match f with
| "%B" -> Option.protect (conv typeof<'r> 2) s |> Option.map string |> Option.bind tryParse
| "%o" -> Option.protect (conv typeof<'r> 8) s |> Option.map string |> Option.bind tryParse
| "%x" | "%X" -> Option.protect (conv typeof<'r> 16) s |> Option.map string |> Option.bind tryParse
| _ -> tryParse s
Expand Down
5 changes: 4 additions & 1 deletion tests/FSharpPlus.Tests/General.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,10 +2188,13 @@ module Parsing =

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

areEqual (Some (19, 67)) x
areEqual (Some 8) o

areEqual (Some 5) b
areEqual (Some (4, 64, 256, 256)) a

module Conversions =
let test =
Expand Down