Created
August 27, 2014 11:57
-
-
Save kamatama41/f9549bb45df0a7c1b27d to your computer and use it in GitHub Desktop.
go-tourの#61を解いてみる ref: http://qiita.com/kamatama_41/items/192f96d05c675135d9b9
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 ( | |
"bytes" | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (reader rot13Reader) Read(p []byte) (n int, err error) { | |
tmp1 := make([]byte, 10) | |
n, err = reader.r.Read(tmp1) | |
tmp2 := bytes.Map(func(r rune) rune { | |
switch { | |
case ('a' <= r && r <= 'z'): | |
return 'a' + (r-'a'+13)%26 | |
case ('A' <= r && r <= 'Z'): | |
return 'A' + (r-'A'+13)%26 | |
default: | |
return r | |
} | |
}, tmp1) | |
copy(p, tmp2) | |
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