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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::error::Error;
use crate::{
binary::BitVec,
context::{Context, ContextError},
decoder::Decoder,
encoder::{Capacity, Encoder, EncoderResult},
};
use log::trace;
use crate::binary::Bit;
use super::Method;
pub trait UnicodeSet {
fn get_set(&self) -> &[char];
fn size(&self) -> usize {
self.get_set().len()
}
fn get_character(&self, index: u32) -> Option<&char> {
let index = index as usize;
if index == 0 {
None
} else if index > self.size() {
panic!("Too large number for given unicode set - cannot encode this amount of bits");
} else {
self.get_set().get(index - 1)
}
}
fn character_to_bits(&self, chr: &char) -> u32 {
if let Some(pos) = self.get_set().iter().position(|x| x == chr) {
(pos + 1) as u32
} else {
0
}
}
}
pub const FULL_UNICODE_CHARACTER_SET: [char; 31] = [
'\u{0020}', '\u{2000}', '\u{2001}', '\u{2002}', '\u{2003}', '\u{2004}', '\u{2005}', '\u{2006}',
'\u{2007}', '\u{2009}', '\u{200A}', '\u{200B}', '\u{200C}', '\u{200D}', '\u{200E}', '\u{2028}',
'\u{202A}', '\u{202C}', '\u{202D}', '\u{202F}', '\u{205F}', '\u{2060}', '\u{2061}', '\u{2062}',
'\u{2063}', '\u{2064}', '\u{2066}', '\u{2068}', '\u{2069}', '\u{3000}', '\u{FEFF}',
];
pub struct FullUnicodeSet;
impl UnicodeSet for FullUnicodeSet {
fn get_set(&self) -> &[char] {
&FULL_UNICODE_CHARACTER_SET
}
}
pub struct TrailingUnicodeMethod<T: UnicodeSet> {
unicode_set: T,
}
impl Default for TrailingUnicodeMethod<FullUnicodeSet> {
fn default() -> Self {
Self::new(FullUnicodeSet {})
}
}
impl<T> TrailingUnicodeMethod<T>
where
T: UnicodeSet,
{
pub fn new(unicode_set: T) -> Self {
TrailingUnicodeMethod { unicode_set }
}
}
impl<T> Capacity for TrailingUnicodeMethod<T>
where
T: UnicodeSet,
{
fn bitrate(&self) -> usize {
let amount_of_bits = std::mem::size_of::<usize>() * 8;
amount_of_bits - self.unicode_set.size().leading_zeros() as usize
}
}
impl<T, E> Encoder<E> for TrailingUnicodeMethod<T>
where
T: UnicodeSet,
E: Context,
{
fn partial_encode(
&self,
context: &mut E,
data: &mut dyn Iterator<Item = Bit>,
) -> Result<EncoderResult, Box<dyn Error>> {
let set_capacity = self.bitrate();
let next_n_bits = data.take(set_capacity).collect::<Vec<Bit>>();
let amount_bits_taken = next_n_bits.len();
let mut number: u32 = BitVec::from(next_n_bits).into();
number <<= set_capacity - amount_bits_taken;
trace!(
"Took {} bits and assembled a number: {}",
set_capacity,
number
);
if let Some(character) = self.unicode_set.get_character(number) {
trace!(
"Putting unicode character {:?} at the end of the line",
character
);
context.get_current_text_mut()?.push(*character);
}
Ok(EncoderResult::Success)
}
}
impl<T, D> Decoder<D> for TrailingUnicodeMethod<T>
where
T: UnicodeSet,
D: Context,
{
fn partial_decode(&self, context: &D) -> Result<Vec<Bit>, ContextError> {
if let Some(character) = context.get_current_text()?.chars().last() {
let decoded_number = self.unicode_set.character_to_bits(&character);
trace!(
"Found {:?} at the end of the line, decoded into {}",
&character,
decoded_number
);
let data: Vec<Bit> = BitVec::from(decoded_number).into();
let data_length = data.len();
let data_iter = data.into_iter().skip(data_length - self.bitrate());
let decoded_data = data_iter.collect::<Vec<Bit>>();
return Ok(decoded_data);
}
Ok(BitVec::filled_with(0, self.bitrate()).into())
}
}
impl<T, E, D> Method<E, D> for TrailingUnicodeMethod<T>
where
T: UnicodeSet,
E: Context,
D: Context,
{
}