Created
November 8, 2016 01:32
-
-
Save alexbosworth/e7dcb76f7cf82bcf7e981321a667ee5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (r rot13Reader) Read(b []byte) (int, error) { | |
bytesRead, err := r.r.Read(b) | |
if err != nil { | |
return bytesRead, io.EOF | |
} | |
input := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " | |
output := "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm " | |
for i := 0; i < bytesRead; i++ { | |
position := strings.IndexByte(input, b[i]) | |
if position == -1 { | |
continue | |
} | |
b[i] = output[position] | |
} | |
return bytesRead, 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