-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
spelling.go
142 lines (133 loc) · 3.02 KB
/
spelling.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-FileCopyrightText: 2021-2024 Winni Neessen <[email protected]>
//
// SPDX-License-Identifier: MIT
package apg
import (
"fmt"
"strings"
)
var (
symbNumNames = map[byte]string{
'1': "ONE",
'2': "TWO",
'3': "THREE",
'4': "FOUR",
'5': "FIVE",
'6': "SIX",
'7': "SEVEN",
'8': "EIGHT",
'9': "NINE",
'0': "ZERO",
33: "EXCLAMATION_POINT",
34: "QUOTATION_MARK",
35: "CROSSHATCH",
36: "DOLLAR_SIGN",
37: "PERCENT_SIGN",
38: "AMPERSAND",
39: "APOSTROPHE",
40: "LEFT_PARENTHESIS",
41: "RIGHT_PARENTHESIS",
42: "ASTERISK",
43: "PLUS_SIGN",
44: "COMMA",
45: "HYPHEN",
46: "PERIOD",
47: "SLASH",
58: "COLON",
59: "SEMICOLON",
60: "LESS_THAN",
61: "EQUAL_SIGN",
62: "GREATER_THAN",
63: "QUESTION_MARK",
64: "AT_SIGN",
91: "LEFT_BRACKET",
92: "BACKSLASH",
93: "RIGHT_BRACKET",
94: "CIRCUMFLEX",
95: "UNDERSCORE",
96: "GRAVE",
123: "LEFT_BRACE",
124: "VERTICAL_BAR",
125: "RIGHT_BRACE",
126: "TILDE",
}
alphabetNames = map[byte]string{
'A': "Alfa",
'B': "Bravo",
'C': "Charlie",
'D': "Delta",
'E': "Echo",
'F': "Foxtrot",
'G': "Golf",
'H': "Hotel",
'I': "India",
'J': "Juliett",
'K': "Kilo",
'L': "Lima",
'M': "Mike",
'N': "November",
'O': "Oscar",
'P': "Papa",
'Q': "Quebec",
'R': "Romeo",
'S': "Sierra",
'T': "Tango",
'U': "Uniform",
'V': "Victor",
'W': "Whiskey",
'X': "X_ray",
'Y': "Yankee",
'Z': "Zulu",
}
)
// Spell returns a given string as spelled english phonetic alphabet string
func Spell(input string) (string, error) {
var returnString []string
for _, curChar := range input {
curSpellString, err := ConvertByteToWord(byte(curChar))
if err != nil {
return "", err
}
returnString = append(returnString, curSpellString)
}
return strings.Join(returnString, "/"), nil
}
// Pronounce returns last generated pronounceable password as spelled syllables string
func (g *Generator) Pronounce() (string, error) {
var returnString []string
for _, syllable := range g.syllables {
isKoremutake := false
for _, x := range KoremutakeSyllables {
if x == strings.ToLower(syllable) {
isKoremutake = true
}
}
if isKoremutake {
returnString = append(returnString, syllable)
continue
}
curSpellString, err := ConvertByteToWord(syllable[0])
if err != nil {
return "", err
}
returnString = append(returnString, curSpellString)
}
return strings.Join(returnString, "-"), nil
}
// ConvertByteToWord converts a given ASCII byte into the corresponding spelled version
// of the english phonetic alphabet
func ConvertByteToWord(charByte byte) (string, error) {
var returnString string
switch {
case charByte > 64 && charByte < 91:
returnString = alphabetNames[charByte]
case charByte > 96 && charByte < 123:
returnString = strings.ToLower(alphabetNames[charByte-32])
default:
returnString = symbNumNames[charByte]
}
if returnString == "" {
return "", fmt.Errorf("failed to convert given byte to word: %s is unsupported", string(charByte))
}
return returnString, nil
}