MCPcopy Create free account
hub / github.com/NextDotID/proof_server / DecodeString

Function DecodeString

util/base1024/decode.go:8–64  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

6)
7
8func DecodeString(s string) ([]byte, error) {
9 trimTail := false
10 if strings.HasSuffix(s, TAIL) {
11 s = strings.TrimRight(s, TAIL)
12 trimTail = true
13 }
14 arr := strings.Split(s, "")
15 points := lo.Map(arr, func(point string, index int) int {
16 return lo.IndexOf(Emojis, point)
17 })
18
19 pointsLength := len(points)
20 remainder := pointsLength % 4
21 safe := pointsLength - remainder
22 source := make([]int, 0)
23
24 for i := 0; i <= safe; i += 4 {
25 tmp := make([]int, 0)
26 if i < pointsLength { // first
27 tmp = append(tmp, points[i]>>2)
28 }
29
30 if i+1 < pointsLength { // second
31 tmp = append(tmp, ((points[i]&0x3)<<6)|(points[i+1]>>4))
32 } else if i+1 == pointsLength { // second is last
33 tmp = append(tmp, (points[i]&0x3)<<6)
34 }
35
36 if i+2 < pointsLength { // third
37 tmp = append(tmp, ((points[i+1]&0xf)<<4)|(points[i+2]>>6))
38 } else if i+2 == pointsLength { // third is last
39 tmp = append(tmp, (points[i+1]&0xf)<<4)
40 }
41
42 if i+3 < pointsLength { // forth
43 tmp = append(tmp, ((points[i+2]&0x3f)<<2)|(points[i+3]>>8))
44 tmp = append(tmp, points[i+3]&0xff)
45 } else if i+3 == pointsLength { // forth is last
46 tmp = append(tmp, (points[i+2]&0x3f)<<2)
47 }
48
49 if i < safe { // Not last chunk
50 source = append(source, tmp...)
51 } else if i >= safe && remainder != 0 { // Last chunk, with remainder
52 source = append(source, tmp[0:remainder]...)
53 }
54 }
55
56 if trimTail {
57 source = lo.DropRight(source, 1)
58 }
59 resList := lo.Map(source, func(x int, _ int) byte {
60 return byte(x)
61 })
62
63 return resList, nil
64}

Callers 2

DecodeStringFunction · 0.92
TestDecodeStringFunction · 0.70

Calls

no outgoing calls

Tested by 1

TestDecodeStringFunction · 0.56