This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
summer-utils/utils/quoted-string.go

37 lines
540 B
Go

package utils
func QuotedStringToArray(text string) []string {
var escape, quoted bool
out := make([]string, 0)
var a string
runeArr := []rune(text)
for _, char := range runeArr {
if escape {
a += string(char)
escape = false
continue
}
switch char {
case ' ':
if quoted {
a += " "
} else if a != "" {
out = append(out, a)
a = ""
}
case '\\':
escape = true
case '"':
quoted = !quoted
default:
a += string(char)
}
}
if a != "" {
out = append(out, a)
a = ""
}
return out
}