Skip to content

Commit

Permalink
Feat: text (#1)
Browse files Browse the repository at this point in the history
* Feat: optional text feature

* Bump: version to 1.0.4-ss220

* Fix: add C char
  • Loading branch information
Bizzonium authored Oct 8, 2022
1 parent 7077d7d commit 1c4e6f9
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rust-g"
edition = "2021"
version = "1.0.3-ss220"
version = "1.0.4-ss220"
authors = ["Bjorn Neergaard <[email protected]>", "Tad Hardesty <[email protected]>", "rust-g maintainer team"]
repository = "https://github.com/tgstation/rust-g"
license = "MIT"
Expand Down Expand Up @@ -49,6 +49,7 @@ dbpnoise = { version = "0.1.2", optional = true}
pathfinding = { version = "3.0.13", optional = true }
num = { version = "0.4.0", optional = true }
workerpool = "1.2.0"
translit = { version = "0.5.0", optional = true }

[features]
default = ["acreplace", "cellularnoise", "dmi", "file", "git", "http", "json", "log", "noise", "sql", "time", "toml", "url"]
Expand All @@ -74,6 +75,7 @@ pathfinder = [ "num", "pathfinding", "serde", "serde_json"]
redis_pubsub = ["flume", "redis", "serde", "serde_json"]
unzip = ["zip", "jobs"]
worleynoise = ["rand","rayon"]
text = ["translit"]

# internal feature-like things
jobs = ["flume"]
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 @@ -46,6 +46,8 @@ pub mod unzip;
pub mod url;
#[cfg(feature = "worleynoise")]
pub mod worleynoise;
#[cfg(feature = "text")]
pub mod text;

#[cfg(not(target_pointer_width = "32"))]
compile_error!("rust-g must be compiled for a 32-bit target");
209 changes: 209 additions & 0 deletions src/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
use translit::{Transliterator, CharsMapping};

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"),
("№", "#"),
].iter()
.cloned()
.collect()
}

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"),
("№", "#"),
].iter()
.cloned()
.collect()
}

#[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 1c4e6f9

Please sign in to comment.