Simplify scanFollows usage (#510)

Use static functions to avoid declaring global vars and creating more
package init costs.  This change has no negative effects on benchmarks
in my testing.
This commit is contained in:
Cameron Moore
2021-04-15 15:48:19 -05:00
committed by GitHub
parent 9bc4641a49
commit 24b62ebe61
+26 -18
View File
@@ -2,26 +2,34 @@ package toml
import "fmt" import "fmt"
func scanFollows(pattern []byte) func(b []byte) bool { func scanFollows(b []byte, pattern string) bool {
return func(b []byte) bool { n := len(pattern)
if len(b) < len(pattern) { return len(b) >= n && string(b[:n]) == pattern
return false
}
for i, c := range pattern {
if b[i] != c {
return false
}
}
return true
}
} }
var scanFollowsMultilineBasicStringDelimiter = scanFollows([]byte{'"', '"', '"'}) func scanFollowsMultilineBasicStringDelimiter(b []byte) bool {
var scanFollowsMultilineLiteralStringDelimiter = scanFollows([]byte{'\'', '\'', '\''}) return scanFollows(b, `"""`)
var scanFollowsTrue = scanFollows([]byte{'t', 'r', 'u', 'e'}) }
var scanFollowsFalse = scanFollows([]byte{'f', 'a', 'l', 's', 'e'})
var scanFollowsInf = scanFollows([]byte{'i', 'n', 'f'}) func scanFollowsMultilineLiteralStringDelimiter(b []byte) bool {
var scanFollowsNan = scanFollows([]byte{'n', 'a', 'n'}) return scanFollows(b, `'''`)
}
func scanFollowsTrue(b []byte) bool {
return scanFollows(b, `true`)
}
func scanFollowsFalse(b []byte) bool {
return scanFollows(b, `false`)
}
func scanFollowsInf(b []byte) bool {
return scanFollows(b, `inf`)
}
func scanFollowsNan(b []byte) bool {
return scanFollows(b, `nan`)
}
func scanUnquotedKey(b []byte) ([]byte, []byte, error) { func scanUnquotedKey(b []byte) ([]byte, []byte, error) {
// unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _ // unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _