5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 20:40:27 +00:00
matterbridge/vendor/github.com/pelletier/go-toml/keysparsing.go

113 lines
2.4 KiB
Go
Raw Normal View History

2018-03-04 22:46:13 +00:00
// Parsing keys handling both bare and quoted keys.
package toml
import (
"errors"
"fmt"
)
// Convert the bare key group string to an array.
2020-08-09 22:29:54 +00:00
// The input supports double quotation and single quotation,
2018-03-04 22:46:13 +00:00
// but escape sequences are not supported. Lexers must unescape them beforehand.
func parseKey(key string) ([]string, error) {
2020-08-09 22:29:54 +00:00
runes := []rune(key)
var groups []string
2018-03-04 22:46:13 +00:00
2020-08-09 22:29:54 +00:00
if len(key) == 0 {
return nil, errors.New("empty key")
}
idx := 0
for idx < len(runes) {
for ; idx < len(runes) && isSpace(runes[idx]); idx++ {
// skip leading whitespace
2018-03-04 22:46:13 +00:00
}
2020-08-09 22:29:54 +00:00
if idx >= len(runes) {
break
}
r := runes[idx]
if isValidBareChar(r) {
// parse bare key
startIdx := idx
endIdx := -1
idx++
for idx < len(runes) {
r = runes[idx]
if isValidBareChar(r) {
idx++
} else if r == '.' {
endIdx = idx
break
} else if isSpace(r) {
endIdx = idx
for ; idx < len(runes) && isSpace(runes[idx]); idx++ {
// skip trailing whitespace
2018-03-04 22:46:13 +00:00
}
2020-08-09 22:29:54 +00:00
if idx < len(runes) && runes[idx] != '.' {
return nil, fmt.Errorf("invalid key character after whitespace: %c", runes[idx])
}
break
} else {
return nil, fmt.Errorf("invalid bare key character: %c", r)
}
}
if endIdx == -1 {
endIdx = idx
}
groups = append(groups, string(runes[startIdx:endIdx]))
} else if r == '\'' {
// parse single quoted key
idx++
startIdx := idx
for {
if idx >= len(runes) {
return nil, fmt.Errorf("unclosed single-quoted key")
2018-03-04 22:46:13 +00:00
}
2020-08-09 22:29:54 +00:00
r = runes[idx]
if r == '\'' {
groups = append(groups, string(runes[startIdx:idx]))
idx++
break
}
idx++
2018-03-04 22:46:13 +00:00
}
2020-08-09 22:29:54 +00:00
} else if r == '"' {
// parse double quoted key
idx++
startIdx := idx
for {
if idx >= len(runes) {
return nil, fmt.Errorf("unclosed double-quoted key")
}
r = runes[idx]
if r == '"' {
groups = append(groups, string(runes[startIdx:idx]))
idx++
break
}
idx++
2018-03-04 22:46:13 +00:00
}
2020-08-09 22:29:54 +00:00
} else if r == '.' {
idx++
if idx >= len(runes) {
return nil, fmt.Errorf("unexpected end of key")
2018-03-04 22:46:13 +00:00
}
2020-08-09 22:29:54 +00:00
r = runes[idx]
if !isValidBareChar(r) && r != '\'' && r != '"' && r != ' ' {
return nil, fmt.Errorf("expecting key part after dot")
2018-03-04 22:46:13 +00:00
}
2020-08-09 22:29:54 +00:00
} else {
return nil, fmt.Errorf("invalid key character: %c", r)
2018-03-04 22:46:13 +00:00
}
}
if len(groups) == 0 {
2020-08-09 22:29:54 +00:00
return nil, fmt.Errorf("empty key")
2018-03-04 22:46:13 +00:00
}
return groups, nil
}
func isValidBareChar(r rune) bool {
2020-10-19 21:40:00 +00:00
return isAlphanumeric(r) || r == '-' || isDigit(r)
2018-03-04 22:46:13 +00:00
}