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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
extern crate alloc;
use Cow;
use from_utf8;
pub use DecodingError;
use contains_utf8_4_byte_char_header;
use ;
/// Converts a slice of bytes to a string, including invalid characters.
///
/// The algorithm is as follows:
///
/// - If the input is valid UTF-8, but also valid CESU-8, the function will
/// return <code>[Cow::Borrowed]\(&[str]\)</code>.
/// - If the input is valid CESU-8, but not valid UTF-8, the function will
/// return <code>[Cow::Owned]\([String]\)</code>. This case has the potential
/// to panic.
/// - If the input is not valid CESU-8, the function will return
/// <code>[Cow::Owned]\([String]\)</code>, where the best attempt at decoding
/// the input strictly as CESU-8 will be made, with any invalid bytes being
/// replaced with the [U+FFFD REPLACEMENT CHARACTER] (�). This case has the
/// potential to panic.
///
/// **NOTE:** This function is significantly slower than [`decode_lossy`], but
/// is more "correct" in the sense that valid UTF-8 that is not valid CESU-8
/// will be treated as invalid input and properly replaced with the [U+FFFD
/// REPLACEMENT CHARACTER] (�). If you don't need this guarantee, and can
/// tolerate valid UTF-8 that is not valid CESU-8, use [`decode_lossy`] instead.
///
/// [U+FFFD REPLACEMENT CHARACTER]: char::REPLACEMENT_CHARACTER
///
/// # Panics
///
/// This function will panic if the buffer required to decode the input exceeds
/// [`isize::MAX`] bytes.
///
/// # Examples
///
/// If the input is valid UTF-8, but also valid CESU-8, the function will return
/// a borrowed string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
///
/// let bytes = b"Hello, world!";
/// let decoded = simd_cesu8::decode_lossy_strict(bytes);
/// assert_eq!(decoded, Cow::Borrowed("Hello, world!"));
/// ```
///
/// If the input is valid CESU-8, but not valid UTF-8, the function will return
/// an owned Rust string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::string::String;
///
/// let bytes = [0xed, 0xa0, 0xbd, 0xed, 0xb2, 0x96];
/// let decoded = simd_cesu8::decode_lossy_strict(&bytes);
/// assert_eq!(decoded, Cow::<str>::Owned(String::from("💖")));
/// ```
///
/// If the input is not valid CESU-8, the function will return an owned Rust
/// string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::string::String;
///
/// // NOTE: This is an unpaired surrogate followed by a valid CESU-8 surrogate
/// // pair.
/// let bytes = [0xed, 0xa0, 0xbd, 0xed, 0xa0, 0xbd, 0xed, 0xb2, 0x96];
/// let decoded = simd_cesu8::decode_lossy_strict(&bytes);
/// assert_eq!(decoded, Cow::<str>::Owned(String::from("���💖")));
/// ```
///
/// Unlike [`decode_lossy`], this function will treat valid UTF-8 that is not
/// valid CESU-8 as invalid input:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::string::String;
///
/// // NOTE: This is a valid UTF-8 string, but not valid CESU-8.
/// let bytes = [0xf0, 0x9f, 0x92, 0x96];
/// assert_eq!(core::str::from_utf8(&bytes), Ok("💖"));
/// let decoded = simd_cesu8::decode_lossy_strict(&bytes);
/// assert_eq!(decoded, Cow::<str>::Owned(String::from("����")));
/// ```
/// Converts a slice of bytes to a string, including invalid characters.
///
/// The algorithm is as follows:
///
/// - If the input is valid UTF-8, but invalid CESU-8, the function will return
/// <code>[Cow::Borrowed]\(&[str]\)</code>.
/// - If the input is valid CESU-8, but also valid UTF-8, the function will
/// return <code>[Cow::Borrowed]\(&[str]\)</code>.
/// - If the input is valid CESU-8, but not valid UTF-8, the function will
/// return <code>[Cow::Owned]\([String]\)</code>. This case has the potential
/// to panic.
/// - If the input is not valid CESU-8 or UTF-8, the function will return
/// <code>[Cow::Owned]\([String]\)</code>, where the best attempt at decoding
/// the input strictly as CESU-8 will be made, with any invalid bytes being
/// replaced with the [U+FFFD REPLACEMENT CHARACTER] (�). This case has the
/// potential to panic.
///
/// **NOTE:** The first case may be interpreted as an error, but it's actually
/// a significant performance optimization. If you need to strictly enforce
/// CESU-8 decoding, use [`decode_lossy_strict`] instead.
///
/// [U+FFFD REPLACEMENT CHARACTER]: char::REPLACEMENT_CHARACTER
///
/// # Panics
///
/// This function will panic if the buffer required to decode the input exceeds
/// [`isize::MAX`] bytes.
///
/// # Examples
///
/// If the input is valid UTF-8, but invalid CESU-8, the function will return a
/// borrowed string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
///
/// // NOTE: This is a valid UTF-8 string, but not valid CESU-8.
/// let bytes = [0xf0, 0x9f, 0x92, 0x96];
/// let decoded = simd_cesu8::decode_lossy(&bytes);
/// assert_eq!(decoded, Cow::Borrowed("💖"));
/// ```
///
/// If the input is valid CESU-8, but also valid UTF-8, the function will return
/// a borrowed string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
///
/// let bytes = b"Hello, world!";
/// let decoded = simd_cesu8::decode_lossy(bytes);
/// assert_eq!(decoded, Cow::Borrowed("Hello, world!"));
/// ```
///
/// If the input is valid CESU-8, but not valid UTF-8, the function will return
/// an owned Rust string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::string::String;
///
/// let bytes = [0xed, 0xa0, 0xbd, 0xed, 0xb2, 0x96];
/// let decoded = simd_cesu8::decode_lossy(&bytes);
/// assert_eq!(decoded, Cow::<str>::Owned(String::from("💖")));
/// ```
///
/// If the input is not valid CESU-8 or UTF-8, the function will return an owned
/// Rust string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::string::String;
///
/// // NOTE: This is an unpaired surrogate followed by a valid CESU-8 surrogate
/// // pair.
/// let bytes = [0xed, 0xa0, 0xbd, 0xed, 0xa0, 0xbd, 0xed, 0xb2, 0x96];
/// let decoded = simd_cesu8::decode_lossy(&bytes);
/// assert_eq!(decoded, Cow::<str>::Owned(String::from("���💖")));
/// ```
/// Converts a slice of bytes to a string.
///
/// The algorithm is as follows:
///
/// - If the input is valid CESU-8, but also valid UTF-8, the function will
/// return <code>[Cow::Borrowed]\(&[str]\)</code>.
/// - If the input is valid CESU-8, but not valid UTF-8, the function will
/// return <code>[Cow::Owned]\([String]\)</code>. This case has the potential
/// to panic.
///
/// # Errors
///
/// If the input is not valid CESU-8, this function will return a
/// [`DecodingError`].
///
/// # Panics
///
/// This function will panic if the buffer required to decode the input exceeds
/// [`isize::MAX`] bytes.
///
/// # Examples
///
/// If the input is valid CESU-8, but also valid UTF-8, the function will return
/// a borrowed string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
///
/// # fn main() -> Result<(), simd_cesu8::DecodingError> {
/// let bytes = b"Hello, world!";
/// let decoded = simd_cesu8::decode_strict(bytes)?;
/// assert_eq!(decoded, Cow::Borrowed("Hello, world!"));
/// # Ok(())
/// # }
/// ```
///
/// If the input is valid CESU-8, but not valid UTF-8, the function will return
/// an owned Rust string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::string::String;
///
/// # fn main() -> Result<(), simd_cesu8::DecodingError> {
/// let bytes = [0xed, 0xa0, 0xbd, 0xed, 0xb2, 0x96];
/// let decoded = simd_cesu8::decode_strict(&bytes)?;
/// assert_eq!(decoded, Cow::<str>::Owned(String::from("💖")));
/// # Ok(())
/// # }
/// ```
///
/// Unlike [`decode`], this function will treat valid UTF-8 that is not valid
/// CESU-8 as invalid input:
///
/// ```
/// // NOTE: This is a valid UTF-8 string, but not valid CESU-8.
/// let bytes = [0xf0, 0x9f, 0x92, 0x96];
/// assert_eq!(core::str::from_utf8(&bytes), Ok("💖"));
///
/// let result = simd_cesu8::decode_strict(&bytes);
/// assert!(result.is_err());
/// ```
/// Converts a slice of bytes to a string.
///
/// The algorithm is as follows:
///
/// - If the input is valid UTF-8, but invalid CESU-8, the function will return
/// <code>[Cow::Borrowed]\(&[str]\)</code>.
/// - If the input is valid CESU-8, but also valid UTF-8, the function will
/// return <code>[Cow::Borrowed]\(&[str]\)</code>.
/// - If the input is valid CESU-8, but not valid UTF-8, the function will
/// return <code>[Cow::Owned]\([String]\)</code>. This case has the potential
/// to panic.
///
/// **NOTE:** The first case may be interpreted as an error, but it's actually
/// a significant performance optimization. If you need to strictly enforce
/// CESU-8 decoding, use [`decode_strict`] instead.
///
/// # Errors
///
/// If the input is not valid CESU-8 or UTF-8, this function will return a
/// [`DecodingError`].
///
/// # Panics
///
/// This function will panic if the buffer required to decode the input exceeds
/// [`isize::MAX`] bytes.
///
/// # Examples
///
/// If the input is valid UTF-8, but invalid CESU-8, the function will return a
/// borrowed string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
///
/// # fn main() -> Result<(), simd_cesu8::DecodingError> {
/// // NOTE: This is a valid UTF-8 string, but not valid CESU-8.
/// let bytes = [0xf0, 0x9f, 0x92, 0x96];
/// let decoded = simd_cesu8::decode(&bytes)?;
/// assert_eq!(decoded, Cow::Borrowed("💖"));
/// # Ok(())
/// # }
/// ```
///
/// If the input is valid CESU-8, but also valid UTF-8, the function will return
/// a borrowed string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
///
/// # fn main() -> Result<(), simd_cesu8::DecodingError> {
/// let bytes = b"Hello, world!";
/// let decoded = simd_cesu8::decode(bytes)?;
/// assert_eq!(decoded, Cow::Borrowed("Hello, world!"));
/// # Ok(())
/// # }
/// ```
///
/// If the input is valid CESU-8, but not valid UTF-8, the function will return
/// an owned Rust string:
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
/// use alloc::string::String;
///
/// # fn main() -> Result<(), simd_cesu8::DecodingError> {
/// let bytes = [0xed, 0xa0, 0xbd, 0xed, 0xb2, 0x96];
/// let decoded = simd_cesu8::decode(&bytes)?;
/// assert_eq!(decoded, Cow::<str>::Owned(String::from("💖")));
/// # Ok(())
/// # }
/// ```
/// Encodes a string to CESU-8.
///
/// The algorithm is as follows:
///
/// - If the input, as UTF-8, is also valid CESU-8, the function will return
/// <code>[Cow::Borrowed]\([&\[u8\]][slice]\)</code>.
/// - If the input, as UTF-8, is not valid CESU-8, the function will return
/// <code>[Cow::Owned]\([Vec]<[u8]>\)</code>. This case has the potential to
/// panic.
///
/// # Panics
///
/// This function will panic if the buffer required to encode the input exceeds
/// [`isize::MAX`] bytes.
///
/// # Examples
///
/// ```
/// # extern crate alloc;
/// use alloc::borrow::Cow;
///
/// let single_byte = "\u{0045}";
/// assert_eq!(single_byte, "E");
/// assert_eq!(single_byte.len(), 1);
/// assert_eq!(single_byte.as_bytes(), &[0x45]);
/// assert_eq!(simd_cesu8::encode(single_byte), Cow::Borrowed(&[0x45]));
///
/// let two_bytes = "\u{0205}";
/// assert_eq!(two_bytes, "ȅ");
/// assert_eq!(two_bytes.len(), 2);
/// assert_eq!(two_bytes.as_bytes(), &[0xc8, 0x85]);
/// assert_eq!(simd_cesu8::encode(two_bytes), Cow::Borrowed(&[0xc8, 0x85]));
///
/// let three_bytes = "\u{20ac}";
/// assert_eq!(three_bytes, "€");
/// assert_eq!(three_bytes.len(), 3);
/// assert_eq!(three_bytes.as_bytes(), &[0xe2, 0x82, 0xac]);
/// assert_eq!(
/// simd_cesu8::encode(three_bytes),
/// Cow::Borrowed(&[0xe2, 0x82, 0xac])
/// );
///
/// let four_bytes = "\u{10400}";
/// assert_eq!(four_bytes, "𐐀");
/// assert_eq!(four_bytes.len(), 4);
/// assert_eq!(four_bytes.as_bytes(), &[0xf0, 0x90, 0x90, 0x80]);
/// assert_eq!(
/// simd_cesu8::encode(four_bytes),
/// Cow::<[u8]>::Owned(vec![0xed, 0xa0, 0x81, 0xed, 0xb0, 0x80])
/// );
/// ```
/// Returns `true` if the input string needs to be encoded to CESU-8.
///
/// # Examples
///
/// ```
/// let one = "E";
/// assert_eq!(one.len(), 1);
/// assert!(!simd_cesu8::needs_encoded(one));
///
/// let two = "ȅ";
/// assert_eq!(two.len(), 2);
/// assert!(!simd_cesu8::needs_encoded(two));
///
/// let three = "€";
/// assert_eq!(three.len(), 3);
/// assert!(!simd_cesu8::needs_encoded(three));
///
/// let four = "𐐀";
/// assert_eq!(four.len(), 4);
/// assert!(simd_cesu8::needs_encoded(four));
/// ```