Skip to content

Commit a9857d2

Browse files
committed
more refactoring
1 parent 24926a3 commit a9857d2

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,25 @@ Head over to [releases](https://github.com/quackduck/aces/releases) and download
5959
Aces - Encode in any character set
6060

6161
Usage:
62-
aces <charset> - encode data from STDIN into <charset>
63-
aces -d/--decode <charset> - decode data from STDIN from <charset>
64-
aces -h/--help - print this help message
65-
66-
Aces reads from STDIN for your data and outputs the result to STDOUT. The charset length must be
67-
a power of 2. While decoding, bytes not in the charset are ignored. Aces does not add any padding.
68-
```
69-
## Examples
70-
```shell
71-
echo hello world | aces "<>(){}[]" | aces --decode "<>(){}[]" # basic usage
72-
echo matthew stanciu | aces HhAa | say # make funny sounds (macOS)
73-
aces " X" < /bin/echo # see binaries visually
74-
echo 0100100100100001 | aces -d 01 | aces 01234567 # convert bases
75-
echo Calculus | aces 01 # what's stuff in binary?
76-
echo Aces™ | base64 | aces -d
77-
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ # even decode base64
62+
aces <charset> - encode data from STDIN into <charset>
63+
aces -d/--decode <charset> - decode data from STDIN from <charset>
64+
aces -h/--help - print this help message
65+
66+
Aces reads from STDIN for your data and outputs the result to STDOUT. An optimized algorithm is used
67+
for character sets with a power of 2 length. Newlines are ignored when decoding.
68+
69+
Examples:
70+
echo hello world | aces "<>(){}[]" | aces --decode "<>(){}[]" # basic usage
71+
echo matthew stanciu | aces HhAa | say # make funny sounds (macOS)
72+
aces " X" < /bin/echo # see binaries visually
73+
echo 0100100100100001 | aces -d 01 | aces 0123456789abcdef # convert bases
74+
echo Calculus | aces 01 # what's stuff in binary?
75+
echo Aces™ | base64 | aces -d
76+
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ # even decode base64
77+
echo -n uwonsmth | aces 🥇🥈🥉 # emojis work too!
78+
Set the encoding/decoding buffer size with --bufsize <size> (default 16KiB).
79+
80+
File issues, contribute or star at github.com/quackduck/aces
7881
```
7982
8083
## How does it work?

aces.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,13 @@ func (c *anyCoding) SetByteChunkSize(size int) {
291291
func (c *anyCoding) SetBufferSize(bufSize int) { c.bufSize = bufSize }
292292

293293
func (c *anyCoding) Encode(dst io.Writer, src io.Reader) error {
294-
//br := bufio.NewReaderSize(src, c.bufSize)
295294
result := make([]rune, 0, c.bufSize)
296295
buf := make([]byte, c.chunkSize)
297-
endWrite := func(n int) error { // endWrite takes the number of bytes that were just read, encodes it and writes to dst.
298-
result = append(result, encodeByteChunk(c.charset, buf, c.rPerChunk)...)
299-
result = append(result, toBase(bytesToInt([]byte{byte(n)}), make([]rune, 0, 8), c.charset)...)
300-
_, err := dst.Write([]byte(string(result)))
296+
endWrite := func(n int) error { // endWrite takes the number of bytes that were just read, encodes it and writes to dst. This allows the decoder to trim the last chunk down to the right size.
297+
_, err := dst.Write([]byte(string(append(append(
298+
result,
299+
encodeByteChunk(c.charset, buf, c.rPerChunk)...),
300+
toBase(bytesToInt([]byte{byte(n)}), make([]rune, 0, 8), c.charset)...))))
301301
return err
302302
}
303303
for {
@@ -309,7 +309,6 @@ func (c *anyCoding) Encode(dst io.Writer, src io.Reader) error {
309309
return err
310310
}
311311
if err == io.ErrUnexpectedEOF { // end of data, not a multiple of chunk size.
312-
// encode how many bytes of this chunk to keep
313312
if err = endWrite(n); err != nil {
314313
return err
315314
}
@@ -349,8 +348,6 @@ func decodeToByteChunk(set []rune, runes []rune, chunkSize int) ([]byte, error)
349348
if err != nil {
350349
return nil, err
351350
}
352-
//println(num.String())
353-
//fmt.Println(num.String())
354351
return num.FillBytes(make([]byte, chunkSize)), nil
355352
}
356353

0 commit comments

Comments
 (0)