diff --git a/exercises/07-rot13/go.go b/exercises/07-rot13/go.go index ad48d67..51d594f 100644 --- a/exercises/07-rot13/go.go +++ b/exercises/07-rot13/go.go @@ -11,13 +11,21 @@ type rot13Reader struct { } func (r rot13Reader) Read(p []byte) (n int, err error) { - if len(p) == 0 { - return 0, nil + 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) } - if r.i >= len(r.s) { - return 0, io.EOF - } - return + return b } func main() {