forked from gofiber/fiber
/
helpers.go
93 lines (85 loc) 路 1.88 KB
/
helpers.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
package fiber
import (
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"unsafe"
)
var replacer = strings.NewReplacer(":", "", "?", "")
func getParams(path string) (params []string) {
segments := strings.Split(path, "/")
for _, s := range segments {
if s == "" {
continue
}
if strings.Contains(s, ":") {
s = replacer.Replace(s)
params = append(params, s)
continue
}
if strings.Contains(s, "*") {
params = append(params, "*")
}
}
return params
}
func getRegex(path string) (*regexp.Regexp, error) {
pattern := "^"
segments := strings.Split(path, "/")
for _, s := range segments {
if s == "" {
continue
}
if strings.Contains(s, ":") {
if strings.Contains(s, "?") {
pattern += "(?:/([^/]+?))?"
} else {
pattern += "/(?:([^/]+?))"
}
} else if strings.Contains(s, "*") {
pattern += "/(.*)"
} else {
pattern += "/" + s
}
}
pattern += "/?$"
regex, err := regexp.Compile(pattern)
return regex, err
}
func walk(root string) (files []string, dir bool, err error) {
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
if !strings.Contains(path, ".fasthttp.gz") {
files = append(files, path)
}
} else {
dir = true
}
return nil
})
return files, dir, err
}
// Credits to @savsgio
// https://github.com/savsgio/gotils/blob/master/conv.go
// b2s converts byte slice to a string without memory allocation.
func b2s(b []byte) string {
sh := (*reflect.StringHeader)(unsafe.Pointer(&b))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*string)(unsafe.Pointer(&bh))
}
// s2b converts string to a byte slice without memory allocation.
func s2b(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}