Created
March 29, 2022 03:56
-
-
Save anna-hope/5bcf5dfa9cd9bd9a7f09fb55b756f095 to your computer and use it in GitHub Desktop.
go rot13
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 (r13reader rot13Reader) Read(bytes []byte) (int, error) { | |
n, err := r13reader.r.Read(bytes) | |
if err != nil { | |
if err == io.EOF { | |
return n, io.EOF | |
} else { | |
panic("something went horribly wrong") | |
} | |
} | |
for n, c := range bytes { | |
if (c > 77 && c < 91) || c > 109 { | |
c -= 13 | |
} else if c > 64 || c > 96 { | |
c += 13 | |
} | |
bytes[n] = c | |
} | |
return n, 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