-
Notifications
You must be signed in to change notification settings - Fork 3
/
context_path.go
executable file
·51 lines (45 loc) · 1014 Bytes
/
context_path.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
package httpKit
import (
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"path"
)
// PolyfillContextPath
/**
* e.g.
* "" => ""
* "/" => "/"
* "//" => "/"
* "///" => "/"
* "/c/////c//" => "/c/c/"
*
* @return 优化过的ContextPath
*/
func PolyfillContextPath(relativePath string) string {
rst := joinPaths("", strKit.TrimSpace(relativePath))
switch rst {
case "":
case "/":
default:
rst = strKit.PrependIfMissing(rst, "/")
rst = strKit.AppendIfMissing(rst, "/")
}
return rst
}
// joinPaths
// 参考:github.com/gin-gonic/gin routergroup.go calculateAbsolutePath()
func joinPaths(absolutePath, relativePath string) string {
if relativePath == "" {
return absolutePath
}
finalPath := path.Join(absolutePath, relativePath)
if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
return finalPath + "/"
}
return finalPath
}
func lastChar(str string) byte {
if str == "" {
panic("The length of the string can't be 0")
}
return str[len(str)-1]
}