Skip to content

Instantly share code, notes, and snippets.

@atemate
Created May 5, 2020 22:34
Show Gist options
  • Save atemate/18350323136104a8d06c4a0496d60074 to your computer and use it in GitHub Desktop.
Save atemate/18350323136104a8d06c4a0496d60074 to your computer and use it in GitHub Desktop.
A Tour of Go: Exercise: rot13Reader https://tour.golang.org/methods/23
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r13 *rot13Reader) Read(s []byte) (int, error) {
r := r13.r
b := make([]byte, 1)
for i:=0 ;; i++ {
_, err := r.Read(b)
if err == io.EOF {
return i, io.EOF
}
c := b[0]
var a byte
switch {
case 'a' <= c && c <= 'z':
a = 'a'
case 'A' <= c && c <= 'Z':
a = 'A'
}
if a != 0 {
c = (c - a + 13) % 26 + a
}
s[i] = byte(c)
}
}
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