Created
April 23, 2019 13:10
-
-
Save tai2/347ebc11077d4186f8d52b8c3cdf6603 to your computer and use it in GitHub Desktop.
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 rot(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 (r rot13Reader) Read(b []byte) (int, error) { | |
amount := 0 | |
for amount < len(b) { | |
s := b[amount:] | |
n, err := r.Read(s) | |
for i := 0; i < n; i++ { | |
s[i] = rot(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