-
Notifications
You must be signed in to change notification settings - Fork 100
Support putting two format specifiers next to each other #620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Indeed
But I was expecting |
Test failure seems... unrelated? |
Not sure, at first sight it looks related, here's what I am seeing:
Have you tried to repro locally ? I normally paste code fragments in a script to work it out. It's way quicker than compiling the whole lib and trying stuff. |
When I try to debug locally, Visual Studio just hangs on me... |
That's why I always use (and suggest) a script. |
Ok, so the root cause is that sometimes we do want to consume spaces before and after as shown in one of the existing tests. I've added support via the |
I think it's now fixed |
I rewrote it to be more performant and also removed the spaces |
src/FSharpPlus/Parsing.fs
Outdated
let inline trySscanf (pf: PrintfFormat<_,_,_,_,'``(T1 * T2 * ... * Tn)``>) : string -> '``(T1 * T2 * ... * Tn)`` option = getGroups pf >> TryParseArray.Invoke | ||
|
||
/// Matches a formatted text with the result of parsing each element. Will not match in case of failure. | ||
let inline (|Scan|_|) (pf: PrintfFormat<_,_,_,_,'``(T1 * T2 * ... * Tn)``>) : string -> '``(T1 * T2 * ... * Tn)`` option = trySscanf pf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it makes sense to re-use our existing Parsed
active pattern ?
It think this one expand it, but it doesn't break it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can you fit a format string into (|Parsed|_|): string -> 'T option
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't, it is the other way around. Scan is an upgrade of Parsed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(|Parsed|_|)
uses tryParse
inside and (|Scan|_|)
uses TryParseArray
inside which uses tryParse
. The logic is already shared.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or you mean this active pattern should be named (|Parse|_|)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I just call it (|Parse|_|)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. In general, active patterns should describe what something is, rather than what it does. That’s why we avoid naming them after actions like Parse
.
We actually used to have an active pattern named Parse
, but we deprecated it in favor of Parsed
, to better reflect that it's identifying a parsed value, not performing the parsing itself.
So I’d recommend sticking with Parsed
(or something similar that describes the result), to stay consistent with F# naming conventions and improve code readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reusing the name Parsed
+ F# naming convention for formatted strings f
-> Parsedf
But it doesn't read well. Something like ParsedFormat
would be too long especially when used across multiple lines. Maybe an adjective like Like
per fsharp/fslang-suggestions#1426 would read better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose, let's move the Active Pattern to its own issue/PR.
Then let's merge this PR, which is a different thing and it is already approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separated to #635
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job !
Right now, trySscanf on e.g.
"%c%c"
won't work because all specifiers are translated to(.*?)
under the hood.Also added:
-
format specifier consumes spaces to right+
format specifier consumes preceding plus for numbers(|Scan|_|)
that turnstrySscanf
into an active pattern