-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
184 lines (164 loc) · 2.74 KB
/
utils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package rego
import (
"encoding/xml"
"log"
"math"
)
func Min(a,b int)int{
if b>=a{
return a
}
return b
}
func CheckErr(err error,msg string){
if err!=nil{
log.Fatalf("%s : %s",msg,err)
}
}
func PrintErr(format string,values ...interface{}){
log.Fatalf(format,values)
}
func ContainInt(a []int,b int)bool{
for _,v:=range a{
if v==b{
return true
}
}
return false
}
func cleanPath(p string) string {
if p == "" {
return "/"
}
n := len(p)
var buf []byte
r := 1
w := 1
if p[0] != '/' {
r = 0
buf = make([]byte, n+1)
buf[0] = '/'
}
trailing := n > 1 && p[n-1] == '/'
for r < n {
switch {
case p[r] == '/':
r++
case p[r] == '.' && r+1 == n:
trailing = true
r++
case p[r] == '.' && p[r+1] == '/':
r += 2
case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
r += 3
if w > 1 {
w--
if buf == nil {
for w > 1 && p[w] != '/' {
w--
}
} else {
for w > 1 && buf[w] != '/' {
w--
}
}
}
default:
if w > 1 {
bufApp(&buf, p, w, '/')
w++
}
for r < n && p[r] != '/' {
bufApp(&buf, p, w, p[r])
w++
r++
}
}
}
if trailing && w > 1 {
bufApp(&buf, p, w, '/')
w++
}
if buf == nil {
return p[:w]
}
return string(buf[:w])
}
func bufApp(buf *[]byte, s string, w int, c byte) {
if *buf == nil {
if s[w] == c {
return
}
*buf = make([]byte, len(s))
copy(*buf, s[:w])
}
(*buf)[w] = c
}
func GetAllKeys(m map[string]HandlerChain)(result []string){
keys:=make([]string,len(m))
j:=0
for k:=range m{
keys[j]=k
j++
}
return keys
}
func GetParamAllKeys(m map[string]ParamMap)(result []string){
keys:=make([]string,len(m))
j:=0
for k:=range m{
keys[j]=k
j++
}
return keys
}
func IsKeyExist(m map[string]HandlerChain,key string)bool{
for k:=range m{
if k==key{
return true
}
}
return false
}
type H map[string]interface{}
func (h H) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{
Space: "",
Local: "map",
}
if err := encoder.EncodeToken(start); err != nil {
CheckErr(err,"encode the token failed")
return err
}
for key, value := range h {
element := xml.StartElement{
Name: xml.Name{Space: "", Local: key},
Attr: []xml.Attr{},
}
if err := encoder.EncodeElement(value, element); err != nil {
CheckErr(err,"encode the element failed")
return err
}
}
return encoder.EncodeToken(xml.EndElement{Name: start.Name})
}
func ParseIntToBin(i int)(b []bool){
b = make([]bool,8)
for pos:=7;i>0;pos,i=pos-1,i/2{
flag:=false
if i%2==1{
flag=true
}
b[pos]=flag
}
return
}
func ParseBinToInt(b []bool)(i int){
i=0
for pos,j:=0,len(b)-1;j>-1;j,pos=j-1,pos+1{
if b[j]{
i+=int(math.Pow(float64(2),float64(pos)))
}
}
return
}