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

proposal: different octal base literal representation #12711

Open
RomanSaveljev opened this Issue Sep 22, 2015 · 28 comments

Comments

Projects
None yet
9 participants
@RomanSaveljev
Copy link

RomanSaveljev commented Sep 22, 2015

Shortly

Proposal is to add an alternative syntax for representing octal literals and deprecate the current syntax in Go 2+. Deprecation is not necessary, but is personally perceived to be inline with Go's idioms

Current syntax: octal_lit = "0" { octal_digit } .
Proposed change: octal_lit = "0o" { octal_digit } .

Background

The idea is directly borrowed from CoffeeScript, where the feature exists to help developers to avoid various typos in their code:

  • Missed x in 0x1234
  • Missed . after 0 in 0.1234
  • Missed . before 0 in .01234
  • Extra leading 0 in decimal literal as in 01234

Personal view on the current syntax

I think that the current syntax for octal literals is outdated and was simply copied from ancestor languages, because the problem it introduced there was not evident. This proposal may very well be rejected on the grounds of Go 1 compatibility promise assesment, but I guess it is good to keep the pressure for a change.

@DisposaBoy

This comment has been minimized.

Copy link

DisposaBoy commented Sep 22, 2015

I don't have any opinion on this proposal, I'd just like to point out that at least 3 of those 4 points are invalid IMO.

Missed x in 0x1234

If you can miss x, what stops you missing o

Missed . after 0 in 0.1234
Missed . before 0 in .01234

I don't think most real code would even compile if you made this mistake because (presumably) you'd end up passing/assigning that float to an int and vice-versa

Extra leading 0 in decimal literal as in 01234

This one's a little less invalid, but I don't know that I see any benefit here either. I can't remember a time when I've seen such a number that wasn't octal so that kinda rules out copy-paste mistakes. And if you can type that number by mistake, what stops you from typing 11234.

@RomanSaveljev

This comment has been minimized.

Copy link
Author

RomanSaveljev commented Sep 22, 2015

@DisposaBoy thanks, I realized that I mostly agree with your comments on floating point numbers. There is one more thing though. 072.40 is a perfectly valid floating point number and is equal to 72.40. To me this is again a confusing language decision.

If you can miss x, what stops you missing o

The deprecation of a leading 0 to denote octal (floating-point?) literals will turn it into a compilation error.

And if you can type that number by mistake, what stops you from typing 11234

Nothing would stop me from typing mistyping any number, correct. The proposal is simply about little more help from a compiler here and there.

Please continue giving a constructive feedback. I appreciate your comments

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

ianlancetaylor commented Sep 22, 2015

I feel like you are missing a cost/benefit discussion. Changing the language, even in a hypothetical Go 2, carries a cost. The benefit should be something more than personal preference.

I'm not saying that it's impossible to change the language in Go 2, if there is a Go 2. I'm saying that the language should only be changed for good reasons that bring clear benefits.

@RomanSaveljev

This comment has been minimized.

Copy link
Author

RomanSaveljev commented Sep 22, 2015

That is true. Some cost is implied here (as with any change). It is obvious the effort should weighed against the gains. However, I am in no position to judge on that (speaking from my less than shallow experience with Go).

Any educated input on the matter is more than welcome.

@robpike

This comment has been minimized.

Copy link
Contributor

robpike commented Sep 22, 2015

If anything like this is to happen in Go 2, it should be more interesting than this. One possibility is a general way to have literals of arbitrary base, such as 3r012 where 3 is the base (radix), r is the marker, and the digits follow. This form has been used in other languages.

Regardless, I don't believe any change is worthwhile. There is no pressing need for any of this. The current triad is convenient, familiar, and within a tiny fraction of being always enough.

@griesemer

This comment has been minimized.

Copy link
Contributor

griesemer commented Sep 22, 2015

For the historical perspective: The leading-0 notation was not "simply copied from ancestor languages" - this was quite a deliberate decision, with several weeks of on-and-off thinking about it. There was also discussion about a more general notation to support other number bases (2 comes to mind), or arbitrary bases. Eventually we decided to stick with the historic notation, despite its problems for several reasons:

  • C programmers are familiar with it, there are no surprises
  • there was a feeling that ignoring the leading 0 might lead to hard-to-track down errors when existing C code using the 0-notation (file access) would be ported to Go
  • at the time, none of us (Ken, Rob, I) had a better suggestion or felt a strong need for changing it

In retrospect, being afraid of introducing subtle errors was perhaps overly cautious, but we don't really know.

That said, I don't like you suggestion, and fwiw, both the 'o' for octal and 'b' for binary notation were also discussed in the design of Go. It's simply not enough bang for the buck.

If, and that is a big IF, there were such a change, I think it should enable arbitrary number bases with a uniform syntax (I see Rob made just the same point). I have proposed the following notation during the design phase of Go (albeit after we made the decision to stick with the 0x and leading-0 notation):

int_lit = [ radix 'x' ] digit { digit } .
radix = decimal_digit [ decimal_digit ] .
digit = decimal_digit | 'a' | 'A' | 'b' | 'B' | ... 'z' | 'Z' .
decimal_digit = '0' | '1' | ... '9' .

With the following semantics: A leading radix indicates the number base, any value from 2 to 36 is valid. The subsequent digits must be in the range from 0 to radix-1. For instance, 2x101 means (decimal) 5, 8x660 corresponds to our current 0660, 12x106 means (decimal) 150, etc. Additionally, we permit the radix 0 and state that radix 0 is equivalent to radix 16. This permits the common and widely recognized hexadecimal notation 0x1234 as we know it. (This is exactly what Rob suggested above, but with r replaced by x which enables backward-compatibility with our existing hex notation).

The cost of introducing this is probably small if we keep that leading-0 notation as well (it's fully backward-compatible, and it's done in one or 2 places in a compiler's frontend, and its trivial). But the benefit might be even smaller.

@RomanSaveljev

This comment has been minimized.

Copy link
Author

RomanSaveljev commented Sep 22, 2015

@robpike @griesemer Thank you. I agree with your reasoning. Tthis is also good to know similar thoughts have (had) been sitting in the back of your head.

This proposal is not pressing whatsoever and the least surprise principle must hold. Well, I am happy to see it implemented in a more "interesting" way, or not at all.

@griesemer griesemer added this to the Unplanned milestone Sep 22, 2015

@griesemer griesemer added the Go2 label Sep 22, 2015

@adg adg added Proposal and removed Proposal labels Sep 25, 2015

@rsc rsc changed the title Proposal: Enhanced octal base literal representation proposal: different octal base literal representation Oct 19, 2015

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Oct 24, 2015

I am against this, for the reasons Rob and Robert already gave (which were the reasons we didn't do it in 2009).

General consensus seems against too.

@rsc rsc closed this Oct 24, 2015

@golang golang locked and limited conversation to collaborators Oct 24, 2016

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Jan 17, 2019

We've posted a combined proposal for #19308, #12711, #28493, and #29008 at golang.org/design/19308-number-literals, and it includes octal literals as suggested in this issue - the world looks different now than it did in 2015.

Note that this will be the first proposal to follow the process outlined in the blog post: we will have everything ready to go and checked in at the start of the Go 1.13 cycle (Feb 1), we will spend the next three months using those features and soliciting feedback based on actual usage, and then at the start of the release freeze (May 1), we will make the "launch decision" about whether to include the work in Go 1.13 or not.

Thanks for your feedback and all your help improving Go.

@robpike

This comment has been minimized.

Copy link
Contributor

robpike commented Jan 17, 2019

Originally suggested to me by @rsc: it might be wise to deprecate (but still support) 0X for hexadecimal, and then not add 0B (unnecessary) and 0O (unnecessary, confusing, and hard to read).

gopherbot pushed a commit to golang/proposal that referenced this issue Jan 30, 2019

design/19308-number-literals: add note about gofmt
Add note about lowercasing base prefixes and exponents in gofmt.
See also golang.org/cl/160184.

For golang/go#19308.
For golang/go#12711.
For golang/go#29008.

Change-Id: I79f813badc0b28147fba40072d9c7436bfa91aaa
Reviewed-on: https://go-review.googlesource.com/c/160478
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Feb 4, 2019

Change https://golang.org/cl/161098 mentions this issue: spec: document new Go2 number literals

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Feb 5, 2019

Change https://golang.org/cl/161199 mentions this issue: text/scanner: accept new Go2 number literals

gopherbot pushed a commit that referenced this issue Feb 11, 2019

cmd/compile: accept new Go2 number literals
This CL introduces compiler support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.

The new Go 2 number literal scanner accepts the following liberal format:

number   = [ prefix ] digits [ "." digits ] [ exponent ] [ "i" ] .
prefix   = "0" [ "b" |"B" | "o" | "O" | "x" | "X" ] .
digits   = { digit | "_" } .
exponent = ( "e" | "E" | "p" | "P" ) [ "+" | "-" ] digits .

If the number starts with "0x" or "0X", digit is any hexadecimal digit;
otherwise, digit is any decimal digit. If the accepted number is not valid,
errors are reported accordingly.

See the new test cases in scanner_test.go for a selection of valid and
invalid numbers and the respective error messages.

R=Go1.13

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Change-Id: Ic8febc7bd4dc5186b16a8c8897691e81125cf0ca
Reviewed-on: https://go-review.googlesource.com/c/157677
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>

gopherbot pushed a commit that referenced this issue Feb 11, 2019

go/scanner: accept new Go2 number literals
This CL introduces go/scanner support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.
The new code is closely mirroring the respective code for number literals in
cmd/compile/internal/syntax/scanner.go.

R=Go1.13

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Change-Id: I5315c6aaa7cfc41a618296be20e3acd5114d6b3c
Reviewed-on: https://go-review.googlesource.com/c/159997
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>

gopherbot pushed a commit that referenced this issue Feb 11, 2019

cmd/gofmt: test that Go 2 number literals can be formatted
R=Go1.13

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Change-Id: Icd25aa7f6e18ed671ea6cf2b1b292899daf4b1a5
Reviewed-on: https://go-review.googlesource.com/c/160018
Reviewed-by: Russ Cox <rsc@golang.org>

gopherbot pushed a commit that referenced this issue Feb 11, 2019

go/constant: accept new Go2 number literals
This CL introduces go/constant support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.

R=Go1.13

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Change-Id: I7a55f91b8b6373ae6d98ba923b626d33c5552946
Reviewed-on: https://go-review.googlesource.com/c/160239
Reviewed-by: Russ Cox <rsc@golang.org>

gopherbot pushed a commit that referenced this issue Feb 11, 2019

go/types: add tests for new Go 2 number literals
This CL ensures that go/types can now handle the new
Go 2 number literals. The relevant changes enabling
them in go/types were made in go/constant in the CL
https://golang.org/cl/160239.

R=Go1.13

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Change-Id: I45c1387198fac94769ac59c5301d86b4e1a1ff98
Reviewed-on: https://go-review.googlesource.com/c/160240
Reviewed-by: Russ Cox <rsc@golang.org>

gopherbot pushed a commit that referenced this issue Feb 11, 2019

cmd/gofmt: normalize number prefixes and exponents
Rewrite non-decimal number prefixes to always use a lower-case base
("0X" -> "0x", etc.), and rewrite exponents to use a lower-case 'e'
or 'p'. Leave hexadecimal digits and 0-octals alone.

Comparing the best time of 3 runs of `time go test -run All` with
the time for a gofmt that doesn't do the rewrite shows no increase
in runtime for this bulk gofmt application (in fact on my machine
I see a small decline, probably due to cache effects).

R=Go1.13

Updates #12711.
Updates #19308.
Updates #29008.

Change-Id: I9c6ebed2ffa0a6a001c59412a73382090955f5a9
Reviewed-on: https://go-review.googlesource.com/c/160184
Reviewed-by: Ian Lance Taylor <iant@golang.org>

gopherbot pushed a commit that referenced this issue Feb 11, 2019

text/scanner: accept new Go2 number literals
This CL introduces text/scanner support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.
The new code is closely mirroring the respective code for number literals in
cmd/compile/internal/syntax/scanner.go.

Uniformly use the term "invalid" rather than "illegal" in error messages
to match the respective error messages in the other scanners directly.

R=Go1.13

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Change-Id: I2f291de13ba5afc0e530cd8326e6bf4c3858ebac
Reviewed-on: https://go-review.googlesource.com/c/161199
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>

gopherbot pushed a commit that referenced this issue Feb 12, 2019

strconv: add 0b, 0o integer prefixes in ParseInt, ParseUint
This CL modifies ParseInt and ParseUint to recognize
0b and 0o as binary and octal base prefixes when base == 0.

See golang.org/design/19308-number-literals for background.

For #19308.
For #12711.

Change-Id: I8efe067f415aa517bdefbff7e230d3fa1694d530
Reviewed-on: https://go-review.googlesource.com/c/160244
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

cmd/compile: accept new Go2 number literals
This CL introduces compiler support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.

The new Go 2 number literal scanner accepts the following liberal format:

number   = [ prefix ] digits [ "." digits ] [ exponent ] [ "i" ] .
prefix   = "0" [ "b" |"B" | "o" | "O" | "x" | "X" ] .
digits   = { digit | "_" } .
exponent = ( "e" | "E" | "p" | "P" ) [ "+" | "-" ] digits .

If the number starts with "0x" or "0X", digit is any hexadecimal digit;
otherwise, digit is any decimal digit. If the accepted number is not valid,
errors are reported accordingly.

See the new test cases in scanner_test.go for a selection of valid and
invalid numbers and the respective error messages.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: Ic8febc7bd4dc5186b16a8c8897691e81125cf0ca
Reviewed-on: https://go-review.googlesource.com/c/157677
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

go/scanner: accept new Go2 number literals
This CL introduces go/scanner support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.
The new code is closely mirroring the respective code for number literals in
cmd/compile/internal/syntax/scanner.go.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I5315c6aaa7cfc41a618296be20e3acd5114d6b3c
Reviewed-on: https://go-review.googlesource.com/c/159997
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

cmd/gofmt: test that Go 2 number literals can be formatted
R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: Icd25aa7f6e18ed671ea6cf2b1b292899daf4b1a5
Reviewed-on: https://go-review.googlesource.com/c/160018
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

go/constant: accept new Go2 number literals
This CL introduces go/constant support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I7a55f91b8b6373ae6d98ba923b626d33c5552946
Reviewed-on: https://go-review.googlesource.com/c/160239
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

go/types: add tests for new Go 2 number literals
This CL ensures that go/types can now handle the new
Go 2 number literals. The relevant changes enabling
them in go/types were made in go/constant in the CL
https://golang.org/cl/160239.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I45c1387198fac94769ac59c5301d86b4e1a1ff98
Reviewed-on: https://go-review.googlesource.com/c/160240
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

cmd/gofmt: normalize number prefixes and exponents
Rewrite non-decimal number prefixes to always use a lower-case base
("0X" -> "0x", etc.), and rewrite exponents to use a lower-case 'e'
or 'p'. Leave hexadecimal digits and 0-octals alone.

Comparing the best time of 3 runs of `time go test -run All` with
the time for a gofmt that doesn't do the rewrite shows no increase
in runtime for this bulk gofmt application (in fact on my machine
I see a small decline, probably due to cache effects).

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#29008.

Change-Id: I9c6ebed2ffa0a6a001c59412a73382090955f5a9
Reviewed-on: https://go-review.googlesource.com/c/160184
Reviewed-by: Ian Lance Taylor <iant@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

text/scanner: accept new Go2 number literals
This CL introduces text/scanner support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.
The new code is closely mirroring the respective code for number literals in
cmd/compile/internal/syntax/scanner.go.

Uniformly use the term "invalid" rather than "illegal" in error messages
to match the respective error messages in the other scanners directly.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I2f291de13ba5afc0e530cd8326e6bf4c3858ebac
Reviewed-on: https://go-review.googlesource.com/c/161199
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 18, 2019

strconv: add 0b, 0o integer prefixes in ParseInt, ParseUint
This CL modifies ParseInt and ParseUint to recognize
0b and 0o as binary and octal base prefixes when base == 0.

See golang.org/design/19308-number-literals for background.

For golang#19308.
For golang#12711.

Change-Id: I8efe067f415aa517bdefbff7e230d3fa1694d530
Reviewed-on: https://go-review.googlesource.com/c/160244
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

cmd/compile: accept new Go2 number literals
This CL introduces compiler support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.

The new Go 2 number literal scanner accepts the following liberal format:

number   = [ prefix ] digits [ "." digits ] [ exponent ] [ "i" ] .
prefix   = "0" [ "b" |"B" | "o" | "O" | "x" | "X" ] .
digits   = { digit | "_" } .
exponent = ( "e" | "E" | "p" | "P" ) [ "+" | "-" ] digits .

If the number starts with "0x" or "0X", digit is any hexadecimal digit;
otherwise, digit is any decimal digit. If the accepted number is not valid,
errors are reported accordingly.

See the new test cases in scanner_test.go for a selection of valid and
invalid numbers and the respective error messages.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: Ic8febc7bd4dc5186b16a8c8897691e81125cf0ca
Reviewed-on: https://go-review.googlesource.com/c/157677
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

go/scanner: accept new Go2 number literals
This CL introduces go/scanner support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.
The new code is closely mirroring the respective code for number literals in
cmd/compile/internal/syntax/scanner.go.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I5315c6aaa7cfc41a618296be20e3acd5114d6b3c
Reviewed-on: https://go-review.googlesource.com/c/159997
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

cmd/gofmt: test that Go 2 number literals can be formatted
R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: Icd25aa7f6e18ed671ea6cf2b1b292899daf4b1a5
Reviewed-on: https://go-review.googlesource.com/c/160018
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

go/constant: accept new Go2 number literals
This CL introduces go/constant support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I7a55f91b8b6373ae6d98ba923b626d33c5552946
Reviewed-on: https://go-review.googlesource.com/c/160239
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

go/types: add tests for new Go 2 number literals
This CL ensures that go/types can now handle the new
Go 2 number literals. The relevant changes enabling
them in go/types were made in go/constant in the CL
https://golang.org/cl/160239.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I45c1387198fac94769ac59c5301d86b4e1a1ff98
Reviewed-on: https://go-review.googlesource.com/c/160240
Reviewed-by: Russ Cox <rsc@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

cmd/gofmt: normalize number prefixes and exponents
Rewrite non-decimal number prefixes to always use a lower-case base
("0X" -> "0x", etc.), and rewrite exponents to use a lower-case 'e'
or 'p'. Leave hexadecimal digits and 0-octals alone.

Comparing the best time of 3 runs of `time go test -run All` with
the time for a gofmt that doesn't do the rewrite shows no increase
in runtime for this bulk gofmt application (in fact on my machine
I see a small decline, probably due to cache effects).

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#29008.

Change-Id: I9c6ebed2ffa0a6a001c59412a73382090955f5a9
Reviewed-on: https://go-review.googlesource.com/c/160184
Reviewed-by: Ian Lance Taylor <iant@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

text/scanner: accept new Go2 number literals
This CL introduces text/scanner support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.
The new code is closely mirroring the respective code for number literals in
cmd/compile/internal/syntax/scanner.go.

Uniformly use the term "invalid" rather than "illegal" in error messages
to match the respective error messages in the other scanners directly.

R=Go1.13

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Change-Id: I2f291de13ba5afc0e530cd8326e6bf4c3858ebac
Reviewed-on: https://go-review.googlesource.com/c/161199
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 20, 2019

strconv: add 0b, 0o integer prefixes in ParseInt, ParseUint
This CL modifies ParseInt and ParseUint to recognize
0b and 0o as binary and octal base prefixes when base == 0.

See golang.org/design/19308-number-literals for background.

For golang#19308.
For golang#12711.

Change-Id: I8efe067f415aa517bdefbff7e230d3fa1694d530
Reviewed-on: https://go-review.googlesource.com/c/160244
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Feb 20, 2019

Change https://golang.org/cl/163079 mentions this issue: text/scanner: don't liberally consume (invalid) floats or underbars

gopherbot pushed a commit that referenced this issue Feb 20, 2019

text/scanner: don't liberally consume (invalid) floats or underbars
This is a follow-up on https://golang.org/cl/161199 which introduced
the new Go 2 number literals to text/scanner.

That change introduced a bug by allowing decimal and hexadecimal floats
to be consumed even if the scanner was not configured to accept floats.

This CL changes the code to not consume a radix dot '.' or exponent
unless the scanner is configured to accept floats.

This CL also introduces a new mode "AllowNumberbars" which controls
whether underbars '_' are permitted as digit separators in numbers
or not.

There is a possibility that we may need to refine text/scanner
further (e.g., the Float mode now includes hexadecimal floats
which it didn't recognize before). We're very early in the cycle,
so let's see how it goes.

RELNOTE=yes

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Fixes #30320.

Change-Id: I6481d314f0384e09ef6803ffad38dc529b1e89a3
Reviewed-on: https://go-review.googlesource.com/c/163079
Reviewed-by: Ian Lance Taylor <iant@golang.org>

nebulabox added a commit to nebulabox/go that referenced this issue Feb 21, 2019

text/scanner: don't liberally consume (invalid) floats or underbars
This is a follow-up on https://golang.org/cl/161199 which introduced
the new Go 2 number literals to text/scanner.

That change introduced a bug by allowing decimal and hexadecimal floats
to be consumed even if the scanner was not configured to accept floats.

This CL changes the code to not consume a radix dot '.' or exponent
unless the scanner is configured to accept floats.

This CL also introduces a new mode "AllowNumberbars" which controls
whether underbars '_' are permitted as digit separators in numbers
or not.

There is a possibility that we may need to refine text/scanner
further (e.g., the Float mode now includes hexadecimal floats
which it didn't recognize before). We're very early in the cycle,
so let's see how it goes.

RELNOTE=yes

Updates golang#12711.
Updates golang#19308.
Updates golang#28493.
Updates golang#29008.

Fixes golang#30320.

Change-Id: I6481d314f0384e09ef6803ffad38dc529b1e89a3
Reviewed-on: https://go-review.googlesource.com/c/163079
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment