Skip to content

Commit 1f20cce

Browse files
authored
feat:Add ascii85 coding tool class and test class (#104)
1 parent f9e7ca8 commit 1f20cce

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

gox/encodingx/ascii85x/ascii85x.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ascii85x
19+
20+
import (
21+
"encoding/ascii85"
22+
)
23+
24+
// Ascii85EncodeToInt ascii85x convert bytes to int
25+
func Ascii85EncodeToInt(dst, src []byte) int {
26+
return ascii85.Encode(dst, src)
27+
}
28+
29+
// Ascii85Decode ascii85x convert bytes and the number to number of bytes
30+
func Ascii85Decode(dst, src []byte, flush bool) (ndst, nsrc int, err error) {
31+
return ascii85.Decode(dst, src, flush)
32+
}
33+
34+
// Ascii85MaxEncodedLenToInt ascii85x convert int to int
35+
func Ascii85MaxEncodedLenToInt(n int) int {
36+
return ascii85.MaxEncodedLen(n)
37+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ascii85x
19+
20+
import "testing"
21+
22+
var pairs = []struct {
23+
decoded string
24+
encoded string
25+
}{
26+
{
27+
"",
28+
"",
29+
},
30+
{
31+
"\000\000\000\000",
32+
"z",
33+
},
34+
}
35+
36+
func TestAscii85EncodeToInt(t *testing.T) {
37+
for _, p := range pairs {
38+
buf := make([]byte, Ascii85MaxEncodedLenToInt(len(p.decoded)))
39+
result := Ascii85EncodeToInt(buf, []byte(p.decoded))
40+
buf = buf[0:result]
41+
if strip85(string(buf)) != strip85(p.encoded) {
42+
t.Errorf("TestAscii85EncodeToInt() result = %v, want %v", result, strip85(p.encoded))
43+
}
44+
}
45+
}
46+
47+
func TestAscii85Decode(t *testing.T) {
48+
for _, p := range pairs {
49+
dbuf := make([]byte, 4*len(p.encoded))
50+
ndst, nsrc, err := Ascii85Decode(dbuf, []byte(p.encoded), true)
51+
if err != error(nil) {
52+
t.Errorf("TestAscii85Decode() error = %v, want %v", err, error(nil))
53+
}
54+
if nsrc != len(p.encoded) {
55+
t.Errorf("TestAscii85Decode() nsrc = %v, want %v", nsrc, len(p.encoded))
56+
}
57+
if ndst != len(p.decoded) {
58+
t.Errorf("TestAscii85Decode() ndst = %v, want %v", ndst, len(p.decoded))
59+
}
60+
if string(dbuf[0:ndst]) != p.decoded {
61+
t.Errorf("TestAscii85Decode() want %v", p.decoded)
62+
}
63+
}
64+
}
65+
66+
func strip85(s string) string {
67+
t := make([]byte, len(s))
68+
w := 0
69+
for r := 0; r < len(s); r++ {
70+
c := s[r]
71+
if c > ' ' {
72+
t[w] = c
73+
w++
74+
}
75+
}
76+
return string(t[0:w])
77+
}

0 commit comments

Comments
 (0)