Skip to content

Instantly share code, notes, and snippets.

@zh-f
Created June 11, 2020 01:28
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 zh-f/8ad522f070ec05eec0bd60720ec95ad9 to your computer and use it in GitHub Desktop.
Save zh-f/8ad522f070ec05eec0bd60720ec95ad9 to your computer and use it in GitHub Desktop.
Solution of Exercise: rot13Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
//ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
const src string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
//NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm
const dest string = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
func (r *rot13Reader) Read (b []byte) (n int, err error) {
m, err := r.r.Read(b)
for i := 0; i < m; i++ {
if b[i] >= 'A' && b[i] <= 'Z' || b[i] >= 'a' && b[i] <= 'z' {
b[i] = dest[strings.IndexByte(src, b[i])]
}
}
return m, err
}
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