forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyval.go
79 lines (65 loc) · 1.26 KB
/
keyval.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
package headers
import (
"fmt"
)
func readKey(origstr string, str string, separator byte) (string, string) {
i := 0
for {
if i >= len(str) || str[i] == '=' || str[i] == separator {
break
}
i++
}
return str[:i], str[i:]
}
func readValue(origstr string, str string, separator byte) (string, string, error) {
if len(str) > 0 && str[0] == '"' {
i := 1
for {
if i >= len(str) {
return "", "", fmt.Errorf("apexes not closed (%v)", origstr)
}
if str[i] == '"' {
return str[1:i], str[i+1:], nil
}
i++
}
}
i := 0
for {
if i >= len(str) || str[i] == separator {
return str[:i], str[i:], nil
}
i++
}
}
func keyValParse(str string, separator byte) (map[string]string, error) {
ret := make(map[string]string)
origstr := str
for len(str) > 0 {
var k string
k, str = readKey(origstr, str, separator)
if len(k) > 0 {
if len(str) > 0 && str[0] == '=' {
var v string
var err error
v, str, err = readValue(origstr, str[1:], separator)
if err != nil {
return nil, err
}
ret[k] = v
} else {
ret[k] = ""
}
}
// skip separator
if len(str) > 0 && str[0] == separator {
str = str[1:]
}
// skip spaces
for len(str) > 0 && str[0] == ' ' {
str = str[1:]
}
}
return ret, nil
}