Skip to content

Commit ad371b0

Browse files
committed
Fixed the entire feature
1 parent 5afd6f0 commit ad371b0

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/FSharpPlus/Parsing.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Parsing =
1111
open FSharpPlus.Internals
1212
open FSharpPlus.Internals.Prelude
1313

14-
let inline private getGroups (pf: PrintfFormat<_,_,_,_,_>) str =
14+
let getGroups (pf: PrintfFormat<_,_,_,_,_>) str =
1515
let format = pf.Value
1616
let regex = System.Text.StringBuilder "^"
1717
let mutable groups = FSharp.Core.CompilerServices.ArrayCollector()
@@ -33,7 +33,7 @@ module Parsing =
3333
| 'B' -> "([01]+)"
3434
| 'c' -> "(.)"
3535
| 'd' | 'i' -> "([+-]?[0-9]+)"
36-
| 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | 'M' -> "([+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?)"
36+
| 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | 'M' -> "([+-]?[0-9.]+(?:[eE][+-]?[0-9]+)?)"
3737
| 'o' -> "([0-7]+)"
3838
| 'u' -> "([0-9]+)"
3939
| 's' -> "(.*?)"
@@ -50,12 +50,14 @@ module Parsing =
5050
|> string
5151
|> Regex
5252
|> _.Match(str)
53-
|> _.Groups
53+
|> fun m ->
54+
if not m.Success then [||] else
55+
m.Groups
5456
|> Seq.cast<Group>
5557
|> Seq.skip 1
5658
|> Seq.map _.Value
5759
|> Seq.toArray
58-
|> Array.zip <| groups.Close()
60+
|> Array.zip <| groups.Close()
5961

6062
let inline private conv (destType: System.Type) (b: int) (s: string) =
6163
match destType with

tests/FSharpPlus.Tests/Parsing.fs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,24 @@ module Parsing =
100100

101101

102102
let _zzz = sscanf "(%%%s)" "(%hello)"
103+
let _zzz1 = sscanf "%%(%s)" "%(hello)"
103104
let (_x1,_y1,_z1) = sscanf "%s--%s-%s" "test--this-string"
104105

105106
let inline (|Like|_|) format = FSharpPlus.Parsing.trySscanf format
106-
match "(%hello)" with Like "(%%%s)" "hello" -> () | _ -> failwith "didn't match"
107+
match "ab" with Like "%c" _ -> failwith "wrong match" | Like "%c%c" ('a', 'b') -> () | _ -> failwith "didn't match"
108+
match "abc" with Like "%c%c" ('a', 'b') -> failwith "wrong match" | Like "%c%c%c%s" ('a', 'b', 'c', "") -> () | _ -> failwith "didn't match"
109+
match "(%hello)" with
110+
| Like "%d" _ | Like "%f" _ | Like "%x" _ -> failwith "wrong match"
111+
| Like "%%(%%%s)" _ | Like "(%%%sa" _ | Like "(%%hel%c" _ | Like "%%h%cllo)" _ -> failwith "wrong match"
112+
| Like "(%%%s)" "hello" -> ()
113+
| _ -> failwith "didn't match"
107114
match "test--this-gg" with Like "%s--%s-%s" ("test", "this", "gg") -> () | _ -> failwith "didn't match"
108115
match "1 2.1 3.4 .3 43.2e32 0 f f" with Like "%f %F %g %G %e %E %c %c" (1f, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f') -> () | _ -> failwith "didn't match"
109116
match "1 2.1 3.4 .3 43.2e32 0 f f f" with Like "%f %F %g %G %e %E %c %c %c" (1f, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
110-
match "1 2.1 3.4 .3 43.2e32 0 f f ff" with Like "%f %F %g %G %e %E %c %c %c%c" (1f, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
111-
match "1 2.1 3.4 .3 43.2e32 0 f f fff" with Like "%f %F %g %G %e %E %c %c %c%c%c" (1f, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
112-
match "1 2.1 3.4 .3 43.2e32 0 f f fff 16" with Like "%f %F %g %G %e %E %c %c %c%c%c%i" (1f, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16) -> () | _ -> failwith "didn't match"
113-
match "1 2.1 3.4 .3 43.2e32 0 f f fff 16 17" with Like "%f %F %g %G %e %E %c %c %c%c%c%i %f" (1f, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16, 17.) -> () | _ -> failwith "didn't match"
117+
match "1 2.1 3.4 .3 43.2e32 0 f f ff" with Like "%B %F %g %G %e %E %c %c %c%c" (1, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
118+
match "1 2.1 3.4 .3 43.2e32 0 f f fff" with Like "%o %F %g %G %e %E %c %c %c%c%c" (1, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
119+
match "1 2.1 3.4 .3 43.2e32 0 f f fff16" with Like "%x %F %g %G %e %E %c %c %c%c%c%i" (1, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16) -> () | _ -> failwith "didn't match"
120+
match "1 2.1 3.4 .3 43.2e32 0 f f fff16 17" with Like "%X %F %g %G %e %E %c %c %c%c%c%i %f" (1, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16, 17.) -> () | _ -> failwith "didn't match"
114121
match "13 43 AA 77A" with Like "%x %X %x %o%X" (0x13, 0x43, 0xAA, 0o77, 0xA) -> () | _ -> failwith "didn't match"
115122
match "13 43 AA 77A" with Like "%B%x %X %x %o%X" (0b1, 0x3, 0x43, 0xAA, 0o77, 0xA) -> () | _ -> failwith "didn't match"
116123
match "111AAA" with Like "%B%s" (0b111, "AAA") -> () | _ -> failwith "didn't match"

0 commit comments

Comments
 (0)