Skip to content

Commit

Permalink
Feat: text (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bizzonium authored and Dimach committed Aug 5, 2024
1 parent 1a00f91 commit f4bed5f
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dmi = { version = "0.3.5", optional = true }
tracy_full = { version = "1.8.0", optional = true }
ammonia = { version = "4.0.0", optional = true }
workerpool = "1.2.0"
translit = { version = "0.5.0", optional = true }

[features]
default = [
Expand Down Expand Up @@ -101,6 +102,7 @@ all = [
"rustls_tls",
"sanitize",
"sql",
"text",
"time",
"toml",
"url",
Expand Down Expand Up @@ -158,6 +160,7 @@ iconforge = [
pathfinder = ["num-integer", "pathfinding", "serde", "serde_json"]
redis_pubsub = ["flume", "redis", "serde", "serde_json"]
redis_reliablequeue = ["flume", "redis", "serde", "serde_json"]
text = ["translit"]
unzip = ["zip", "jobs"]
worleynoise = ["rand", "rayon"]

Expand Down
2 changes: 2 additions & 0 deletions dmsrc/text.dm
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]")
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub mod redis_reliablequeue;
pub mod sanitize;
#[cfg(feature = "sql")]
pub mod sql;
#[cfg(feature = "text")]
pub mod text;
#[cfg(feature = "time")]
pub mod time;
#[cfg(feature = "toml")]
Expand Down
210 changes: 210 additions & 0 deletions src/text.rs
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, "Зэ куик броун фокс джумпс овер зэ лазы дог!");
}
}

0 comments on commit f4bed5f

Please sign in to comment.