-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringutil.go
58 lines (51 loc) · 1.04 KB
/
stringutil.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
52
53
54
55
56
57
58
package chart
import "strings"
// SplitCSV splits a corpus by the `,`, dropping leading or trailing whitespace unless quoted.
func SplitCSV(text string) (output []string) {
if len(text) == 0 {
return
}
var state int
var word []rune
var opened rune
for _, r := range text {
switch state {
case 0: // word
switch {
case isQuote(r):
opened = r
state = 1
case isCSVDelim(r):
output = append(output, strings.TrimSpace(string(word)))
word = nil
default:
word = append(word, r)
}
case 1: // we're in a quoted section
if matchesQuote(opened, r) {
state = 0
continue
}
word = append(word, r)
}
}
if len(word) > 0 {
output = append(output, strings.TrimSpace(string(word)))
}
return
}
func isCSVDelim(r rune) bool {
return r == rune(',')
}
func isQuote(r rune) bool {
return r == '"' || r == '\'' || r == '“' || r == '”' || r == '`'
}
func matchesQuote(a, b rune) bool {
if a == '“' && b == '”' {
return true
}
if a == '”' && b == '“' {
return true
}
return a == b
}