1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-20 22:52:53 +00:00
fiber/helpers.go
2020-01-06 18:38:39 -05:00

94 lines
1.9 KiB
Go

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))
}