...
1
2
3
4
5 package objabi
6
7 import "strings"
8
9
10
11
12
13
14 func PathToPrefix(s string) string {
15 slash := strings.LastIndex(s, "/")
16
17 n := 0
18 for r := 0; r < len(s); r++ {
19 if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F {
20 n++
21 }
22 }
23
24
25 if n == 0 {
26 return s
27 }
28
29
30 const hex = "0123456789abcdef"
31 p := make([]byte, 0, len(s)+2*n)
32 for r := 0; r < len(s); r++ {
33 if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F {
34 p = append(p, '%', hex[c>>4], hex[c&0xF])
35 } else {
36 p = append(p, c)
37 }
38 }
39
40 return string(p)
41 }
42
View as plain text