forked from alloy-d/goauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
52 lines (47 loc) · 1022 Bytes
/
url.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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package oauth
func shouldEscape(c byte) bool {
switch {
case c >= 0x41 && c <= 0x5A:
return false
case c >= 0x61 && c <= 0x7A:
return false
case c >= 0x30 && c <= 0x39:
return false
case c == '-', c == '.', c == '_', c == '~':
return false
}
return true
}
//Performs percent-encoding as specified by RFC 5849.
//
//Can be reversed with http.URLUnescape.
func PercentEncode(s string) string {
spaceCount, hexCount := 0, 0
for i := 0; i < len(s); i++ {
c := s[i]
if shouldEscape(c) {
hexCount++
}
}
if spaceCount == 0 && hexCount == 0 {
return s
}
t := make([]byte, len(s)+2*hexCount)
j := 0
for i := 0; i < len(s); i++ {
switch c := s[i]; {
case shouldEscape(c):
t[j] = '%'
t[j+1] = "0123456789ABCDEF"[c>>4]
t[j+2] = "0123456789ABCDEF"[c&15]
j += 3
default:
t[j] = s[i]
j++
}
}
return string(t)
}