Skip to content

Instantly share code, notes, and snippets.

@royling
Created April 14, 2013 07:43
Show Gist options
  • Save royling/bdbf8a81e88e47cbe0c5 to your computer and use it in GitHub Desktop.
Save royling/bdbf8a81e88e47cbe0c5 to your computer and use it in GitHub Desktop.
Implement a rot13Reader that implements io.Reader and reads from an io.Reader, modifying the stream by applying the ROT13 substitution cipher to all alphabetical characters.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rot13 *rot13Reader) Read(p []byte) (n int, err error) {
n, err = rot13.r.Read(p)
var b byte
for i := 0; i < n; i++ {
if c :=p[i]; c >= 'A' && c <= 'z' {
if c >= 'a' {
b = byte('a')
} else {
b = byte('A')
}
// rotate by 13 places
p[i] = (c - b + 13) % 26 + b
}
}
return
}
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