mirror of
https://github.com/beego/bee.git
synced 2024-11-30 03:11:28 +00:00
64 lines
1.0 KiB
Go
64 lines
1.0 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"unicode"
|
||
|
)
|
||
|
|
||
|
// Like strings.Fields but ignores spaces inside areas surrounded
|
||
|
// by the specified quote character.
|
||
|
// To specify a single quote use backslash to escape it: '\''
|
||
|
func SplitQuotedFields(in string, quote rune) []string {
|
||
|
type stateEnum int
|
||
|
const (
|
||
|
inSpace stateEnum = iota
|
||
|
inField
|
||
|
inQuote
|
||
|
inQuoteEscaped
|
||
|
)
|
||
|
state := inSpace
|
||
|
r := []string{}
|
||
|
var buf bytes.Buffer
|
||
|
|
||
|
for _, ch := range in {
|
||
|
switch state {
|
||
|
case inSpace:
|
||
|
if ch == quote {
|
||
|
state = inQuote
|
||
|
} else if !unicode.IsSpace(ch) {
|
||
|
buf.WriteRune(ch)
|
||
|
state = inField
|
||
|
}
|
||
|
|
||
|
case inField:
|
||
|
if ch == quote {
|
||
|
state = inQuote
|
||
|
} else if unicode.IsSpace(ch) {
|
||
|
r = append(r, buf.String())
|
||
|
buf.Reset()
|
||
|
} else {
|
||
|
buf.WriteRune(ch)
|
||
|
}
|
||
|
|
||
|
case inQuote:
|
||
|
if ch == quote {
|
||
|
state = inField
|
||
|
} else if ch == '\\' {
|
||
|
state = inQuoteEscaped
|
||
|
} else {
|
||
|
buf.WriteRune(ch)
|
||
|
}
|
||
|
|
||
|
case inQuoteEscaped:
|
||
|
buf.WriteRune(ch)
|
||
|
state = inQuote
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if buf.Len() != 0 {
|
||
|
r = append(r, buf.String())
|
||
|
}
|
||
|
|
||
|
return r
|
||
|
}
|