-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define rustg_cyrillic_to_latin(text) call(RUST_G, "cyrillic_to_latin")("[text]") | ||
#define rustg_latin_to_cyrillic(text) call(RUST_G, "latin_to_cyrillic")("[text]") |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
use translit::{CharsMapping, Transliterator}; | ||
|
||
byond_fn!(fn cyrillic_to_latin (string) { | ||
Some(cyr_to_latin(string)) | ||
}); | ||
|
||
byond_fn!(fn latin_to_cyrillic (string) { | ||
Some(latin_to_cyr(string)) | ||
}); | ||
|
||
fn cyr_to_latin(string: &str) -> String { | ||
Transliterator::new(table_to_latin()).convert(string, false) | ||
} | ||
|
||
fn latin_to_cyr(string: &str) -> String { | ||
Transliterator::new(table_from_latin()).convert(string, true) | ||
} | ||
|
||
fn table_to_latin() -> CharsMapping { | ||
[ | ||
("А", "A"), | ||
("Б", "B"), | ||
("В", "V"), | ||
("Г", "G"), | ||
("Д", "D"), | ||
("Е", "E"), | ||
("Ё", "Yo"), | ||
("Ж", "Zh"), | ||
("З", "Z"), | ||
("И", "I"), | ||
("Й", "I"), | ||
("К", "K"), | ||
("Л", "L"), | ||
("М", "M"), | ||
("Н", "N"), | ||
("О", "O"), | ||
("П", "P"), | ||
("Р", "R"), | ||
("С", "S"), | ||
("Т", "T"), | ||
("У", "U"), | ||
("Ф", "F"), | ||
("Х", "Kh"), | ||
("Х", "H"), | ||
("Ц", "Ts"), | ||
("Ч", "Ch"), | ||
("Ш", "Sh"), | ||
("Щ", "Shch"), | ||
("Ъ", "Ie"), | ||
("Ы", "Y"), | ||
("Ь", "'"), | ||
("Э", "E"), | ||
("Ю", "Iu"), | ||
("Я", "Ia"), | ||
("а", "a"), | ||
("б", "b"), | ||
("в", "v"), | ||
("г", "g"), | ||
("д", "d"), | ||
("е", "e"), | ||
("ё", "yo"), | ||
("ж", "zh"), | ||
("з", "z"), | ||
("и", "i"), | ||
("й", "i"), | ||
("к", "k"), | ||
("л", "l"), | ||
("м", "m"), | ||
("н", "n"), | ||
("о", "o"), | ||
("п", "p"), | ||
("р", "r"), | ||
("с", "s"), | ||
("т", "t"), | ||
("у", "u"), | ||
("ф", "f"), | ||
("х", "kh"), | ||
("ц", "ts"), | ||
("ч", "ch"), | ||
("ш", "sh"), | ||
("щ", "shch"), | ||
("ъ", "ie"), | ||
("ы", "y"), | ||
("ь", "'"), | ||
("э", "e"), | ||
("ю", "iu"), | ||
("я", "ia"), | ||
("№", "#"), | ||
] | ||
.to_vec() | ||
} | ||
|
||
fn table_from_latin() -> CharsMapping { | ||
[ | ||
("А", "A"), | ||
("Б", "B"), | ||
("В", "V"), | ||
("В", "W"), | ||
("Г", "G"), | ||
("Д", "D"), | ||
("Дж", "J"), | ||
("Э", "E"), | ||
("Ё", "Yo"), | ||
("Ж", "Zh"), | ||
("З", "Z"), | ||
("З", "Th"), | ||
("Зэ", "The"), | ||
("И", "I"), | ||
("Й", "I"), | ||
("К", "C"), | ||
("К", "K"), | ||
("К", "Q"), | ||
("К", "Ck"), | ||
("Кс", "X"), | ||
("Л", "L"), | ||
("М", "M"), | ||
("Н", "N"), | ||
("О", "O"), | ||
("Оу", "Ow"), | ||
("П", "P"), | ||
("Р", "R"), | ||
("С", "S"), | ||
("Т", "T"), | ||
("У", "U"), | ||
("Ф", "F"), | ||
("Х", "Kh"), | ||
("Х", "H"), | ||
("Ц", "Ts"), | ||
("Ч", "Ch"), | ||
("Ш", "Sh"), | ||
("Щ", "Shch"), | ||
("Ъ", "Ie"), | ||
("Ы", "Y"), | ||
("Ь", "'"), | ||
("Е", "E"), | ||
("Ю", "Iu"), | ||
("Я", "Ia"), | ||
("а", "a"), | ||
("б", "b"), | ||
("в", "v"), | ||
("в", "w"), | ||
("г", "g"), | ||
("д", "d"), | ||
("дж", "j"), | ||
("е", "e"), | ||
("ё", "yo"), | ||
("ж", "zh"), | ||
("з", "z"), | ||
("з", "th"), | ||
("зэ", "the"), | ||
("и", "i"), | ||
("й", "i"), | ||
("к", "c"), | ||
("к", "k"), | ||
("к", "q"), | ||
("к", "ck"), | ||
("кс", "x"), | ||
("л", "l"), | ||
("м", "m"), | ||
("н", "n"), | ||
("о", "o"), | ||
("оу", "ow"), | ||
("п", "p"), | ||
("р", "r"), | ||
("с", "s"), | ||
("т", "t"), | ||
("у", "u"), | ||
("ф", "f"), | ||
("х", "kh"), | ||
("х", "h"), | ||
("ц", "ts"), | ||
("ч", "ch"), | ||
("ш", "sh"), | ||
("щ", "shch"), | ||
("ъ", "ie"), | ||
("ы", "y"), | ||
("ь", "'"), | ||
("э", "e"), | ||
("ю", "iu"), | ||
("я", "ia"), | ||
("№", "#"), | ||
] | ||
.to_vec() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn text_test() { | ||
let result = cyr_to_latin("Съешь же ещё этих мягких французских булок, да выпей чаю!"); | ||
assert_eq!( | ||
result, | ||
"Sieesh' zhe eshchyo etikh miagkikh frantsuzskikh bulok, da vypei chaiu!" | ||
); | ||
|
||
let result2 = cyr_to_latin("Привет мир! Hello world!"); | ||
assert_eq!(result2, "Privet mir! Hello world!"); | ||
|
||
let result3 = latin_to_cyr("Privet mir! Hello world! Zhypyr perotin kuroden."); | ||
assert_eq!(result3, "Привет мир! Хелло ворлд! Жыпыр перотин куроден."); | ||
|
||
let result4 = latin_to_cyr("Привет мир! Hello world!"); | ||
assert_eq!(result4, "Привет мир! Хелло ворлд!"); | ||
|
||
let result5 = latin_to_cyr("The quick brown fox jumps over the lazy dog!"); | ||
assert_eq!(result5, "Зэ куик броун фокс джумпс овер зэ лазы дог!"); | ||
} | ||
} |