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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#![cfg_attr(feature = "no-unsafe", forbid(unsafe_code))]
mod chars;
mod rand_or_static;
use self::chars::is_zalgo_char;
pub use self::rand_or_static::RandOrStatic;
use rand::{
seq::SliceRandom,
SeedableRng,
};
#[derive(Debug)]
pub struct ZalgoBuilder {
pub up: RandOrStatic,
pub down: RandOrStatic,
pub mid: RandOrStatic,
}
impl ZalgoBuilder {
#[inline]
pub fn new() -> Self {
Self {
up: RandOrStatic::Rand { start: 0, end: 8 },
down: RandOrStatic::Rand { start: 0, end: 2 },
mid: RandOrStatic::Rand { start: 0, end: 8 },
}
}
#[inline]
pub fn set_up(&mut self, up: impl Into<RandOrStatic>) -> &mut Self {
self.up = up.into();
self
}
#[inline]
pub fn set_down(&mut self, down: impl Into<RandOrStatic>) -> &mut Self {
self.down = down.into();
self
}
#[inline]
pub fn set_mid(&mut self, mid: impl Into<RandOrStatic>) -> &mut Self {
self.mid = mid.into();
self
}
pub fn zalgoify(&self, input: &str) -> String {
let mut push_buf = [0; 4];
let mut rng = rand::rngs::SmallRng::from_entropy();
let up_num = self.up.generate_num(&mut rng);
let mid_num = self.mid.generate_num(&mut rng);
let down_num = self.down.generate_num(&mut rng);
let input_len = input.len();
let bytes_per_char = 1 + ((up_num + down_num + mid_num) * 2);
let estimated_len = input_len * bytes_per_char;
let mut ret = Vec::with_capacity(estimated_len);
for c in input.chars().filter(|c| !is_zalgo_char(*c)) {
if c.len_utf8() == 1 {
ret.push(c as u8);
} else {
for b in c.encode_utf8(&mut push_buf).as_bytes() {
ret.push(*b);
}
}
for _ in 0..up_num {
let bytes = *self::chars::ZALGO_UP_ENCODED
.choose(&mut rng)
.expect("`ZALGO_UP_ENCODED` is empty");
for b in bytes {
ret.push(b);
}
}
for _ in 0..mid_num {
let bytes = *self::chars::ZALGO_MID_ENCODED
.choose(&mut rng)
.expect("`ZALGO_MID_ENCODED` is empty");
for b in bytes {
ret.push(b);
}
}
for _ in 0..down_num {
let bytes = *self::chars::ZALGO_DOWN_ENCODED
.choose(&mut rng)
.expect("`ZALGO_DOWN_ENCODED` is empty");
for b in bytes {
ret.push(b);
}
}
}
#[cfg(not(feature = "no-unsafe"))]
unsafe {
String::from_utf8_unchecked(ret)
}
#[cfg(feature = "no-unsafe")]
String::from_utf8(ret).expect("vec should be utf8")
}
}
impl Default for ZalgoBuilder {
fn default() -> Self {
Self::new()
}
}
#[inline]
pub fn zalgoify(input: &str) -> String {
ZalgoBuilder::new().zalgoify(input)
}
#[cfg(test)]
mod test {
use super::*;
use std::time::Instant;
fn is_zalgo_char_version_2(c: char) -> bool {
if crate::chars::ZALGO_UP.binary_search(&c).is_ok() {
return true;
}
if crate::chars::ZALGO_DOWN.binary_search(&c).is_ok() {
return true;
}
if crate::chars::ZALGO_MID.binary_search(&c).is_ok() {
return true;
}
false
}
#[test]
fn basic_zalgoify_works() {
let ret = zalgoify("Hello World!");
println!("{}", ret);
assert!(!ret.is_empty());
}
#[test]
fn zalgoify_builder_works() {
let mut zalgo_builder = ZalgoBuilder::new();
zalgo_builder.set_up(0..100).set_down(0).set_mid(0);
let ret = zalgo_builder.zalgoify("Hello World!");
println!("{}", ret);
assert!(!ret.is_empty());
}
#[test]
fn zalgo_noop_works() {
let mut zalgo_builder = ZalgoBuilder::new();
zalgo_builder.set_up(0).set_down(0).set_mid(0);
let test = "Hello World!";
assert_eq!(test, zalgo_builder.zalgoify("Hello World!"));
}
#[test]
fn zalgo_bench() {
let data = "Hello World!".repeat(12);
let start = Instant::now();
zalgoify(&data);
let elapsed = start.elapsed();
println!("Time: {:?}", elapsed);
}
#[test]
fn test_is_zalgo_char() {
for i in 0..u32::MAX {
if let Ok(c) = char::try_from(i) {
let is_zalgo_char_version_2_result = is_zalgo_char_version_2(c);
let is_zalgo_char_result = is_zalgo_char(c);
assert!(
is_zalgo_char_version_2_result == is_zalgo_char_result,
"failed on {:x?} ({:b}), expected {}, got {}",
c,
u32::from(c),
is_zalgo_char_version_2_result,
is_zalgo_char_result,
);
}
}
}
}