1
0
gotour-notes/exercises/07-rot13/go.go

36 lines
553 B
Go
Raw Normal View History

2012-08-28 21:37:55 -07:00
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r rot13Reader) Read(p []byte) (n int, err error) {
2012-09-03 06:52:35 -07:00
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)
2012-08-28 21:37:55 -07:00
}
2012-09-03 06:52:35 -07:00
return b
2012-08-28 21:37:55 -07:00
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}