Created
July 19, 2018 19:52
-
-
Save bjorne/0fb38ad2e34c62ee0a549fec8c556bc7 to your computer and use it in GitHub Desktop.
rot13 from the Go tour
This file contains hidden or 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(d []byte) (int, error) { | |
count, err := r.r.Read(d) | |
if err != nil { | |
return 0, err | |
} | |
for i := 0; i < count; i++ { | |
code := uint(d[i]) | |
if code >= 65 && code <= 90 { | |
code = ((code - 65) + 13 ) % (90 - 65 + 1) + 65 | |
} else if code >= 97 && code <= 122 { | |
code = ((code - 97) + 13 ) % (122 - 97 + 1) + 97 | |
} | |
d[i] = byte(code) | |
} | |
return count, 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