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
//! # ZW
//!
//! Utility for encoding and decoding text using zero-width characters.
//!
//! ## How it works
//!
//! Subject text is first converted to its binary representation (e.g. "foo" ->
//! "011001100110111101101111"), then each digit is replaced with a zero-width
//! character (specifically: `U+200B` and `U+200C`). Decoding is simply the
//! inverse of the same flow of operations.
//!
//! ## Example usage
//!
//! ```
//! use zw;
//! let encoded = zw::encode("Hello");
//! let decoded = zw::decode(&encoded);
//! assert_ne!("Hello", &encoded);
//! assert_eq!("Hello", &decoded);
//! ```

use std::iter::FromIterator;

/// Zero-width character to exchange with "0"
pub const ZW_0: char = '\u{200c}';
/// Zero-width character to exchange with "1"
pub const ZW_1: char = '\u{200b}';

fn str_to_bin(s: &str) -> String {
    s.as_bytes()
        .iter()
        .map(|b| format!("{:08b}", b))
        .collect::<Vec<_>>()
        .concat()
}

fn bin_to_zw(s: &str) -> String {
    let chars = s.chars()
        .filter_map(|c| match c {
            '0' => Some(ZW_0),
            '1' => Some(ZW_1),
            _ => None,
        })
        .collect::<Vec<_>>();

    String::from_iter(chars)
}

/// Encodes a string, returning a new one containing only zero-width characters
/// # Example
/// ```
/// let encoded = zw::encode("x");
/// assert_eq!(
///     &encoded,
///     "\u{200c}\u{200b}\u{200b}\u{200b}\u{200b}\u{200c}\u{200c}\u{200c}",
/// );
/// ```
pub fn encode<S: AsRef<str>>(s: S) -> String {
    let bin = str_to_bin(s.as_ref());
    bin_to_zw(&bin)
}

fn bin_to_str(s: &str) -> String {
    let out: Vec<u8> = s.chars()
        .collect::<Vec<char>>()
        .chunks(8)
        .filter_map(|b| {
            let s = String::from_iter(b);
            u8::from_str_radix(&s, 2).ok()
        })
        .collect();

    std::str::from_utf8(&out)
        .unwrap()
        .to_owned()
}

fn zw_to_bin(s: &str) -> String {
    let chars = s.chars()
        .filter_map(|c| match c {
            ZW_0 => Some('0'),
            ZW_1 => Some('1'),
            _ => None,
        })
        .collect::<Vec<_>>();
    String::from_iter(chars)
}

/// Decodes a zero-width character encoded string, returning a new one
/// # Example
/// ```
/// let decoded = zw::decode(
///     "\u{200c}\u{200b}\u{200b}\u{200b}\u{200b}\u{200c}\u{200c}\u{200c}"
/// );
/// assert_eq!(&decoded, "x");
/// ```
pub fn decode<S: AsRef<str>>(s: S) -> String {
    let bin = zw_to_bin(s.as_ref());
    bin_to_str(&bin)
}


#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn try_encode() {
        assert_eq!(encode("One"), "‌​‌‌​​​​‌​​‌​​​‌‌​​‌‌​‌​");
    }

    #[test]
    fn bin_test() {
        assert_eq!(str_to_bin("Hello"), "0100100001100101011011000110110001101111");
    }

    #[test]
    fn full_circle() {
        let s = "Test! 🍔";
        let enc = encode(s);
        let dec = decode(&enc);
        assert_eq!(dec, s);
    }

    #[test]
    fn encode_empty() {
        assert_eq!(encode(""), "");
    }

    #[test]
    fn decode_empty() {
        assert_eq!(decode(""), "");
    }

    #[test]
    fn decode_invalid() {
        assert_eq!(decode("asdf"), "");
    }

    #[test]
    fn double_cycle() {
        let enc = encode("Test");
        let enc2 = encode(&enc);
        let dec2 = decode(&enc2);
        let dec = decode(&dec2);

        assert_eq!(dec, "Test");
        assert_ne!(enc, enc2);
        assert_eq!(enc, dec2);
    }
}