spinoso_string/enc/utf8/
mod.rs

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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
macro_rules! impl_partial_eq {
    ($lhs:ty, $rhs:ty) => {
        impl<'a, 'b> PartialEq<$rhs> for $lhs {
            #[inline]
            fn eq(&self, other: &$rhs) -> bool {
                let other: &[u8] = other.as_ref();
                PartialEq::eq(self.as_bytes(), other)
            }
        }

        impl<'a, 'b> PartialEq<$lhs> for $rhs {
            #[inline]
            fn eq(&self, other: &$lhs) -> bool {
                let this: &[u8] = self.as_ref();
                PartialEq::eq(this, other.as_bytes())
            }
        }
    };
}

macro_rules! impl_partial_eq_array {
    ($lhs:ty, $rhs:ty) => {
        impl<'a, 'b, const N: usize> PartialEq<$rhs> for $lhs {
            #[inline]
            fn eq(&self, other: &$rhs) -> bool {
                let other: &[u8] = other.as_ref();
                PartialEq::eq(self.as_bytes(), other)
            }
        }

        impl<'a, 'b, const N: usize> PartialEq<$lhs> for $rhs {
            #[inline]
            fn eq(&self, other: &$lhs) -> bool {
                let this: &[u8] = self.as_ref();
                PartialEq::eq(this, other.as_bytes())
            }
        }
    };
}

mod borrowed;
mod inspect;
mod owned;

pub use borrowed::Codepoints;
pub use borrowed::Utf8Str;
pub use inspect::Inspect;
pub use owned::Utf8String;

#[cfg(test)]
#[allow(clippy::invisible_characters)]
mod tests {
    use alloc::string::String;
    use alloc::vec::Vec;
    use core::str;

    use quickcheck::quickcheck;

    use super::{Utf8Str, Utf8String};

    const REPLACEMENT_CHARACTER_BYTES: [u8; 3] = [239, 191, 189];

    quickcheck! {
        fn fuzz_char_len_utf8_contents_utf8_string(contents: String) -> bool {
            let expected = contents.chars().count();
            let s = Utf8String::from(contents);
            s.char_len() == expected
        }

        fn fuzz_len_utf8_contents_utf8_string(contents: String) -> bool {
            let expected = contents.len();
            let s = Utf8String::from(contents);
            s.len() == expected
        }

        fn fuzz_char_len_binary_contents_utf8_string(contents: Vec<u8>) -> bool {
            if let Ok(utf8_contents) = str::from_utf8(&contents) {
                let expected = utf8_contents.chars().count();
                let s = Utf8String::from(contents);
                s.char_len() == expected
            } else {
                let expected_at_most = contents.len();
                let s = Utf8String::from(contents);
                s.char_len() <= expected_at_most
            }
        }

        fn fuzz_len_binary_contents_utf8_string(contents: Vec<u8>) -> bool {
            let expected = contents.len();
            let s = Utf8String::from(contents);
            s.len() == expected
        }
    }

    #[test]
    fn constructs_empty_buffer() {
        let s = Utf8String::from(Vec::new());
        assert_eq!(0, s.len());
    }

    #[test]
    fn char_len_empty() {
        let s = Utf8String::from("");
        assert_eq!(s.char_len(), 0);
    }

    #[test]
    fn char_len_ascii() {
        let s = Utf8String::from("Artichoke Ruby");
        assert_eq!(s.char_len(), 14);
    }

    #[test]
    fn char_len_emoji() {
        let s = Utf8String::from("๐Ÿ’Ž");
        assert_eq!(s.char_len(), 1);
        let s = Utf8String::from("๐Ÿ’Ž๐Ÿฆ€๐ŸŽ‰");
        assert_eq!(s.char_len(), 3);
        let s = Utf8String::from("a๐Ÿ’Žb๐Ÿฆ€c๐ŸŽ‰d");
        assert_eq!(s.char_len(), 7);
        // with invalid UTF-8 bytes
        let s = Utf8String::from(b"a\xF0\x9F\x92\x8E\xFFabc");
        assert_eq!(s.char_len(), 6);
    }

    #[test]
    fn char_len_unicode_replacement_character() {
        let s = Utf8String::from("๏ฟฝ");
        assert_eq!(s.char_len(), 1);
        let s = Utf8String::from("๏ฟฝ๏ฟฝ๏ฟฝ");
        assert_eq!(s.char_len(), 3);
        let s = Utf8String::from("a๏ฟฝb๏ฟฝc๏ฟฝd");
        assert_eq!(s.char_len(), 7);
        let s = Utf8String::from("๏ฟฝ๐Ÿ’Žb๐Ÿฆ€c๐ŸŽ‰๏ฟฝ");
        assert_eq!(s.char_len(), 7);
        // with invalid UFF-8 bytes
        let s = Utf8String::from(b"\xEF\xBF\xBD\xF0\x9F\x92\x8E\xFF\xEF\xBF\xBDab");
        assert_eq!(s.char_len(), 6);
        let s = Utf8String::from(REPLACEMENT_CHARACTER_BYTES);
        assert_eq!(s.char_len(), 1);
    }

    #[test]
    fn char_len_nul_byte() {
        let s = Utf8String::from(b"\x00");
        assert_eq!(s.char_len(), 1);
        let s = Utf8String::from(b"abc\x00");
        assert_eq!(s.char_len(), 4);
        let s = Utf8String::from(b"abc\x00xyz");
        assert_eq!(s.char_len(), 7);
    }

    #[test]
    fn char_len_invalid_utf8_byte_sequences() {
        let s = Utf8String::from(b"\x00\x00\xD8\x00");
        assert_eq!(s.char_len(), 4);
        let s = Utf8String::from(b"\xFF\xFE");
        assert_eq!(s.char_len(), 2);
    }

    #[test]
    fn char_len_binary() {
        let bytes = &[
            0xB3, 0x7E, 0x39, 0x70, 0x8E, 0xFD, 0xBB, 0x75, 0x62, 0x77, 0xE7, 0xDF, 0x6F, 0xF2, 0x76, 0x27, 0x81,
            0x9A, 0x3A, 0x9D, 0xED, 0x6B, 0x4F, 0xAE, 0xC4, 0xE7, 0xA1, 0x66, 0x11, 0xF1, 0x08, 0x1C,
        ];
        let s = Utf8String::from(bytes);
        assert_eq!(s.char_len(), 32);
        // Mixed binary and ASCII
        let bytes = &[
            b'?', b'!', b'a', b'b', b'c', 0xFD, 0xBB, 0x75, 0x62, 0x77, 0xE7, 0xDF, 0x6F, 0xF2, 0x76, 0x27, 0x81,
            0x9A, 0x3A, 0x9D, 0xED, 0x6B, 0x4F, 0xAE, 0xC4, 0xE7, 0xA1, 0x66, 0x11, 0xF1, 0x08, 0x1C,
        ];
        let s = Utf8String::from(bytes);
        assert_eq!(s.char_len(), 32);
    }

    #[test]
    fn char_len_mixed_ascii_emoji_invalid_bytes() {
        // ```
        // [2.6.3] > s = "๐Ÿฆ€abc๐Ÿ’Ž\xff"
        // => "๐Ÿฆ€abc๐Ÿ’Ž\xFF"
        // [2.6.3] > s.length
        // => 6
        // [2.6.3] > puts s.bytes.map{|b| "\\x#{b.to_s(16).upcase}"}.join
        // \xF0\x9F\xA6\x80\x61\x62\x63\xF0\x9F\x92\x8E\xFF
        // ```
        let s = Utf8String::from(b"\xF0\x9F\xA6\x80\x61\x62\x63\xF0\x9F\x92\x8E\xFF");
        assert_eq!(s.char_len(), 6);
    }

    #[test]
    fn char_len_utf8() {
        // https://github.com/minimaxir/big-list-of-naughty-strings/blob/894882e7/blns.txt#L147-L157
        let s = Utf8String::from("ฮฉโ‰ˆรงโˆšโˆซหœยตโ‰คโ‰ฅรท");
        assert_eq!(s.char_len(), 10);
        let s = Utf8String::from("รฅรŸโˆ‚ฦ’ยฉห™โˆ†หšยฌโ€ฆรฆ");
        assert_eq!(s.char_len(), 11);
        let s = Utf8String::from("ล“โˆ‘ยดยฎโ€ ยฅยจห†รธฯ€โ€œโ€˜");
        assert_eq!(s.char_len(), 12);
        let s = Utf8String::from("ยกโ„ขยฃยขโˆžยงยถโ€ขยชยบโ€“โ‰ ");
        assert_eq!(s.char_len(), 12);
        let s = Utf8String::from("ยธห›ร‡โ—Šฤฑหœร‚ยฏห˜ยฟ");
        assert_eq!(s.char_len(), 10);
        let s = Utf8String::from("ร…รรŽรหร“ร”๏ฃฟร’รšร†โ˜ƒ");
        assert_eq!(s.char_len(), 12);
        let s = Utf8String::from("ล’โ€žยดโ€ฐห‡รยจห†ร˜โˆโ€โ€™");
        assert_eq!(s.char_len(), 12);
        let s = Utf8String::from("`โ„โ‚ฌโ€นโ€บ๏ฌ๏ฌ‚โ€กยฐยทโ€šโ€”ยฑ");
        assert_eq!(s.char_len(), 13);
        let s = Utf8String::from("โ…›โ…œโ…โ…ž");
        assert_eq!(s.char_len(), 4);
        let s = Utf8String::from("ะะ‚ะƒะ„ะ…ะ†ะ‡ะˆะ‰ะŠะ‹ะŒะะŽะะะ‘ะ’ะ“ะ”ะ•ะ–ะ—ะ˜ะ™ะšะ›ะœะะžะŸะ ะกะขะฃะคะฅะฆะงะจะฉะชะซะฌะญะฎะฏะฐะฑะฒะณะดะตะถะทะธะนะบะปะผะฝะพะฟั€ัั‚ัƒั„ั…ั†ั‡ัˆั‰ัŠั‹ัŒััŽั");
        assert_eq!(s.char_len(), 79);
    }

    #[test]
    fn char_len_vmware_super_string() {
        // A super string recommended by VMware Inc. Globalization Team: can
        // effectively cause rendering issues or character-length issues to
        // validate product globalization readiness.
        //
        // https://github.com/minimaxir/big-list-of-naughty-strings/blob/894882e7/blns.txt#L202-L224
        let s = Utf8String::from("่กจใƒใ‚A้ท—ล’รฉ๏ผข้€รœรŸยชฤ…รฑไธ‚ใ€๐ €€");
        assert_eq!(s.char_len(), 17);
    }

    #[test]
    fn char_len_two_byte_chars() {
        // https://github.com/minimaxir/big-list-of-naughty-strings/blob/894882e7/blns.txt#L188-L196
        let s = Utf8String::from("็”ฐไธญใ•ใ‚“ใซใ‚ใ’ใฆไธ‹ใ•ใ„");
        assert_eq!(s.char_len(), 11);
        let s = Utf8String::from("ใƒ‘ใƒผใƒ†ใ‚ฃใƒผใธ่กŒใ‹ใชใ„ใ‹");
        assert_eq!(s.char_len(), 11);
        let s = Utf8String::from("ๅ’Œ่ฃฝๆผข่ชž");
        assert_eq!(s.char_len(), 4);
        let s = Utf8String::from("้ƒจ่ฝๆ ผ");
        assert_eq!(s.char_len(), 3);
        let s = Utf8String::from("์‚ฌํšŒ๊ณผํ•™์› ์–ดํ•™์—ฐ๊ตฌ์†Œ");
        assert_eq!(s.char_len(), 11);
        let s = Utf8String::from("์ฐฆ์ฐจ๋ฅผ ํƒ€๊ณ  ์˜จ ํŽฒ์‹œ๋งจ๊ณผ ์‘›๋‹ค๋ฆฌ ๋˜ ๋ฐฉ๊ฐํ•˜");
        assert_eq!(s.char_len(), 22);
        let s = Utf8String::from("็คพๆœƒ็ง‘ๅญธ้™ข่ชžๅญธ็ ”็ฉถๆ‰€");
        assert_eq!(s.char_len(), 10);
        let s = Utf8String::from("์šธ๋ž€๋ฐ”ํ† ๋ฅด");
        assert_eq!(s.char_len(), 5);
        let s = Utf8String::from("๐ œŽ๐ œฑ๐ น๐ ฑ“๐ ฑธ๐ ฒ–๐ ณ");
        assert_eq!(s.char_len(), 7);
    }

    #[test]
    fn char_len_space_chars() {
        // Whitespace: all the characters with category `Zs`, `Zl`, or `Zp` (in Unicode
        // version 8.0.0), plus `U+0009 (HT)`, `U+000B (VT)`, `U+000C (FF)`, `U+0085 (NEL)`,
        // and `U+200B` (ZERO WIDTH SPACE), which are in the C categories but are often
        // treated as whitespace in some contexts.
        //
        // This file unfortunately cannot express strings containing
        // `U+0000`, `U+000A`, or `U+000D` (`NUL`, `LF`, `CR`).
        //
        // The next line may appear to be blank or mojibake in some viewers.
        //
        // The next line may be flagged for "trailing whitespace" in some viewers.
        //
        // https://github.com/minimaxir/big-list-of-naughty-strings/blob/894882e7/blns.txt#L131
        let bytes = "	 ย… แš€โ€‚โ€ƒโ€‚โ€ƒโ€„โ€…โ€†โ€‡โ€ˆโ€‰โ€Šโ€‹โ€จโ€ฉโ€ฏโŸใ€€
";
        let s = Utf8String::from(bytes);
        assert_eq!(s.char_len(), 25);
    }

    #[test]
    fn casing_utf8_string_empty() {
        let mut s = Utf8String::from(b"");

        s.make_capitalized();
        assert_eq!(s, "");

        s.make_lowercase();
        assert_eq!(s, "");

        s.make_uppercase();
        assert_eq!(s, "");
    }

    #[test]
    fn casing_utf8_string_ascii() {
        let lower = Utf8String::from(b"abc");
        let mid_upper = Utf8String::from(b"aBc");
        let upper = Utf8String::from(b"ABC");
        let long = Utf8String::from(b"aBC, 123, ABC, baby you and me girl");

        let capitalize: fn(&Utf8String) -> Utf8String = |value: &Utf8String| {
            let mut value = value.clone();
            value.make_capitalized();
            value
        };
        let lowercase: fn(&Utf8String) -> Utf8String = |value: &Utf8String| {
            let mut value = value.clone();
            value.make_lowercase();
            value
        };
        let uppercase: fn(&Utf8String) -> Utf8String = |value: &Utf8String| {
            let mut value = value.clone();
            value.make_uppercase();
            value
        };

        assert_eq!(capitalize(&lower), "Abc");
        assert_eq!(capitalize(&mid_upper), "Abc");
        assert_eq!(capitalize(&upper), "Abc");
        assert_eq!(capitalize(&long), "Abc, 123, abc, baby you and me girl");

        assert_eq!(lowercase(&lower), "abc");
        assert_eq!(lowercase(&mid_upper), "abc");
        assert_eq!(lowercase(&upper), "abc");
        assert_eq!(lowercase(&long), "abc, 123, abc, baby you and me girl");

        assert_eq!(uppercase(&lower), "ABC");
        assert_eq!(uppercase(&mid_upper), "ABC");
        assert_eq!(uppercase(&upper), "ABC");
        assert_eq!(uppercase(&long), "ABC, 123, ABC, BABY YOU AND ME GIRL");
    }

    #[test]
    fn casing_utf8_string_utf8() {
        // Capitalization of รŸ (SS) differs from MRI:
        //
        // ```console
        // [2.6.3] > "รŸ".capitalize
        // => "Ss"
        // ```
        let sharp_s = Utf8String::from("รŸ");
        let tomorrow = Utf8String::from("ฮฑฯฯฮนฮฟ");
        let year = Utf8String::from("ฮญฯ„ฮฟฯ‚");
        // two-byte characters
        // https://github.com/minimaxir/big-list-of-naughty-strings/blob/894882e7/blns.txt#L198-L200
        let two_byte_chars = Utf8String::from("๐œ ๐”๐‡๐๐€๐ก๐‡๐“ ๐™๐Š๐ก๐๐“/๐๐‡๐—๐Š๐ค๐” ๐’๐‹๐— ๐’๐Œ ๐œ ๐ก๐€๐–๐‡๐ค๐“๐ ๐ฑ๐‘‚ ๐‘„ ๐”๐‡๐๐€๐ก๐‡๐“ ๐๐†๐…๐ค๐†๐š๐Š๐ก๐๐†๐“๐†");
        // Changes length when case changes
        // https://github.com/minimaxir/big-list-of-naughty-strings/blob/894882e7/blns.txt#L226-L232
        let varying_length = Utf8String::from("zศบศพ");
        // There doesn't appear to be any RTL scripts that have cases, but might as well make sure
        let rtl = Utf8String::from("ู…ุฑุญุจุง ุงู„ุฎุฑุดูˆู");

        let capitalize: fn(&Utf8String) -> Utf8String = |value: &Utf8String| {
            let mut value = value.clone();
            value.make_capitalized();
            value
        };
        let lowercase: fn(&Utf8String) -> Utf8String = |value: &Utf8String| {
            let mut value = value.clone();
            value.make_lowercase();
            value
        };
        let uppercase: fn(&Utf8String) -> Utf8String = |value: &Utf8String| {
            let mut value = value.clone();
            value.make_uppercase();
            value
        };

        assert_eq!(capitalize(&sharp_s), "SS");
        assert_eq!(capitalize(&tomorrow), "ฮ‘ฯฯฮนฮฟ");
        assert_eq!(capitalize(&year), "ฮˆฯ„ฮฟฯ‚");
        assert_eq!(
            capitalize(&two_byte_chars),
            "๐œ ๐ผ๐ฏ๐‘…๐จ๐‘‰๐ฏ๐ป ๐‘๐ฒ๐‘‰๐‘…๐ป/๐‘…๐ฏ๐ฟ๐ฒ๐‘Œ๐ผ ๐บ๐ณ๐ฟ ๐บ๐ด ๐‘„ ๐‘‰๐จ๐พ๐ฏ๐‘Œ๐ป๐‘… ๐ฑ๐‘‚ ๐‘„ ๐ผ๐ฏ๐‘…๐จ๐‘‰๐ฏ๐ป ๐ท๐ฎ๐ญ๐‘Œ๐ฎ๐‘‚๐ฒ๐‘‰๐‘…๐ฎ๐ป๐ฎ"
        );
        assert_eq!(capitalize(&varying_length), "Zโฑฅโฑฆ");
        assert_eq!(capitalize(&rtl), "ู…ุฑุญุจุง ุงู„ุฎุฑุดูˆู");

        assert_eq!(lowercase(&sharp_s), "รŸ");
        assert_eq!(lowercase(&tomorrow), "ฮฑฯฯฮนฮฟ");
        assert_eq!(lowercase(&year), "ฮญฯ„ฮฟฯ‚");
        assert_eq!(
            lowercase(&two_byte_chars),
            "๐‘„ ๐ผ๐ฏ๐‘…๐จ๐‘‰๐ฏ๐ป ๐‘๐ฒ๐‘‰๐‘…๐ป/๐‘…๐ฏ๐ฟ๐ฒ๐‘Œ๐ผ ๐บ๐ณ๐ฟ ๐บ๐ด ๐‘„ ๐‘‰๐จ๐พ๐ฏ๐‘Œ๐ป๐‘… ๐ฑ๐‘‚ ๐‘„ ๐ผ๐ฏ๐‘…๐จ๐‘‰๐ฏ๐ป ๐ท๐ฎ๐ญ๐‘Œ๐ฎ๐‘‚๐ฒ๐‘‰๐‘…๐ฎ๐ป๐ฎ"
        );
        assert_eq!(lowercase(&varying_length), "zโฑฅโฑฆ");
        assert_eq!(lowercase(&rtl), "ู…ุฑุญุจุง ุงู„ุฎุฑุดูˆู");

        assert_eq!(uppercase(&sharp_s), "SS");
        assert_eq!(uppercase(&tomorrow), "ฮ‘ฮŽฮกฮ™ฮŸ");
        assert_eq!(uppercase(&year), "ฮˆฮคฮŸฮฃ");
        assert_eq!(
            uppercase(&two_byte_chars),
            "๐œ ๐”๐‡๐๐€๐ก๐‡๐“ ๐™๐Š๐ก๐๐“/๐๐‡๐—๐Š๐ค๐” ๐’๐‹๐— ๐’๐Œ ๐œ ๐ก๐€๐–๐‡๐ค๐“๐ ๐‰๐š ๐œ ๐”๐‡๐๐€๐ก๐‡๐“ ๐๐†๐…๐ค๐†๐š๐Š๐ก๐๐†๐“๐†"
        );
        assert_eq!(uppercase(&varying_length), "Zศบศพ");
        assert_eq!(uppercase(&rtl), "ู…ุฑุญุจุง ุงู„ุฎุฑุดูˆู");
    }

    #[test]
    fn casing_utf8_string_invalid_utf8() {
        let mut s = Utf8String::from(b"\xFF\xFE");

        s.make_capitalized();
        assert_eq!(s, &b"\xFF\xFE"[..]);

        s.make_lowercase();
        assert_eq!(s, &b"\xFF\xFE"[..]);

        s.make_uppercase();
        assert_eq!(s, &b"\xFF\xFE"[..]);
    }

    #[test]
    fn casing_utf8_string_unicode_replacement_character() {
        let mut s = Utf8String::from("๏ฟฝ");

        s.make_capitalized();
        assert_eq!(s, "๏ฟฝ");

        s.make_lowercase();
        assert_eq!(s, "๏ฟฝ");

        s.make_uppercase();
        assert_eq!(s, "๏ฟฝ");
    }

    #[test]
    fn chr_does_not_return_more_than_one_byte_for_invalid_utf8() {
        // ```ruby
        // [3.0.1] > "\xF0\x9F\x87".chr
        // => "\xF0"
        // ```
        //
        // Per `bstr`:
        //
        // The bytes `\xF0\x9F\x87` could lead to a valid UTF-8 sequence, but 3 of them
        // on their own are invalid. Only one replacement codepoint is substituted,
        // which demonstrates the "substitution of maximal subparts" strategy.
        let s = Utf8String::from(b"\xF0\x9F\x87");
        assert_eq!(s.chr(), b"\xF0");
    }

    #[test]
    fn get_char_slice_valid_range() {
        let s = Utf8String::from(b"a\xF0\x9F\x92\x8E\xFF".to_vec()); // "a๐Ÿ’Ž\xFF"
        assert_eq!(s.get_char_slice(0..0), Some(Utf8Str::empty()));
        assert_eq!(s.get_char_slice(0..1), Some(Utf8Str::new(b"a")));
        assert_eq!(s.get_char_slice(0..2), Some(Utf8Str::new("a๐Ÿ’Ž")));
        assert_eq!(s.get_char_slice(0..3), Some(Utf8Str::new(b"a\xF0\x9F\x92\x8E\xFF")));
        assert_eq!(s.get_char_slice(0..4), Some(Utf8Str::new(b"a\xF0\x9F\x92\x8E\xFF")));
        assert_eq!(s.get_char_slice(1..1), Some(Utf8Str::empty()));
        assert_eq!(s.get_char_slice(1..2), Some(Utf8Str::new("๐Ÿ’Ž")));
        assert_eq!(s.get_char_slice(1..3), Some(Utf8Str::new(b"\xF0\x9F\x92\x8E\xFF")));
    }

    #[test]
    #[allow(clippy::reversed_empty_ranges)]
    fn get_char_slice_invalid_range() {
        let s = Utf8String::from(b"a\xF0\x9F\x92\x8E\xFF".to_vec()); // "a๐Ÿ’Ž\xFF"
        assert_eq!(s.get_char_slice(4..5), None);
        assert_eq!(s.get_char_slice(4..1), None);
        assert_eq!(s.get_char_slice(3..1), Some(Utf8Str::empty()));
        assert_eq!(s.get_char_slice(2..1), Some(Utf8Str::empty()));
        assert_eq!(s.get_char_slice(7..10), None);
        assert_eq!(s.get_char_slice(10..8), None);
        assert_eq!(s.get_char_slice(10..5), None);
        assert_eq!(s.get_char_slice(10..2), None);
    }

    #[test]
    fn index_with_default_offset() {
        let s = Utf8String::from("f๐Ÿ’Žoo");
        assert_eq!(s.index("f".as_bytes(), 0), Some(0));
        assert_eq!(s.index("o".as_bytes(), 0), Some(2));
        assert_eq!(s.index("oo".as_bytes(), 0), Some(2));
        assert_eq!(s.index("ooo".as_bytes(), 0), None);
    }

    #[test]
    fn index_with_different_offset() {
        let s = Utf8String::from("f๐Ÿ’Žoo");
        assert_eq!(s.index("o".as_bytes(), 1), Some(2));
        assert_eq!(s.index("o".as_bytes(), 2), Some(2));
        assert_eq!(s.index("o".as_bytes(), 3), Some(3));
        assert_eq!(s.index("o".as_bytes(), 4), None);
    }

    #[test]
    fn rindex_with_default_offset() {
        let s = Utf8String::from("f๐Ÿ’Žoo");
        assert_eq!(s.rindex("f".as_bytes(), 3), Some(0));
        assert_eq!(s.rindex("o".as_bytes(), 3), Some(3));
        assert_eq!(s.rindex("oo".as_bytes(), 3), Some(2));
        assert_eq!(s.rindex("ooo".as_bytes(), 3), None);
    }

    #[test]
    fn rindex_with_different_offset() {
        let s = Utf8String::from("f๐Ÿ’Žoo");
        assert_eq!(s.rindex("o".as_bytes(), 4), Some(3));
        assert_eq!(s.rindex("o".as_bytes(), 3), Some(3));
        assert_eq!(s.rindex("o".as_bytes(), 2), Some(2));
        assert_eq!(s.rindex("o".as_bytes(), 1), None);
        assert_eq!(s.rindex("o".as_bytes(), 0), None);
    }

    #[test]
    fn index_and_rindex_support_invalid_utf8_in_needle() {
        // Invalid UTF-8 in needle
        let needle = &"๐Ÿ’Ž".as_bytes()[..3];

        assert_eq!(Utf8String::from("f๐Ÿ’Žoo").index(needle, 0), None); // FIXME: Currently `Some(1)`
        assert_eq!(Utf8String::from("f๐Ÿ’Žoo").rindex(needle, 3), None); // FIXME: Currently `Some(1)`
    }

    #[test]
    fn index_and_rindex_support_invalid_utf8_in_haystack() {
        // Invalid UTF-8 in haystack
        let mut haystack = Vec::new();
        haystack.extend_from_slice(b"f");
        haystack.extend_from_slice(&"๐Ÿ’Ž".as_bytes()[..2]);
        haystack.extend_from_slice(b"oo");
        let haystack = Utf8String::from(haystack);

        assert_eq!(haystack.index("๐Ÿ’Ž".as_bytes(), 0), None);
        assert_eq!(haystack.rindex("๐Ÿ’Ž".as_bytes(), 3), None);
    }

    #[test]
    fn index_empties() {
        // ```console
        // [3.2.2] > "".index ""
        // => 0
        // [3.2.2] > "".index "a"
        // => nil
        // [3.2.2] > "a".index ""
        // => 0
        // ```
        let s = Utf8String::from("");
        assert_eq!(s.index(b"", 0), Some(0));

        assert_eq!(s.index(b"a", 0), None);

        let s = Utf8String::from("a");
        assert_eq!(s.index(b"", 0), Some(0));
    }

    #[test]
    fn rindex_empties() {
        // ```console
        // [3.2.2] > "".rindex ""
        // => 0
        // [3.2.2] > "".rindex "a"
        // => nil
        // [3.2.2] > "a".rindex ""
        // => 1
        // ```
        let s = Utf8String::from("");
        assert_eq!(s.rindex(b"", usize::MAX), Some(0));
        assert_eq!(s.rindex(b"", 1), Some(0));
        assert_eq!(s.rindex(b"", 0), Some(0));

        assert_eq!(s.rindex(b"a", usize::MAX), None);
        assert_eq!(s.rindex(b"a", 1), None);
        assert_eq!(s.rindex(b"a", 0), None);

        let s = Utf8String::from("a");
        assert_eq!(s.rindex(b"", usize::MAX), Some(1));
        assert_eq!(s.rindex(b"", 1), Some(1));
    }
}