Skip to content

Instantly share code, notes, and snippets.

@alexbosworth
Created November 8, 2016 01:32
Show Gist options
  • Save alexbosworth/e7dcb76f7cf82bcf7e981321a667ee5e to your computer and use it in GitHub Desktop.
Save alexbosworth/e7dcb76f7cf82bcf7e981321a667ee5e to your computer and use it in GitHub Desktop.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r rot13Reader) Read(b []byte) (int, error) {
bytesRead, err := r.r.Read(b)
if err != nil {
return bytesRead, io.EOF
}
input := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
output := "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm "
for i := 0; i < bytesRead; i++ {
position := strings.IndexByte(input, b[i])
if position == -1 {
continue
}
b[i] = output[position]
}
return bytesRead, nil
}
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