Remove underscore regexps (#448)
* Remove underscore regexps Fixes #440. ``` benchmark old ns/op new ns/op delta BenchmarkUnmarshalToml-8 269582 257032 -4.66% benchmark old allocs new allocs delta BenchmarkUnmarshalToml-8 2650 2650 +0.00% benchmark old bytes new bytes delta BenchmarkUnmarshalToml-8 127761 127030 -0.57% ```
This commit is contained in:
@@ -1,19 +1,2 @@
|
|||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
|
||||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
|
|
||||||
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
|
|
||||||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
|
||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -231,19 +230,38 @@ func (p *tomlParser) parseAssign() tomlParserStateFn {
|
|||||||
return p.parseStart
|
return p.parseStart
|
||||||
}
|
}
|
||||||
|
|
||||||
var numberUnderscoreInvalidRegexp *regexp.Regexp
|
var errInvalidUnderscore = errors.New("invalid use of _ in number")
|
||||||
var hexNumberUnderscoreInvalidRegexp *regexp.Regexp
|
|
||||||
|
|
||||||
func numberContainsInvalidUnderscore(value string) error {
|
func numberContainsInvalidUnderscore(value string) error {
|
||||||
if numberUnderscoreInvalidRegexp.MatchString(value) {
|
// For large numbers, you may use underscores between digits to enhance
|
||||||
return errors.New("invalid use of _ in number")
|
// readability. Each underscore must be surrounded by at least one digit on
|
||||||
|
// each side.
|
||||||
|
|
||||||
|
hasBefore := false
|
||||||
|
for idx, r := range value {
|
||||||
|
if r == '_' {
|
||||||
|
if !hasBefore || idx+1 >= len(value) {
|
||||||
|
// can't end with an underscore
|
||||||
|
return errInvalidUnderscore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hasBefore = isDigit(r)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errInvalidUnderscoreHex = errors.New("invalid use of _ in hex number")
|
||||||
|
|
||||||
func hexNumberContainsInvalidUnderscore(value string) error {
|
func hexNumberContainsInvalidUnderscore(value string) error {
|
||||||
if hexNumberUnderscoreInvalidRegexp.MatchString(value) {
|
hasBefore := false
|
||||||
return errors.New("invalid use of _ in hex number")
|
for idx, r := range value {
|
||||||
|
if r == '_' {
|
||||||
|
if !hasBefore || idx+1 >= len(value) {
|
||||||
|
// can't end with an underscore
|
||||||
|
return errInvalidUnderscoreHex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hasBefore = isHexDigit(r)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -488,8 +506,3 @@ func parseToml(flow []token) *Tree {
|
|||||||
parser.run()
|
parser.run()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
numberUnderscoreInvalidRegexp = regexp.MustCompile(`([^\d]_|_[^\d])|_$|^_`)
|
|
||||||
hexNumberUnderscoreInvalidRegexp = regexp.MustCompile(`(^0x_)|([^\da-f]_|_[^\da-f])|_$|^_`)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user