/
url.go
58 lines (50 loc) · 1.16 KB
/
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
53
54
55
56
57
58
package decode
import (
"bytes"
"net/url"
)
const urlname = "url"
// URL takes an input string
type URL struct {
input string
}
// NewURLCodecC state machine to smartly decode a string with invalid chars
func NewURLCodecC(in string) CodecC {
return &URL{
input: in,
}
}
// Name returns the name of the codec
func (b *URL) Name() string {
return urlname
}
// Decode a valid url encoded string
func (b *URL) Decode() (output string) {
res, err := url.PathUnescape(b.input)
if err != nil {
return "Not a valid URL encoded string"
}
return res
}
// Encode a string to url encode
func (b *URL) Encode() (output string) {
return url.PathEscape(b.input)
}
// Check returns the percentage of valid url characters in the input string
func (b *URL) Check() (acceptability float64) {
var c int
var tot int
for pos, char := range b.input {
tot++
if bytes.ContainsRune([]byte("%"), char) && pos < len(b.input)+2 {
if bytes.ContainsAny([]byte(b16Alphabet), string(b.input[pos+1])) &&
bytes.ContainsAny([]byte(b16Alphabet), string(b.input[pos+2])) {
c++
}
}
if bytes.ContainsRune([]byte(b64Alphabet), char) {
c++
}
}
return float64(c) / float64(tot)
}