-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributes.go
150 lines (127 loc) · 2.69 KB
/
attributes.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package latex
import (
"io"
"regexp"
"strings"
)
var measure = regexp.MustCompile("^(-?[0-9]*(?:\\.[0-9]+)?)(%|\\\\?[a-z ]*)$")
var whitespaces = regexp.MustCompile("[ \n\t\r]+")
type keyValueParserState int
const (
stateLookingForKey keyValueParserState = iota
stateReadingKey
stateKeyRead
stateLookingForValue
stateReadingValue
stateLookingForDelimiter
)
func KeyValue(raw string) (map[string]string, error) {
read := strings.NewReader(raw)
attr := map[string]string{}
key := ""
value := ""
state := stateLookingForKey
escape := rune(0)
for {
char, _, err := read.ReadRune()
if err == io.EOF {
if state == stateReadingValue {
attr[key] = value
}
return attr, nil
}
if err != nil {
return nil, err
}
switch state {
case stateLookingForKey:
if isValidAttributeNameChar(char) {
state = stateReadingKey
key = string(char)
}
continue
case stateReadingKey:
if isValidAttributeNameChar(char) {
key += string(char)
continue
}
switch {
case char == ' ':
state = stateKeyRead
key = strings.ToLower(key)
case char == '=':
state = stateLookingForValue
key = strings.ToLower(key)
default:
state = stateLookingForKey
key = ""
}
continue
case stateKeyRead:
switch {
case char == ' ':
case char == '=':
state = stateLookingForValue
default:
state = stateLookingForKey
key = ""
}
case stateLookingForValue:
if char == '"' || char == '\'' {
value = ""
state = stateReadingValue
escape = char
} else if char != ' ' {
value = string(char)
state = stateReadingValue
escape = 0
}
continue
case stateReadingValue:
if escape == 0 && char == ' ' {
attr[key] = value
state = stateLookingForDelimiter
continue
}
if escape == 0 && char == ',' {
attr[key] = value
state = stateLookingForKey
continue
}
if escape != 0 && char == '\\' {
next, _, err := read.ReadRune()
if err == io.EOF {
value += string(char)
attr[key] = value
return attr, nil
}
if err != nil {
return nil, err
}
if next == '\\' || next == '"' || next == escape {
value += string(next)
continue
}
if err := read.UnreadRune(); err != nil {
return nil, err
}
}
if escape != 0 && char == escape {
attr[key] = value
state = stateLookingForDelimiter
continue
}
value += string(char)
case stateLookingForDelimiter:
if char == ',' {
state = stateLookingForKey
}
}
}
}
func isValidAttributeNameChar(char rune) bool {
return (char >= 'a' && char <= 'z') ||
(char >= 'A' && char <= 'Z') ||
(char >= '0' && char <= '9') ||
(char == '-' || char == '_')
}