Skip to content

Instantly share code, notes, and snippets.

@marcesher
Created January 8, 2014 12:40
Show Gist options
  • Save marcesher/8316237 to your computer and use it in GitHub Desktop.
Save marcesher/8316237 to your computer and use it in GitHub Desktop.
go tour rot13 exercise I imagine the strings.ToLower version is much slower. experimenting is all.
package main
import (
"io"
"os"
"strings"
)
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++ {
lc := strings.ToLower(string(p[i]))
if lc >= "a" && lc <= "m" {
p[i] += 13
} else if lc >= "n" && lc <= "z" {
p[i] -= 13
}
// if p[i] >= 'A' && p[i] <= 'M' || p[i] >= 'a' && p[i] <= 'm' {
// p[i] += 13
// } else if p[i] >= 'N' && p[i] <= 'Z' || p[i] >= 'n' && p[i] <= 'z' {
// p[i] -= 13
// }
}
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