-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
56 lines (46 loc) · 1014 Bytes
/
parse.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
package envStore
import (
"bytes"
"errors"
"strings"
)
const (
MaxLineLen = 1 << 18
)
var (
CommentParseErr error = errors.New("Line is commented out.")
NoKeyParseErr error = errors.New("Key must be provided.")
NoValParseErr error = errors.New("Value must be provided.")
MaxLineLenParseErr error = errors.New("Line length is greater than the maximum allowed.")
)
func parseLine(line string) (key, value string, err error) {
line = strings.TrimSpace(line)
if len(line) > MaxLineLen {
err = MaxLineLenParseErr
return
}
var buffer bytes.Buffer
var metEquals bool
for _, r := range line {
if r == '=' && !metEquals {
metEquals = true
if buffer.Len() == 0 {
break
}
key = string(bytes.ToUpper(buffer.Bytes()))
buffer.Reset()
continue
}
buffer.WriteRune(r)
}
if key == "" {
err = NoKeyParseErr
} else if key[0] == '#' {
err = CommentParseErr
} else if buffer.Len() == 0 {
err = NoValParseErr
} else {
value = buffer.String()
}
return
}