Skip to content

Instantly share code, notes, and snippets.

@tai2
Created April 23, 2019 13:30
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 tai2/af90ec6e610f573c2d0b82999c310268 to your computer and use it in GitHub Desktop.
Save tai2/af90ec6e610f573c2d0b82999c310268 to your computer and use it in GitHub Desktop.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func rot13(b byte) byte {
if 97 <= b && b <= 122 {
return 97 + ((b - 97 + 13) % 26)
} else if 65 <= b && b <= 90 {
return 65 + ((b - 65 + 13) % 26)
} else {
return b
}
}
func (rr rot13Reader) Read(b []byte) (int, error) {
amount := 0
for amount < len(b) {
s := b[amount:]
n, err := rr.r.Read(s)
for i := 0; i < n; i++ {
s[i] = rot13(s[i])
}
amount += n
if err != nil {
return amount, err
}
}
return amount, 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