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
use regex::Captures;
use regex::Regex;
use std::borrow::Cow;

static TRANSFORM_SMALL_MAP: &[char] = &[
    'a', 'ƀ', 'ƈ', 'ḓ', 'e', 'ƒ', 'ɠ', 'ħ', 'i', 'ĵ', 'ķ', 'ŀ', 'ḿ', 'ƞ', 'o', 'ƥ', 'ɋ', 'ř', 'ş',
    'ŧ', 'u', 'ṽ', 'ẇ', 'ẋ', 'ẏ', 'ẑ',
];
static TRANSFORM_CAPS_MAP: &[char] = &[
    'A', 'Ɓ', 'Ƈ', 'Ḓ', 'E', 'Ƒ', 'Ɠ', 'Ħ', 'I', 'Ĵ', 'Ķ', 'Ŀ', 'Ḿ', 'Ƞ', 'O', 'Ƥ', 'Ɋ', 'Ř', 'Ş',
    'Ŧ', 'U', 'Ṽ', 'Ẇ', 'Ẋ', 'Ẏ', 'Ẑ',
];

static FLIPPED_SMALL_MAP: &[char] = &[
    'ɐ', 'q', 'ɔ', 'p', 'ǝ', 'ɟ', 'ƃ', 'ɥ', 'ı', 'ɾ', 'ʞ', 'ʅ', 'ɯ', 'u', 'o', 'd', 'b', 'ɹ', 's',
    'ʇ', 'n', 'ʌ', 'ʍ', 'x', 'ʎ', 'z',
];
static FLIPPED_CAPS_MAP: &[char] = &[
    '∀', 'Ԑ', 'Ↄ', 'ᗡ', 'Ǝ', 'Ⅎ', '⅁', 'H', 'I', 'ſ', 'Ӽ', '⅂', 'W', 'N', 'O', 'Ԁ', 'Ò', 'ᴚ', 'S',
    '⊥', '∩', 'Ʌ', 'M', 'X', '⅄', 'Z',
];

static mut RE_EXCLUDED: Option<Regex> = None;
static mut RE_AZ: Option<Regex> = None;

pub fn transform_dom(s: &str, flipped: bool, elongate: bool, with_markers: bool) -> Cow<str> {
    // Exclude access-keys and other single-char messages
    if s.len() == 1 {
        return s.into();
    }

    // XML entities (&#x202a;) and XML tags.
    let re_excluded =
        unsafe { RE_EXCLUDED.get_or_insert_with(|| Regex::new(r"&[#\w]+;|<\s*.+?\s*>").unwrap()) };

    let mut result = Cow::from(s);

    let mut pos = 0;
    let mut diff = 0;

    for cap in re_excluded.captures_iter(s) {
        let capture = cap.get(0).unwrap();

        let sub_len = capture.start() - pos;
        let range = pos..capture.start();
        let result_range = pos + diff..capture.start() + diff;
        let sub = &s[range.clone()];
        let transform_sub = transform(sub, false, true);
        diff += transform_sub.len() - sub_len;
        result
            .to_mut()
            .replace_range(result_range.clone(), &transform_sub);
        pos = capture.end();
    }
    let range = pos..s.len();
    let result_range = pos + diff..result.len();
    let transform_sub = transform(&s[range], flipped, elongate);
    result.to_mut().replace_range(result_range, &transform_sub);

    if with_markers {
        return Cow::from("[") + result + "]";
    }

    result
}

pub fn transform(s: &str, flipped: bool, elongate: bool) -> Cow<str> {
    let re_az = unsafe { RE_AZ.get_or_insert_with(|| Regex::new(r"[a-zA-Z]").unwrap()) };

    let (small_map, caps_map) = if flipped {
        (FLIPPED_SMALL_MAP, FLIPPED_CAPS_MAP)
    } else {
        (TRANSFORM_SMALL_MAP, TRANSFORM_CAPS_MAP)
    };

    re_az.replace_all(s, |caps: &Captures| {
        let ch = caps[0].chars().next().unwrap();
        let cc = ch as u8;
        if (97..=122).contains(&cc) {
            let pos = cc - 97;
            let new_char = small_map[pos as usize];
            // duplicate "a", "e", "o" and "u" to emulate ~30% longer text
            if elongate && (cc == 97 || cc == 101 || cc == 111 || cc == 117) {
                let mut s = new_char.to_string();
                s.push(new_char);
                s
            } else {
                new_char.to_string()
            }
        } else if (65..=90).contains(&cc) {
            let pos = cc - 65;
            let new_char = caps_map[pos as usize];
            new_char.to_string()
        } else {
            ch.to_string()
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let x = transform("Hello World", false, true);
        assert_eq!(x, "Ħeeŀŀoo Ẇoořŀḓ");

        let x = transform("Hello World", false, false);
        assert_eq!(x, "Ħeŀŀo Ẇořŀḓ");

        let x = transform("Hello World", true, false);
        assert_eq!(x, "Hǝʅʅo Moɹʅp");

        let x = transform("f", false, true);
        assert_eq!(x, "ƒ");
    }

    #[test]
    fn dom_test() {
        let x = transform_dom("Hello <a>World</a>", false, true, false);
        assert_eq!(x, "Ħeeŀŀoo <a>Ẇoořŀḓ</a>");

        let x = transform_dom("Hello <a>World</a> in <b>my</b> House.", false, true, false);
        assert_eq!(x, "Ħeeŀŀoo <a>Ẇoořŀḓ</a> iƞ <b>ḿẏ</b> Ħoouuşee.");

        // Use markers.
        let x = transform_dom("Hello World within markers", false, false, true);
        assert_eq!(x, "[Ħeŀŀo Ẇořŀḓ ẇiŧħiƞ ḿařķeřş]");

        // Don't touch single character values.
        let x = transform_dom("f", false, true, false);
        assert_eq!(x, "f");
    }
}