Created
May 7, 2015 06:21
-
-
Save itsmikeq/0c1c58fc86099956b731 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
rot io.Reader | |
} | |
func rot13(b byte) byte { | |
var a, z byte | |
if 'a' <= b && b <= 'z' { | |
a, z = 'a', 'z' | |
} else if 'A' <= b && b <= 'Z' { | |
a, z = 'A', 'Z' | |
} | |
return (b-a+13)%(z-a+1) + a | |
} | |
func (rot *rot13Reader) Read(bytes []byte) (num int, err error) { | |
num, err = rot.rot.Read(bytes) | |
for i := 0; i < num; i++ { | |
bytes[i] = rot13(bytes[i]) | |
} | |
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