Making strconv.parseFloatPrefix exported?

257 views
Skip to first unread message

ben...@gmail.com

unread,
Jun 3, 2022, 1:02:44 AM6/3/22
to golang-dev
Hi folks,

Currently there's an unexported function strconv.parseFloatPrefix (in strconv/atof.go). This function would be useful for certain types of parsing, for example, you could imagine code that parsed a string like (the somewhat-contrived) "float+float+float". Currently you'd either have to write a full scanner for floats and pass the result to ParseFloat, which means a lot of edge cases *and* doing work that ParseFloat will then do again. If parseFloatPrefix was exported as ParseFloatPrefix, you could simply skip over the number of bytes it returns.

I would use this in my GoAWK project, because the implicit conversion that AWK does when you ask for a number but the input is a string is allowed to be a prefix, like "1.5xyz" yields 1.5. I currently implement this with my own pre-scanner and then pass the result to ParseFloat. (I believe I used to use text/scanner, but it was rather heavy and slow.) I ran into this because I want to add hexadecimal floating point support, which is allowed by the POSIX AWK spec.

In addition, I see that strconv.parseFloatPrefix is used by the Vitess database project in some expression-parsing code. They pull in the real strconv.parseFloatPrefix using a "//go:linkname" hack, which I'm not exactly eager to do: https://github.com/vitessio/vitess/blob/b979359f861d838ed3e0d7adc3cbba57a2975c7e/go/hack/compat.go#L49-L52

Thoughts? I'm happy to open an issue on this if folks think it has a reasonable chance of success, or at least some worthwhile discussion.

-Ben

Russ Cox

unread,
Jun 7, 2022, 8:23:31 AM6/7/22
to ben...@gmail.com, golang-dev
We just added strconv.QuotedPrefix. 
I think we could reasonably add strconv.FloatPrefix,
although at that point perhaps we should also consider
strconv.IntPrefix, strconv.UintPrefix, strconv.BoolPrefix,
and strconv.ComplexPrefix too.
Feel free to file a proposal. Thanks!

Best,
Russ

Rob Pike

unread,
Jun 7, 2022, 8:55:28 AM6/7/22
to Russ Cox, ben...@gmail.com, golang-dev
You need strconv.RunePrefix for that list to be complete. That's a lot
of .... functions. Not sure it's a level of attention that a core
package needs.

-rob
> --
> You received this message because you are subscribed to the Google Groups "golang-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/CAA8EjDQd2Sas3Y4dNKfCZCcvwZiC5%2B%2Br%3D5zNF%3De0FRfxtWAfRQ%40mail.gmail.com.

Russ Cox

unread,
Jun 7, 2022, 11:38:31 AM6/7/22
to Rob Pike, ben...@gmail.com, golang-dev
On Tue, Jun 7, 2022 at 8:55 AM Rob Pike <r...@golang.org> wrote:
You need strconv.RunePrefix for that list to be complete. That's a lot
of .... functions. Not sure it's a level of attention that a core
package needs.

I agree with your general point, which is why I suggested to include all of them in the proposal:
we should decide where the line is across the whole collection, not just one at a time.
That said, QuotedPrefix is handling both quoted strings and quoted runes,
so I don't think RunePrefix needs to be there as well.

Best,
Russ
 

ben...@gmail.com

unread,
Jun 12, 2022, 6:14:01 AM6/12/22
to golang-dev
Proposal issue created: https://github.com/golang/go/issues/53340 -- note my discussion points at the bottom of the proposal.

-Ben
Reply all
Reply to author
Forward
0 new messages