Created
September 16, 2014 08:00
-
-
Save oldergod/f47fcf8bd072442b2878 to your computer and use it in GitHub Desktop.
Tour of Go Rot13
This file contains 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" | |
"unicode" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rot *rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = rot.r.Read(p) | |
for i := 0; i < len(p); i++ { | |
v := p[i] | |
is_upper := unicode.IsUpper(rune(v)) | |
is_lower := unicode.IsLower(rune(v)) | |
if is_upper || is_lower { | |
a := byte('a') | |
if is_upper { | |
a = byte('A') | |
} | |
p[i] = (v-a+13)%26 + a | |
} | |
} | |
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