Last active
December 18, 2015 03:58
-
-
Save uraimo/5721702 to your computer and use it in GitHub Desktop.
A Tour of Go: Rot13 Reader
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" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (reader rot13Reader) Read(p []byte) (n int, err error){ | |
alf := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
n,e := reader.r.Read(p) | |
for i,b := range(p) { | |
ru := rune(b) | |
idx:=strings.IndexRune(alf,ru) | |
if idx>0 { | |
p[i] = alf[(idx+13)%len(alf)] | |
} | |
} | |
return n,e | |
} | |
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