Last active
September 3, 2016 10:09
-
-
Save takatoshiono/6ccedf857ead078a9ec89f8a26a80de2 to your computer and use it in GitHub Desktop.
A Tour of Go: Exercise: rot13Reader, https://go-tour-jp.appspot.com/methods/23
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 (r rot13Reader) Read(b []byte) (int, error) { | |
n, err := r.r.Read(b) | |
for i := 0; i < n; i++ { | |
b[i] = rot13(b[i]) | |
} | |
return n, err | |
} | |
func rot13(b byte) byte { | |
if 'a' <= b && b <= 'z' { | |
base := b - 'a' | |
b = 'a' + (base+13)%26 | |
} else if 'A' <= b && b <= 'Z' { | |
base := b - 'A' | |
b = 'A' + (base+13)%26 | |
} | |
return b | |
} | |
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
https://ja.wikipedia.org/wiki/ROT13