Skip to content

Instantly share code, notes, and snippets.

@kakty3
Last active January 31, 2022 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakty3/b9d0a1cdcccb46721cd9bd1fbebd41fd to your computer and use it in GitHub Desktop.
Save kakty3/b9d0a1cdcccb46721cd9bd1fbebd41fd to your computer and use it in GitHub Desktop.
Go tour rot13Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (reader *rot13Reader) Read(p []byte) (int, error) {
b := make([]byte, 8)
n, err := reader.r.Read(b)
//fmt.Printf("n=%v err=%v b=%s\n", n, err, b[:n])
if err == io.EOF {
return 0, io.EOF
}
i := 0
for ; i < n; i++ {
p[i] = rot13Decode(b[i])
}
//fmt.Printf("decoded=%s\n", p[:n])
return i, nil
}
func rot13Decode(b byte) byte {
if b >= 'A' && b <= 'Z' {
return ((b - 'A' + 13) % 26) + 'A'
}
if b >= 'a' && b <= 'z' {
return ((b - 'a' + 13) % 26) + 'a'
}
return b
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment