Last active
March 4, 2020 11:35
-
-
Save polynomialspace/60e4aae3e804e73e61932c1c8d8c9d98 to your computer and use it in GitHub Desktop.
rot13 / caesar implementation in go (bad/kinda silly)
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 ( | |
"os" | |
"bufio" | |
"fmt" | |
) | |
// double curly brackets to provide arbitrary scoping for variable shadowing. | |
// this is rather disgusting, but cleaner than repeat casts or more vars. | |
func GenCaesar(offset int) (caesarmap map[rune]rune) {{ | |
offset := rune(offset) | |
caesarmap = make(map[rune]rune) | |
// Enumerate only up to offset ... | |
for i := rune(0); (i + offset) < 26; i++ { | |
cur, CUR := 'a' + i, 'A' + i | |
caesarmap[cur] = cur + offset | |
caesarmap[CUR] = CUR + offset | |
} | |
// ... and then loop back around | |
for i := offset; i < 26; i++ { | |
cur, CUR := 'a' + i, 'A' + i | |
caesarmap[cur] = cur - offset | |
caesarmap[CUR] = CUR - offset | |
} | |
return | |
}} | |
// double curly brackets to provide arbitrary scoping for variable shadowing. | |
// this is rather disgusting, but cleaner than repeat casts or more vars. | |
func rot13(in string) (out string) {{ | |
lookup := GenCaesar(13) | |
out := make([]rune, len(in)) | |
for i, c := range in { | |
found := false | |
out[i], found = lookup[c] | |
if !found { | |
out[i] = c | |
} | |
} | |
return string(out) | |
}} | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
for fmt.Printf("> "); scanner.Scan(); fmt.Printf("> ") { | |
in := scanner.Text() | |
out := rot13(in) | |
fmt.Println(out) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment