/
translit.go
290 lines (231 loc) · 8.23 KB
/
translit.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Package translit convert russian text to translit
package translit
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"bytes"
)
// ////////////////////////////////////////////////////////////////////////////////// //
type specProc func(p, c, n rune, extMap map[string]string) (string, bool)
// ////////////////////////////////////////////////////////////////////////////////// //
var baseRuEn = map[string]string{
"а": "a", "А": "A", "Б": "B", "б": "b", "В": "V", "в": "v", "Г": "G", "г": "g",
"Д": "D", "д": "d", "З": "Z", "з": "z", "И": "I", "и": "i", "К": "K", "к": "k",
"Л": "L", "л": "l", "М": "M", "м": "m", "Н": "N", "н": "n", "О": "O", "о": "o",
"П": "P", "п": "p", "Р": "R", "р": "r", "С": "S", "с": "s", "Т": "T", "т": "t",
"У": "U", "у": "u", "Ф": "F", "ф": "f",
}
var scientificRuEn = map[string]string{
"Е": "E", "е": "e", "Ё": "Ë", "ё": "ë", "Ж": "Ž", "ж": "ž", "Й": "J", "й": "j",
"Х": "Ch", "х": "ch", "Ц": "C", "ц": "c", "Ч": "Č", "ч": "č", "Ш": "Š", "ш": "š",
"Щ": "Šč", "щ": "šč", "Ъ": "″", "ъ": "″", "Ы": "Y", "ы": "y", "Ь": "′", "ь": "′",
"Э": "È", "э": "è", "Ю": "Ju", "ю": "ju", "Я": "Ja", "я": "ja",
}
var iso9ARuEn = map[string]string{
"Е": "E", "е": "e", "Ё": "Ë", "ё": "ë", "Ж": "Ž", "ж": "ž", "Й": "J", "й": "j",
"Х": "H", "х": "h", "Ц": "C", "ц": "c", "Ч": "Č", "ч": "č", "Ш": "Š", "ш": "š",
"Щ": "Ŝ", "щ": "ŝ", "Ъ": "″", "ъ": "″", "Ы": "Y", "ы": "y", "Ь": "′", "ь": "′",
"Э": "È", "э": "è", "Ю": "Û", "ю": "û", "Я": "Â", "я": "â",
}
var iso9BRuEn = map[string]string{
"Е": "E", "е": "e", "Ё": "Yo", "ё": "yo", "Ж": "Zh", "ж": "zh", "Й": "J", "й": "j",
"Х": "X", "х": "x", "Ч": "Ch", "ч": "ch", "Ш": "Sh", "ш": "sh", "Щ": "Shh",
"щ": "shh", "Ъ": "``", "ъ": "``", "Ы": "Y`", "ы": "y`", "Ь": "`", "ь": "`",
"Э": "E`", "э": "e`", "Ю": "Yu", "ю": "yu", "Я": "Ya", "я": "ya",
}
var bgnRuEn = map[string]string{
"Ж": "Zh", "ж": "zh", "И": "I", "и": "i", "Й": "Y", "й": "y", "Х": "Kh", "х": "kh",
"Ц": "Ts", "ц": "ts", "Ч": "Ch", "ч": "ch", "Ш": "Sh", "ш": "sh", "Щ": "Shch",
"щ": "shch", "Ъ": "″", "ъ": "″", "Ы": "Y", "ы": "y", "Ь": "′", "ь": "′", "Э": "E",
"э": "e", "Ю": "Yu", "ю": "yu", "Я": "Ya", "я": "ya",
}
var bsRuEn = map[string]string{
"Е": "E", "е": "e", "Ё": "Ë", "ё": "ë", "Ж": "Zh", "ж": "zh", "Й": "Ĭ", "й": "ĭ",
"Х": "Kh", "х": "kh", "Ц": "Ts", "ц": "ts", "Ч": "Ch", "ч": "ch", "Ш": "Sh",
"ш": "sh", "Щ": "Shch", "щ": "shch", "Ъ": "″", "ъ": "″", "Ы": "Ȳ", "ы": "ȳ",
"Ь": "′", "ь": "′", "Э": "É", "э": "é", "Ю": "Yu", "ю": "yu", "Я": "Ya", "я": "ya",
}
var alalcRuEn = map[string]string{
"Е": "E", "е": "e", "Ё": "Ë", "ё": "ë", "Ж": "Zh", "ж": "zh", "Й": "Ĭ", "й": "ĭ",
"Х": "Kh", "х": "kh", "Ц": "T͡s", "ц": "t͡s", "Ч": "Ch", "ч": "ch", "Ш": "Sh",
"ш": "sh", "Щ": "Shch", "щ": "shch", "Ъ": "″", "ъ": "″", "Ы": "Y", "ы": "y",
"Ь": "′", "ь": "′", "Э": "Ė", "э": "ė", "Ю": "I͡u", "ю": "i͡u", "Я": "I͡a", "я": "i͡a",
}
var icaoRuEn = map[string]string{
"Е": "E", "е": "e", "Ё": "E", "ё": "e", "Ж": "Zh", "ж": "zh", "Й": "I", "й": "i",
"Х": "Kh", "х": "kh", "Ц": "Ts", "ц": "ts", "Ч": "Ch", "ч": "ch", "Ш": "Sh",
"ш": "sh", "Щ": "Shch", "щ": "shch", "Ъ": "Ie", "ъ": "ie", "Ы": "Y", "ы": "y",
"Ь": "", "ь": "", "Э": "E", "э": "e", "Ю": "Iu", "ю": "iu", "Я": "Ia", "я": "ia",
}
var iso9BSpecial = map[string]string{
"ц": "cz", "Ц": "Cz", "ц+": "с", "Ц+": "С",
}
var bgnSpecial = map[string]string{
"е": "e", "Е": "E", "ё": "ë", "Ё": "Ë",
"е+": "ye", "Е+": "Ye", "ё+": "yë", "Ё+": "Yë",
}
// ////////////////////////////////////////////////////////////////////////////////// //
// EncodeToScientific encodes text with scientific mappings
//
// Deprecated: Use Scientific method instead
func EncodeToScientific(text string) string {
return Scientific(text)
}
// EncodeToISO9A encodes text with ISO 9:1995/A ГОСТ 7.79-2000/A mappings
//
// Deprecated: Use ISO9A method instead
func EncodeToISO9A(text string) string {
return ISO9A(text)
}
// EncodeToISO9B encodes text with ISO 9:1995/B ГОСТ 7.79-2000/Б mappings
//
// Deprecated: Use ISO9B method instead
func EncodeToISO9B(text string) string {
return ISO9B(text)
}
// EncodeToBGN encodes text with BGN mappings
//
// Deprecated: Use BGN method instead
func EncodeToBGN(text string) string {
return BGN(text)
}
// EncodeToPCGN encodes text with PCGN mappings
//
// Deprecated: Use PCGN method instead
func EncodeToPCGN(text string) string {
return PCGN(text)
}
// EncodeToALALC encodes text with ALA-LC mappings
//
// Deprecated: Use ALALC method instead
func EncodeToALALC(text string) string {
return ALALC(text)
}
// EncodeToBS encodes text with BS 2979:1958 mappings
//
// Deprecated: Use BS method instead
func EncodeToBS(text string) string {
return BS(text)
}
// EncodeToBS encodes text with ICAO mappings
//
// Deprecated: Use ICAO method instead
func EncodeToICAO(text string) string {
return ICAO(text)
}
// Scientific encodes text with scientific mappings
func Scientific(text string) string {
return encode(text, scientificRuEn, nil)
}
// ISO9A encodes text with ISO 9:1995/A ГОСТ 7.79-2000/A mappings
func ISO9A(text string) string {
return encode(text, iso9ARuEn, nil)
}
// ISO9B encodes text with ISO 9:1995/B ГОСТ 7.79-2000/Б mappings
func ISO9B(text string) string {
return encode(text, iso9BRuEn, iso9BSpec)
}
// BGN encodes text with BGN mappings
func BGN(text string) string {
return encode(text, bgnRuEn, bgnSpec)
}
// PCGN encodes text with PCGN mappings
func PCGN(text string) string {
return encode(text, bgnRuEn, bgnSpec)
}
// ALALC encodes text with ALA-LC mappings
func ALALC(text string) string {
return encode(text, alalcRuEn, nil)
}
// BS encodes text with BS 2979:1958 mappings
func BS(text string) string {
return encode(text, bsRuEn, nil)
}
// ICAO encodes text with ICAO mappings
func ICAO(text string) string {
return encode(text, icaoRuEn, nil)
}
// ////////////////////////////////////////////////////////////////////////////////// //
func encode(text string, extMap map[string]string, proc specProc) string {
if text == "" {
return ""
}
var input = bytes.NewBufferString(text)
var output = bytes.NewBuffer(nil)
// Previous, next letter for special processor
var p, n rune
var rr string
var ok bool
for {
r, _, err := input.ReadRune()
if err != nil {
break
}
if !isRussianChar(r) {
output.WriteRune(r)
p = r
continue
}
if proc != nil {
n, _, _ = input.ReadRune()
input.UnreadRune()
rr, ok = proc(p, r, n, extMap)
if ok {
output.WriteString(rr)
continue
}
}
p = r
rr, ok = baseRuEn[string(r)]
if ok {
output.WriteString(rr)
continue
}
rr, ok = extMap[string(r)]
if ok {
output.WriteString(rr)
}
}
return output.String()
}
func iso9BSpec(p, c, n rune, extMap map[string]string) (string, bool) {
if c != 'ц' && c != 'Ц' {
return "", false
}
rr, ok := baseRuEn[string(n)]
if !ok {
rr = extMap[string(n)]
}
switch rr {
case "e", "i", "y", "j", "E", "I", "Y", "J":
return iso9BSpecial[string(c)+"+"], true
}
return iso9BSpecial[string(c)], true
}
func bgnSpec(p, c, n rune, extMap map[string]string) (string, bool) {
switch c {
case 'е', 'Е', 'ё', 'Ё':
// nop
default:
return "", false
}
switch p {
case 0, ' ',
'а', 'у', 'о', 'ы', 'и', 'э', 'я', 'ю',
'А', 'У', 'О', 'Ы', 'И', 'Э', 'Я', 'Ю':
return bgnSpecial[string(c)+"+"], true
}
return bgnSpecial[string(c)], true
}
func isRussianChar(r rune) bool {
switch {
case r >= 1040 && r <= 1103,
r == 1105, r == 1025:
return true
}
return false
}