forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
140 lines (116 loc) · 2.62 KB
/
encode.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
package strutil
import (
"bytes"
"encoding/base32"
"encoding/base64"
"net/url"
"strings"
"text/template"
)
//
// -------------------- escape --------------------
//
// EscapeJS escape javascript string
func EscapeJS(s string) string {
return template.JSEscapeString(s)
}
// EscapeHTML escape html string
func EscapeHTML(s string) string {
return template.HTMLEscapeString(s)
}
// AddSlashes add slashes for the string.
func AddSlashes(s string) string {
if ln := len(s); ln == 0 {
return ""
}
var buf bytes.Buffer
for _, char := range s {
switch char {
case '\'', '"', '\\':
buf.WriteRune('\\')
}
buf.WriteRune(char)
}
return buf.String()
}
// StripSlashes strip slashes for the string.
func StripSlashes(s string) string {
ln := len(s)
if ln == 0 {
return ""
}
var skip bool
var buf bytes.Buffer
for i, char := range s {
if skip {
skip = false
} else if char == '\\' {
if i+1 < ln && s[i+1] == '\\' {
skip = true
}
continue
}
buf.WriteRune(char)
}
return buf.String()
}
//
// -------------------- encode --------------------
//
// URLEncode encode url string.
func URLEncode(s string) string {
if pos := strings.IndexRune(s, '?'); pos > -1 { // escape query data
return s[0:pos+1] + url.QueryEscape(s[pos+1:])
}
return s
}
// URLDecode decode url string.
func URLDecode(s string) string {
if pos := strings.IndexRune(s, '?'); pos > -1 { // un-escape query data
qy, err := url.QueryUnescape(s[pos+1:])
if err == nil {
return s[0:pos+1] + qy
}
}
return s
}
//
// -------------------- base encode --------------------
//
// base32 encoding with no padding
var (
B32Std = base32.StdEncoding.WithPadding(base32.NoPadding)
B32Hex = base32.HexEncoding.WithPadding(base32.NoPadding)
)
// B32Encode base32 encode
func B32Encode(str string) string {
return B32Std.EncodeToString([]byte(str))
}
// B32Decode base32 decode
func B32Decode(str string) string {
dec, _ := B32Std.DecodeString(str)
return string(dec)
}
// B64Std base64 encoding with no padding
var B64Std = base64.StdEncoding.WithPadding(base64.NoPadding)
// B64Encode base64 encode
func B64Encode(str string) string {
return B64Std.EncodeToString([]byte(str))
}
// B64EncodeBytes base64 encode
func B64EncodeBytes(src []byte) []byte {
buf := make([]byte, B64Std.EncodedLen(len(src)))
B64Std.Encode(buf, src)
return buf
}
// B64Decode base64 decode
func B64Decode(str string) string {
dec, _ := B64Std.DecodeString(str)
return string(dec)
}
// B64DecodeBytes base64 decode
func B64DecodeBytes(str []byte) []byte {
dbuf := make([]byte, B64Std.DecodedLen(len(str)))
n, _ := B64Std.Decode(dbuf, str)
return dbuf[:n]
}