-
Notifications
You must be signed in to change notification settings - Fork 20
/
encode.go
56 lines (47 loc) · 1.29 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
package base1024
import (
"github.com/samber/lo"
"strings"
)
func EncodeToString(input []byte) string {
inputLength := len(input)
remainder := inputLength % 5
safe := inputLength - remainder
points := make([]int, 0)
for i := 0; i <= safe; i += 5 {
tmp := make([]int, 0)
if i+1 < inputLength {
tmp = append(tmp, (int(input[i])&0xff)<<2|(int(input[i+1])>>6))
} else if i+1 == inputLength {
tmp = append(tmp, (int(input[i])&0xff)<<2)
}
if i+2 < inputLength {
tmp = append(tmp, (int(input[i+1])&0x3f)<<4|(int(input[i+2])>>4))
} else if i+2 == inputLength {
tmp = append(tmp, (int(input[i+1])&0x3f)<<4)
}
if i+3 < inputLength {
tmp = append(tmp, (int(input[i+2])&0xf)<<6|(int(input[i+3])>>2))
} else if i+3 == inputLength {
tmp = append(tmp, (int(input[i+2])&0xf)<<6)
}
if i+4 < inputLength {
tmp = append(tmp, (int(input[i+3])&0x3)<<8|int(input[i+4]))
} else if i+4 == inputLength {
tmp = append(tmp, (int(input[i+3])&0x3)<<8)
}
if i < safe {
points = append(points, tmp...)
} else if i >= safe && remainder != 0 {
points = append(points, tmp[0:inputLength-safe]...)
}
}
resList := lo.Map(points, func(x int, _ int) string {
return Emojis[x]
})
resStr := strings.Join(resList, "")
if remainder == 4 {
resStr += TAIL
}
return resStr
}