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
|
# File 'lib/logfmt/parser.rb', line 16
def self.parse(line)
output = {}
key, value = "", ""
escaped = false
state = GARBAGE
i = 0
line.each_char do |c|
i += 1
if state == GARBAGE
if c > ' ' && c != '"' && c != '='
key = c
state = KEY
end
next
end
if state == KEY
if c > ' ' && c != '"' && c != '='
state = KEY
key << c
elsif c == '='
output[key.strip()] = true
state = EQUAL
else
output[key.strip()] = true
state = GARBAGE
end
if i >= line.length
output[key.strip()] = true
end
next
end
if state == EQUAL
if c > ' ' && c != '"' && c != '='
value = c
state = IVALUE
elsif c == '"'
value = ""
escaped = false
state = QVALUE
else
state = GARBAGE
end
if i >= line.length
if integer?(value)
value = Integer(value)
elsif numeric?(value)
value = Float(value)
end
output[key.strip()] = value || true
end
next
end
if state == IVALUE
if not (c > ' ' && c != '"' && c != '=')
if integer?(value)
value = Integer(value)
elsif numeric?(value)
value = Float(value)
end
output[key.strip()] = value
state = GARBAGE
else
value << c
end
if i >= line.length
if integer?(value)
value = Integer(value)
elsif numeric?(value)
value = Float(value)
end
output[key.strip()] = value
end
next
end
if state == QVALUE
if c == '\\'
escaped = true
value << "\\"
elsif c == '"'
if escaped
escaped = false
value << c
next
end
output[key.strip()] = value
state = GARBAGE
else
escaped = false
value << c
end
next
end
end
output
end
|