-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_rot13_test.go
74 lines (58 loc) · 1.49 KB
/
example_rot13_test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package flagx_test
import (
"flag"
"fmt"
"github.com/dolmen-go/flagx"
)
var rot13replacer [256]byte
func init() {
for i := 0; i < 256; i++ {
rot13replacer[i] = byte(i)
}
const lower = 'a' - 'A'
for i := 'A'; i <= 'L'; i++ {
rot13replacer[i] = byte(i + 13)
rot13replacer[i+13] = byte(i)
rot13replacer[i+lower] = byte(i + lower + 13)
rot13replacer[i+lower+13] = byte(i + lower)
}
}
type rot13 struct{}
func (rot13) EncodedLen(n int) int { return n }
func (rot13) Encode(dst, src []byte) {
if len(src) == 0 {
return
}
_ = dst[len(src)-1]
for i := 0; i < len(src); i++ {
dst[i] = rot13replacer[src[i]]
}
}
// EncodeToString returns the rot13 encoding of src.
func (rot13) EncodeToString(src []byte) string {
dst := make([]byte, len(src))
rot13{}.Encode(dst, src)
return string(dst)
}
func (rot13) DecodedLen(n int) int { return n }
func (rot13) Decode(dst, src []byte) (int, error) {
rot13{}.Encode(dst, src)
return len(src), nil
}
func (rot13) DecodeString(src string) ([]byte, error) {
dst := make([]byte, len(src))
for i := 0; i < len(src); i++ {
dst[i] = rot13replacer[src[i]]
}
return dst, nil
}
func ExampleEncoded_rot13() {
flags := flag.NewFlagSet("test", flag.PanicOnError) // Usually flag.CommandLine
var bin []byte
// Bind parameter "-password" to value bin above, with ROT13 decoding
flags.Var(flagx.Encoded(&bin, rot13{}), "password", "")
flags.Parse([]string{"-password", "frperg"})
fmt.Printf("Decoded: %q\n", bin)
// Output:
// Decoded: "secret"
}