[x/tour] golang-tour: Solutions to the exercises.
R=adg
CC=golang-dev
https://golang.org/cl/6585074
X-Tour-Commit: 6539f1a515571891846b5c7510ab8f75c7f09ae2
diff --git a/tour/gotour/solutions/rot13.go b/tour/gotour/solutions/rot13.go
new file mode 100644
index 0000000..5533359
--- /dev/null
+++ b/tour/gotour/solutions/rot13.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+)
+
+func rot13(b byte) byte {
+ var a, z byte
+ switch {
+ case 'a' <= b && b <= 'z':
+ a, z = 'a', 'z'
+ case 'A' <= b && b <= 'Z':
+ a, z = 'A', 'Z'
+ default:
+ return b
+ }
+ return (b-a+13)%(z-a+1) + a
+}
+
+type rot13Reader struct {
+ r io.Reader
+}
+
+func (r rot13Reader) Read(p []byte) (n int, err error) {
+ n, err = r.r.Read(p)
+ for i := 0; i < n; i++ {
+ p[i] = rot13(p[i])
+ }
+ return
+}
+
+func main() {
+ s := strings.NewReader(
+ "Lbh penpxrq gur pbqr!")
+ r := rot13Reader{s}
+ io.Copy(os.Stdout, &r)
+}