forked from becgabri/enigma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
27 lines (23 loc) · 738 Bytes
/
utils.go
File metadata and controls
27 lines (23 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package enigma
import (
"regexp"
"strings"
)
// CharToIndex returns the alphabet index of a given letter.
func CharToIndex(char byte) int {
return int(char - 'A')
}
// IndexToChar returns the letter with a given alphabet index.
func IndexToChar(index int) byte {
return byte('A' + index)
}
// SanitizePlaintext will prepare a string to be encoded
// in the Enigma machine: everything except A-Z will be
// stripped, spaces will be replaced with "X".
func SanitizePlaintext(plaintext string) string {
plaintext = strings.TrimSpace(plaintext)
plaintext = strings.ToUpper(plaintext)
plaintext = strings.Replace(plaintext, " ", "", -1)
plaintext = regexp.MustCompile(`[^A-Z]`).ReplaceAllString(plaintext, "X")
return plaintext
}