Created
April 23, 2019 13:30
-
-
Save tai2/af90ec6e610f573c2d0b82999c310268 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 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