Skip to content

Instantly share code, notes, and snippets.

@yicone
Created January 20, 2017 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yicone/c87c8fc5c0c52cd159cd9680b7cd9e47 to your computer and use it in GitHub Desktop.
Save yicone/c87c8fc5c0c52cd159cd9680b7cd9e47 to your computer and use it in GitHub Desktop.
package main
import (
"io"
"os"
"strings"
)
func convert(b byte) byte {
switch {
case b >= 'a' && b < 'n':
fallthrough
case b >= 'A' && b < 'N':
b = b + 13
case b >= 'n' && b <= 'z':
fallthrough
case b >= 'N' && b <= 'Z':
b = b - 13
}
return b
}
type rot13Reader struct {
r io.Reader
}
func (self rot13Reader) Read(b []byte) (int, error) {
n, err := self.r.Read(b)
if err != nil {
return n, err
}
for i := 0; i < n; i++ {
b[i] = convert(b[i])
}
return n, nil
}
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