mirror of
https://github.com/golang/go
synced 2024-11-24 21:50:11 -07:00
text/scanner: rename AllowNumberbars to AllowDigitSeparators
Fixes #32661. Change-Id: I32dc4e7b276b95ac2e87a384caa6c48702368d05 Reviewed-on: https://go-review.googlesource.com/c/go/+/183077 Reviewed-by: Andrew Bonventre <andybons@golang.org>
This commit is contained in:
parent
8456b07099
commit
bc81f52e93
@ -59,16 +59,16 @@ func (pos Position) String() string {
|
|||||||
// "foo" is scanned as the token sequence '"' Ident '"'.
|
// "foo" is scanned as the token sequence '"' Ident '"'.
|
||||||
//
|
//
|
||||||
const (
|
const (
|
||||||
ScanIdents = 1 << -Ident
|
ScanIdents = 1 << -Ident
|
||||||
ScanInts = 1 << -Int
|
ScanInts = 1 << -Int
|
||||||
ScanFloats = 1 << -Float // includes Ints and hexadecimal floats
|
ScanFloats = 1 << -Float // includes Ints and hexadecimal floats
|
||||||
ScanChars = 1 << -Char
|
ScanChars = 1 << -Char
|
||||||
ScanStrings = 1 << -String
|
ScanStrings = 1 << -String
|
||||||
ScanRawStrings = 1 << -RawString
|
ScanRawStrings = 1 << -RawString
|
||||||
ScanComments = 1 << -Comment
|
ScanComments = 1 << -Comment
|
||||||
SkipComments = 1 << -skipComment // if set with ScanComments, comments become white space
|
SkipComments = 1 << -skipComment // if set with ScanComments, comments become white space
|
||||||
AllowNumberbars = 1 << -allowNumberbars // if set, number literals may contain underbars as digit separators
|
AllowDigitSeparators = 1 << -allowDigitSeparators // if set, number literals may contain underscores as digit separators
|
||||||
GoTokens = ScanIdents | ScanFloats | ScanChars | ScanStrings | ScanRawStrings | ScanComments | SkipComments | AllowNumberbars
|
GoTokens = ScanIdents | ScanFloats | ScanChars | ScanStrings | ScanRawStrings | ScanComments | SkipComments | AllowDigitSeparators
|
||||||
)
|
)
|
||||||
|
|
||||||
// The result of Scan is one of these tokens or a Unicode character.
|
// The result of Scan is one of these tokens or a Unicode character.
|
||||||
@ -84,7 +84,7 @@ const (
|
|||||||
|
|
||||||
// internal use only
|
// internal use only
|
||||||
skipComment
|
skipComment
|
||||||
allowNumberbars
|
allowDigitSeparators
|
||||||
)
|
)
|
||||||
|
|
||||||
var tokenString = map[rune]string{
|
var tokenString = map[rune]string{
|
||||||
@ -363,8 +363,8 @@ func lower(ch rune) rune { return ('a' - 'A') | ch } // returns lower-case c
|
|||||||
func isDecimal(ch rune) bool { return '0' <= ch && ch <= '9' }
|
func isDecimal(ch rune) bool { return '0' <= ch && ch <= '9' }
|
||||||
func isHex(ch rune) bool { return '0' <= ch && ch <= '9' || 'a' <= lower(ch) && lower(ch) <= 'f' }
|
func isHex(ch rune) bool { return '0' <= ch && ch <= '9' || 'a' <= lower(ch) && lower(ch) <= 'f' }
|
||||||
|
|
||||||
// digits accepts the sequence { digit } (if AllowNumberbars is not set)
|
// digits accepts the sequence { digit } (if AllowDigitSeparators is not set)
|
||||||
// or { digit | '_' } (if AllowNumberbars is set), starting with ch0.
|
// or { digit | '_' } (if AllowDigitSeparators is set), starting with ch0.
|
||||||
// If base <= 10, digits accepts any decimal digit but records
|
// If base <= 10, digits accepts any decimal digit but records
|
||||||
// the first invalid digit >= base in *invalid if *invalid == 0.
|
// the first invalid digit >= base in *invalid if *invalid == 0.
|
||||||
// digits returns the first rune that is not part of the sequence
|
// digits returns the first rune that is not part of the sequence
|
||||||
@ -374,7 +374,7 @@ func (s *Scanner) digits(ch0 rune, base int, invalid *rune) (ch rune, digsep int
|
|||||||
ch = ch0
|
ch = ch0
|
||||||
if base <= 10 {
|
if base <= 10 {
|
||||||
max := rune('0' + base)
|
max := rune('0' + base)
|
||||||
for isDecimal(ch) || ch == '_' && s.Mode&AllowNumberbars != 0 {
|
for isDecimal(ch) || ch == '_' && s.Mode&AllowDigitSeparators != 0 {
|
||||||
ds := 1
|
ds := 1
|
||||||
if ch == '_' {
|
if ch == '_' {
|
||||||
ds = 2
|
ds = 2
|
||||||
@ -385,7 +385,7 @@ func (s *Scanner) digits(ch0 rune, base int, invalid *rune) (ch rune, digsep int
|
|||||||
ch = s.next()
|
ch = s.next()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for isHex(ch) || ch == '_' && s.Mode&AllowNumberbars != 0 {
|
for isHex(ch) || ch == '_' && s.Mode&AllowDigitSeparators != 0 {
|
||||||
ds := 1
|
ds := 1
|
||||||
if ch == '_' {
|
if ch == '_' {
|
||||||
ds = 2
|
ds = 2
|
||||||
|
@ -887,7 +887,7 @@ func TestIssue30320(t *testing.T) {
|
|||||||
{"foo0/12/0/5.67", "0 12 0 5 67", ScanInts},
|
{"foo0/12/0/5.67", "0 12 0 5 67", ScanInts},
|
||||||
{"xxx1e0yyy", "1 0", ScanInts},
|
{"xxx1e0yyy", "1 0", ScanInts},
|
||||||
{"1_2", "1 2", ScanInts}, // don't consume _ as part of a number if not explicitly enabled
|
{"1_2", "1 2", ScanInts}, // don't consume _ as part of a number if not explicitly enabled
|
||||||
{"1_2", "1_2", ScanInts | AllowNumberbars},
|
{"1_2", "1_2", ScanInts | AllowDigitSeparators},
|
||||||
{"xxx1.0yyy2e3ee", "1 0 2 3", ScanInts},
|
{"xxx1.0yyy2e3ee", "1 0 2 3", ScanInts},
|
||||||
{"xxx1.0yyy2e3ee", "1.0 2e3", ScanFloats},
|
{"xxx1.0yyy2e3ee", "1.0 2e3", ScanFloats},
|
||||||
} {
|
} {
|
||||||
|
Loading…
Reference in New Issue
Block a user