-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
141 lines (106 loc) · 2.08 KB
/
parser.go
File metadata and controls
141 lines (106 loc) · 2.08 KB
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
package otty
import (
"context"
"github.com/Oringik/otty/handlers"
)
// Otty ...
type Otty struct {
Handlers map[string]handlers.Handler
Endpoints map[string]func([]byte)
Ctx context.Context
}
// New returns pointer to new otty structure
func New() *Otty {
return &Otty{
Handlers: make(map[string]handlers.Handler),
Endpoints: make(map[string]func([]byte)),
}
}
// ParseOtty parsing any data and return structure with ready handlers and raw data
func (otty *Otty) ParseOtty(ctx context.Context, data []byte) {
otty.Handlers = handlers.InitHandlers()
otty.Ctx = ctx
for i := 0; i < len(data)-1; i++ {
if isSpace(data[i]) {
continue
}
if isEnd(data[i]) {
data = []byte{}
break
}
if isNewString(data[i]) {
if len(data) > i+1 {
data = otty.ParseHandler(data[i+1:])
i = 0
continue
}
}
data = otty.ParseHandler(data[i:])
i = 0
}
}
// ParseHandler parsing handlers for otty structure
func (otty *Otty) ParseHandler(data []byte) []byte {
handlersToSearch := otty.Handlers
var name []byte
var value []byte
// Parse name of handler
for i := 0; i < len(data)-1; i++ {
if isSpace(data[i]) {
continue
}
if isEnd(data[i]) {
data = []byte{}
break
}
if isColon(data[i]) {
data = data[i+1:]
break
}
name = append(name, data[i])
}
// Parse value of handler
for i := 0; i < len(data)-1; i++ {
if isSpace(data[i]) {
continue
}
if isEnd(data[i]) {
data = []byte{}
break
}
if isNewString(data[i]) {
data = data[i+1:]
break
}
value = append(value, data[i])
}
handler := handlers.FindHandlerByName(handlersToSearch, string(name))
// Set name and data for found handler
handler.SetName(name)
handler.SetValue(value)
return data
}
func isColon(symbol byte) bool {
if symbol == 58 {
return true
}
return false
}
func isEnd(symbol byte) bool {
if symbol == 0 {
return true
}
return false
}
func isSpace(symbol byte) bool {
if symbol == 32 || symbol == 9 {
return true
}
return false
}
func isNewString(symbol byte) bool {
if symbol == 10 {
return true
}
return false
}