1
0
gotour-notes/exercises/07-rot13/go.go
2012-09-03 07:52:35 -06:00

36 lines
553 B
Go

package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r rot13Reader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
for i := range p[:n] {
p[i] = rot13(p[i])
}
return
}
func rot13(b byte) byte {
var a, z, A, Z byte = 'a', 'z', 'A', 'Z'
if a < b && b < z {
b = a + ((b - a + 13) % 26)
} else if A < b && b < Z {
b = A + ((b - A + 13) % 26)
}
return b
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}