-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.go
More file actions
49 lines (42 loc) · 907 Bytes
/
csv.go
File metadata and controls
49 lines (42 loc) · 907 Bytes
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
package HLS
import (
"errors"
"strings"
)
var (
InvalidCSV error = errors.New("Invalid CSV")
)
type csvs []string
// ParseCSV parses the csv as comma separated values.
// Returned []string value is in left to right as in csv string.
func ParseCSV(csv string) (csvs, error) {
tokens := make(csvs, 0, 1)
if len(csv) < 1 {
return tokens, InvalidCSV
} else if csv[len(csv)-1] != ',' {
csv += string(',')
}
var quote bool
var comma int
for i, char := range csv {
if char == '"' {
quote = !quote
} else if !quote && char == ' ' {
return tokens, InvalidCSV
} else if !quote && char == ',' {
token := strings.TrimSpace(csv[comma:i])
comma = i + 1
if len(token) < 1 {
return tokens, InvalidCSV
}
tokens = append(tokens, token)
}
}
if quote {
return tokens, InvalidCSV
}
return tokens, nil
}
func (cb csvs) String() string {
return strings.Join(cb, ",")
}