-
Notifications
You must be signed in to change notification settings - Fork 380
/
quotes.go
51 lines (46 loc) · 1.29 KB
/
quotes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package models
import (
"encoding/csv"
"strings"
)
// IsQuoted returns true if the string starts and ends with a double quote.
func IsQuoted(s string) bool {
if s == "" {
return false
}
if len(s) < 2 {
return false
}
if s[0] == '"' && s[len(s)-1] == s[0] {
return true
}
return false
}
// StripQuotes returns the string with the starting and ending quotes removed.
func StripQuotes(s string) string {
if IsQuoted(s) {
return s[1 : len(s)-1]
}
return s
}
// ParseQuotedTxt returns the individual strings of a combined quoted string.
// `foo` -> []string{"foo"}
// `"foo"` -> []string{"foo"}
// `"foo" "bar"` -> []string{"foo", "bar"}
// NOTE: it is assumed there is exactly one space between the quotes.
func ParseQuotedTxt(s string) []string {
if !IsQuoted(s) {
return []string{s}
}
return strings.Split(StripQuotes(s), `" "`)
}
// ParseQuotedFields is like strings.Fields except individual fields
// might be quoted using `"`.
func ParseQuotedFields(s string) ([]string, error) {
// Fields are space-separated but a field might be quoted. This is,
// essentially, a CSV where spaces are the field separator (not
// commas). Therefore, we use the CSV parser. See https://stackoverflow.com/a/47489846/71978
r := csv.NewReader(strings.NewReader(s))
r.Comma = ' ' // space
return r.Read()
}