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
use crate::{Binary, Error};
use std::str::FromStr;

/// # Zero-width space (ZWSP)
///
/// `char` for the Unicode representation on the ZWSP character
///
/// ## Representations
///
/// Encoding | Code
/// --- | ---
/// Unicode | `U+200B`
/// HTML | `​` `​`
/// TeX | `\hskip0pt`
/// LaTex | `\hspace{0pt}`
/// groff | `\:`
///
/// ## Prohibited in URLs
///
/// ICANN rules prohibit domain names from including non-displayed characters
/// such as zero-width space, and most browsers prohibit their use within domain
/// names, because they can be used to create a homograph attack, where a malicious
/// URL is visually indistinguishable from a legitimate one.
///
/// ## Reference
///
/// [Wikipedia](https://en.wikipedia.org/wiki/Zero-width_space)
///
pub const ZERO_WIDTH_SPACE: (char, &str) = ('\u{200B}', "​");

/// # Zero-width non-joiner (ZWNJ)
///
/// `char` for the Unicode representation on the ZWNJ character
///
/// ## Representations
///
/// Encoding | Code
/// --- | ---
/// Unicode | `U+200C`
/// HTML | `‌` `‌`
///
/// ## Reference
///
/// [Wikipedia](https://en.wikipedia.org/wiki/Zero-width_non-joiner)
///
pub const ZERO_WIDTH_NON_JOINER: (char, &str) = ('\u{200C}', "‌");

/// # Zero-width joiner (ZWJ)
///
/// `char` for the Unicode representation on the ZWJ character
///
/// ## Representations
///
/// Encoding | Code
/// --- | ---
/// Unicode | `U+200D`
/// HTML | `‍` `‍`
///
/// ## Reference
///
/// [Wikipedia](https://en.wikipedia.org/wiki/Zero-width_joiner)
///
pub const ZERO_WIDTH_JOINER: (char, &str) = ('\u{200D}', "‍");

/// # Word joiner (WJ)
///
/// ## Representations
///
/// Encoding | Code
/// --- | ---
/// Unicode | `U+2060`
/// HTML | `⁠` `⁠`
///
/// ## Reference
///
/// [Wikipedia](https://en.wikipedia.org/wiki/Word_joiner)
///
pub const WORD_JOINER: (char, &str) = ('\u{2060}', "⁠");

/// # Zero-width No-Break Space (ZWNBSP)
///
/// `char` for the Unicode representation on the ZWNBSP character
///
/// ## Representations
///
/// Encoding | Code
/// --- | ---
/// Unicode | `U+FEFF`
/// HTML | ``
///
/// ## Reference
///
/// [Wikipedia](https://www.fileformat.info/info/unicode/char/feff/index.htm)
///
pub const ZERO_WIDTH_NO_BREAK_SPACE: (char, &str) = ('\u{FEFF}', "");

pub struct ZeroWidth(Binary);

impl From<Binary> for ZeroWidth {
    fn from(binary: Binary) -> Self {
        Self(binary)
    }
}

impl ZeroWidth {
    /// Creates a new `ZeroWidth` instance given a string slice
    pub fn new(string: &str) -> Result<Self, Error> {
        let binary = Binary::from_str(string)?;

        Ok(ZeroWidth::from(binary))
    }

    /// Retrieve the binary representation of the ASCII
    /// value provided
    pub fn get_binary_string(&self) -> String {
        self.0.to_string()
    }

    /// Creates the Unicode zero width character representation
    /// from the binary representation of the ASCII value
    pub fn to_unicode(&self) -> String {
        let string_value = self.get_binary_string();
        let mut zero_width = String::default();

        // split each ascii letter representation
        let chars: Vec<&str> = string_value.split(' ').collect();

        chars.into_iter().for_each(|character| {
            character.split("").for_each(|ch| {
                if ch.eq("0") {
                    zero_width.push(ZERO_WIDTH_SPACE.0);
                } else {
                    zero_width.push(ZERO_WIDTH_NON_JOINER.0);
                }
            });

            zero_width.push(ZERO_WIDTH_JOINER.0);
        });

        // remove trailing zero width character
        zero_width.pop();

        zero_width
    }

    /// Creates the Unicode zero width character representation
    /// from the binary representation of the ASCII value
    pub fn to_html(&self) -> String {
        let string_value = self.get_binary_string();
        let mut zero_width = String::default();

        // split each ascii letter representation
        let chars: Vec<&str> = string_value.split(' ').collect();

        chars.into_iter().for_each(|character| {
            character.split("").for_each(|ch| {
                if ch.eq("0") {
                    zero_width.push_str(ZERO_WIDTH_SPACE.1);
                } else {
                    zero_width.push_str(ZERO_WIDTH_NON_JOINER.1);
                }
            });

            zero_width.push_str(ZERO_WIDTH_JOINER.1);
        });

        // remove trailing zero width character
        zero_width.pop();

        zero_width
    }
}

#[cfg(test)]
mod test {
    use super::*;
    const RUSTACEANS_ZW_UNICODE: &str = "\u{200c}\u{200b}\u{200c}\u{200b}\u{200c}\u{200b}\u{200b}\u{200c}\u{200b}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200c}\u{200b}\u{200c}\u{200b}\u{200c}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200c}\u{200b}\u{200b}\u{200c}\u{200c}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200c}\u{200b}\u{200c}\u{200b}\u{200b}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200b}\u{200b}\u{200b}\u{200b}\u{200c}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200b}\u{200b}\u{200b}\u{200c}\u{200c}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200b}\u{200b}\u{200c}\u{200b}\u{200c}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200b}\u{200b}\u{200b}\u{200b}\u{200c}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200b}\u{200c}\u{200c}\u{200c}\u{200b}\u{200c}\u{200d}\u{200c}\u{200b}\u{200c}\u{200c}\u{200c}\u{200b}\u{200b}\u{200c}\u{200c}\u{200c}";
    const RUSTACEANS_ZW_HTML: &str = "&#8204;&#8203;&#8204;&#8203;&#8204;&#8203;&#8203;&#8204;&#8203;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8204;&#8203;&#8204;&#8203;&#8204;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8204;&#8203;&#8203;&#8204;&#8204;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8204;&#8203;&#8204;&#8203;&#8203;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8203;&#8203;&#8203;&#8203;&#8204;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8203;&#8203;&#8203;&#8204;&#8204;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8203;&#8203;&#8204;&#8203;&#8204;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8203;&#8203;&#8203;&#8203;&#8204;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8203;&#8204;&#8204;&#8204;&#8203;&#8204;&#8205;&#8204;&#8203;&#8204;&#8204;&#8204;&#8203;&#8203;&#8204;&#8204;&#8204;&#8205";

    #[test]
    fn it_zw_into_unicode() {
        let have = ZeroWidth::new("Rustaceans").unwrap().to_unicode();
        let want = RUSTACEANS_ZW_UNICODE.to_string();

        assert_eq!(have, want);
    }

    #[test]
    fn it_zw_into_html() {
        let have = ZeroWidth::new("Rustaceans").unwrap().to_html();
        let want = RUSTACEANS_ZW_HTML.to_string();

        assert_eq!(have, want);
    }
}