x520-stringprep 1.0.0

String preparation algorithm as described in ITU-T Recommendation X.520
Documentation
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#![doc = include_str!("../README.md")]
#![no_std]
// Some things in this file were copied from `stringprep` crate.
// License at the time of copying:
// Copyright (c) 2017 The rust-stringprep Developers

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

mod rfc3454;

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::string::String;

use core::iter::{Filter, FlatMap, FusedIterator, Iterator, Map};
use core::slice::Iter;
use core::str::Chars;
use unicode_normalization::{Recompositions, UnicodeNormalization};

const REPLACEMENT_CHARACTER: char = '\u{FFFD}';

/// Determine if a character is an unassigned code point
///
/// This is not 100% correct: there are many more unassigned ranges; however,
/// this covers a huge range of them. This sloppy approximation is not done
/// out of laziness, but rather, in the interest of performance. Also, many
/// unassigned code points may become assigned in the future, so this less
/// strict check is a little more future-proof.
///
/// No new code points are even proposed for planes 4 to 13. Plane 3 is
/// entirely unassigned.
#[inline]
const fn is_unassigned(c: char) -> bool {
    c >= '\u{30000}' && c <= '\u{DFFFF}'
}

/// Determine if a character is a private use code point
#[inline]
const fn is_private_use(c: char) -> bool {
    matches!(c, '\u{E000}'..='\u{F8FF}' | '\u{F0000}'..='\u{FFFFD}' | '\u{100000}'..='\u{10FFFD}')
}

/// Determine if a character is a non-character code point
#[inline]
const fn is_non_char(c: char) -> bool {
    let bottom_nybble = c as u32 & 0xFFFF;
    if bottom_nybble >= 0xFFFE && bottom_nybble <= 0xFFFF {
        return true;
    }
    matches!(c, '\u{FDD0}'..='\u{FDEF}')
}

/// Determines if `c` is to be removed according to section 7.2 of
/// [ITU-T Recommendation X.520 (2019)](https://www.itu.int/rec/T-REC-X.520-201910-I/en).
#[inline]
fn x520_mapped_to_something(c: &char) -> bool {
    match *c {
        '\u{00AD}'
        | '\u{1806}'
        | '\u{034F}'
        | '\u{180B}'..='\u{180D}'
        | '\u{FE00}'..='\u{FE0F}'
        | '\u{FFFC}'
        | '\u{200B}' => false,
        // Technically control characters, but mapped to whitespace in X.520.
        '\u{09}' | '\u{0A}'..='\u{0D}' | '\u{85}' => true,
        _ => !c.is_control(),
    }
}

#[inline]
fn is_separator(c: char) -> bool {
    match c {
        | '\u{20}' // SPACE
        | '\u{a0}' // NO-BREAK SPACE
        | '\u{2028}' // LINE SEPARATOR
        | '\u{2029}' // PARAGRAPH SEPARATOR
        | '\u{1680}' // OGHAM SPACE MARK
        | '\u{2000}'..='\u{200a}' // Width-specific spaces
        | '\u{202f}' // NARROW NO-BREAK SPACE
        | '\u{205f}' // MEDIUM MATHEMATICAL SPACE
        | '\u{3000}' // IDEOGRAPHIC SPACE
        => true,
        _ => false,
    }
}

#[inline]
fn x520_map(c: char) -> char {
    match c {
        '\u{09}' | '\u{0A}'..='\u{0D}' | '\u{85}' => ' ',
        c => {
            if is_separator(c) {
                ' '
            } else {
                c
            }
        }
    }
}

/// B.2 Mapping for case-folding used with NFKC.
#[inline]
fn case_fold_for_nfkc(c: char) -> CaseFoldForNfkc {
    let inner = match rfc3454::B_2.binary_search_by_key(&c, |e| e.0) {
        Ok(idx) => FoldInner::Chars(rfc3454::B_2[idx].1.chars()),
        Err(_) => FoldInner::Char(Some(c)),
    };
    CaseFoldForNfkc(inner)
}

enum FoldInner {
    Chars(Chars<'static>),
    Char(Option<char>),
}

/// The iterator returned by `case_fold_for_nfkc`.
struct CaseFoldForNfkc(FoldInner);

impl Iterator for CaseFoldForNfkc {
    type Item = char;

    fn next(&mut self) -> Option<char> {
        match self.0 {
            FoldInner::Chars(ref mut it) => it.next(),
            FoldInner::Char(ref mut ch) => ch.take(),
        }
    }
}

impl FusedIterator for CaseFoldForNfkc {}

pub struct X520CaseExactStringPrepChars<I>
    where I: Iterator<Item = char> {
    s: Recompositions<Map<Filter<I, fn(&char) -> bool>, fn(char) -> char>>,
    previous_was_space: bool,
}

impl<I> X520CaseExactStringPrepChars<I>
    where I: Iterator<Item = char> {
    pub fn new(s: I) -> Self {
        X520CaseExactStringPrepChars {
            previous_was_space: false,
            s: s
                .filter(x520_mapped_to_something as fn(&char) -> bool)
                .map(x520_map as fn(_) -> _)
                .nfkc(),
        }
    }
}

impl<I> Iterator for X520CaseExactStringPrepChars<I>
    where I: Iterator<Item = char> {
    /// The error is returned if a prohibited character is encountered.
    type Item = Result<char, char>;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(c) = self.s.next() {
            if c == ' ' {
                if self.previous_was_space == true {
                    continue;
                } else {
                    self.previous_was_space = true;
                    return Some(Ok(' '));
                }
            }
            self.previous_was_space = false;
            // You don't have to check for Surrogate codes: it is not possible
            // in UTF-8 strings in Rust.
            if is_unassigned(c) || is_private_use(c) || is_non_char(c) || c == '\u{FFFD}' {
                // Prohibited character: matching MUST return UNDEFINED.
                return Some(Err(c));
            }
            return Some(Ok(c));
        }
        None
    }
}

impl<I> FusedIterator for X520CaseExactStringPrepChars<I>
    where I: Iterator<Item = char> {
}

pub struct X520CaseIgnoreStringPrepChars<I>
    where I: Iterator<Item = char> {
    s: Recompositions<
        FlatMap<
            Map<Filter<I, fn(&char) -> bool>, fn(char) -> char>,
            CaseFoldForNfkc,
            fn(char) -> CaseFoldForNfkc,
        >,
    >,
    previous_was_space: bool,
}

impl<I> FusedIterator for X520CaseIgnoreStringPrepChars<I>
    where I: Iterator<Item = char> {
}

impl<I> X520CaseIgnoreStringPrepChars<I>
    where I: Iterator<Item = char> {
    pub fn new(s: I) -> Self {
        X520CaseIgnoreStringPrepChars {
            previous_was_space: false,
            s: s
                .filter(x520_mapped_to_something as fn(&char) -> bool)
                .map(x520_map as fn(_) -> _)
                .flat_map(case_fold_for_nfkc as fn(_) -> _)
                .nfkc(),
        }
    }
}

impl<I> Iterator for X520CaseIgnoreStringPrepChars<I>
    where I: Iterator<Item = char> {
    /// The error is returned if a prohibited character is encountered.
    type Item = Result<char, char>;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(c) = self.s.next() {
            if c == ' ' {
                if self.previous_was_space == true {
                    continue;
                } else {
                    self.previous_was_space = true;
                    return Some(Ok(' '));
                }
            }
            self.previous_was_space = false;
            // You don't have to check for Surrogate codes: it is not possible
            // in UTF-8 strings in Rust.
            if is_unassigned(c) || is_private_use(c) || is_non_char(c) || c == '\u{FFFD}' {
                // Prohibited character: matching MUST return UNDEFINED.
                return Some(Err(c));
            }
            return Some(Ok(c));
        }
        None
    }
}

/// Iterate over characters string-prepped per ITU-T Recommendation X.520 (case-sensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_exact_str<'a>(s: &'a str) -> X520CaseExactStringPrepChars<Chars<'a>> {
    X520CaseExactStringPrepChars::new(s.chars())
}

/// Iterate over characters string-prepped per ITU-T Recommendation X.520 (case-insensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_ignore_str<'a>(s: &'a str) -> X520CaseIgnoreStringPrepChars<Chars<'a>> {
    X520CaseIgnoreStringPrepChars::new(s.chars())
}

/// Iterate over characters of a string-prepped `BMPString` per ITU-T Recommendation X.520 (case-sensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_exact_bmp<'a>(s: &'a [u16]) -> X520CaseExactStringPrepChars<Map<Iter<'a, u16>, fn(&u16) -> char>> {
    let it: Map<Iter<'a, u16>, fn(&u16) -> char> = s
        .iter()
        .map(|c| char::from_u32(*c as u32).unwrap_or(REPLACEMENT_CHARACTER));
    X520CaseExactStringPrepChars::new(it)
}

/// Iterate over characters of a string-prepped `BMPString` per ITU-T Recommendation X.520 (case-insensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_ignore_bmp<'a>(s: &'a [u16]) -> X520CaseIgnoreStringPrepChars<Map<Iter<'a, u16>, fn(&u16) -> char>> {
    let it: Map<Iter<'a, u16>, fn(&u16) -> char> = s
        .iter()
        .map(|c| char::from_u32(*c as u32).unwrap_or(REPLACEMENT_CHARACTER));
    X520CaseIgnoreStringPrepChars::new(it)
}

/// Iterate over characters of a string-prepped `UniversalString` per ITU-T Recommendation X.520 (case-sensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_exact_univ_str<'a>(s: &'a [u32]) -> X520CaseExactStringPrepChars<Map<Iter<'a, u32>, fn(&u32) -> char>> {
    let it: Map<Iter<'a, u32>, fn(&u32) -> char> = s
        .iter()
        .map(|c| char::from_u32(*c as u32).unwrap_or(REPLACEMENT_CHARACTER));
    X520CaseExactStringPrepChars::new(it)
}

/// Iterate over characters of a string-prepped `UniversalString` per ITU-T Recommendation X.520 (case-insensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_ignore_univ_str<'a>(s: &'a [u32]) -> X520CaseIgnoreStringPrepChars<Map<Iter<'a, u32>, fn(&u32) -> char>> {
    let it: Map<Iter<'a, u32>, fn(&u32) -> char> = s
        .iter()
        .map(|c| char::from_u32(*c).unwrap_or(REPLACEMENT_CHARACTER));
    X520CaseIgnoreStringPrepChars::new(it)
}

/// Check if a string is already string-prepped per ITU-T Recommendation X.520 (case-sensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
pub fn is_x520_stringprepped_case_exact_str(s: &str) -> bool {
    let mut chars = s.chars();
    let mut it = x520_stringprep_case_exact_str(s);
    while let Some(c) = it.next() {
        if c.is_err() {
            return false;
        }
        if chars.next() != Some(c.unwrap()) {
            return false;
        }
    }
    true
}

/// Check if a string is already string-prepped per ITU-T Recommendation X.520 (case-insensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
pub fn is_x520_stringprepped_case_ignore_str(s: &str) -> bool {
    let mut chars = s.chars();
    let mut it = x520_stringprep_case_ignore_str(s);
    while let Some(c) = it.next() {
        if c.is_err() {
            return false;
        }
        if chars.next() != Some(c.unwrap()) {
            return false;
        }
    }
    true
}

/// Normalize a string to case-exact form.
/// 
/// Returns an error if the string contains prohibited characters. The error
/// itself contains the prohibited character.
#[cfg(feature = "alloc")]
#[inline]
pub fn x520_stringprep_to_case_exact_string(s: &str) -> Result<String, char> {
    x520_stringprep_case_exact_str(s).collect()
}

/// Normalize a string to case-ignore form.
/// 
/// Returns an error if the string contains prohibited characters. The error
/// itself contains the prohibited character.
#[cfg(feature = "alloc")]
#[inline]
pub fn x520_stringprep_to_case_ignore_string(s: &str) -> Result<String, char> {
    x520_stringprep_case_ignore_str(s).collect()
}

/// Compare two strings for equality, string-prepped per ITU-T Recommendation X.520 (case-sensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_exact_compare(s1: &str, s2: &str) -> bool {
    x520_stringprep_case_exact_str(s1).eq(x520_stringprep_case_exact_str(s2))
}

/// Compare two strings for equality, string-prepped per ITU-T Recommendation X.520 (case-insensitive)
/// 
/// This does NOT trim leading or trailing whitespace: that is left to the caller.
#[inline]
pub fn x520_stringprep_case_ignore_compare(s1: &str, s2: &str) -> bool {
    x520_stringprep_case_ignore_str(s1).eq(x520_stringprep_case_ignore_str(s2))
}

#[cfg(test)]
mod tests {
    use super::{
        x520_stringprep_case_exact_str,
        x520_stringprep_case_ignore_str,
        x520_stringprep_case_exact_bmp,
        x520_stringprep_case_exact_univ_str,
    };
    extern crate alloc;
    use alloc::string::String;
    use alloc::vec::Vec;

    #[test]
    fn test_case_exact_stringprep_1() {
        let input = "Jonathan   Wilbur";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output.as_str(), "Jonathan Wilbur");
    }

    #[test]
    fn test_nfkc_normalization() {
        // Test NFKC normalization - combining characters should be normalized
        let input = "e\u{0301}"; // e + combining acute accent
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "é"); // Should be normalized to precomposed é

        // Test with multiple combining characters
        let input = "e\u{0301}\u{0300}"; // e + acute + grave
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "é\u{0300}"); // Should be normalized but may not combine further
    }

    #[test]
    fn test_whitespace_mapping() {
        // Test ASCII whitespace characters
        let input = "Hello\tWorld\nTest\r\nSpace";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World Test Space");

        // Test Unicode separator characters
        let input = "Hello\u{2000}World\u{2001}Test\u{2002}Space"; // Various Unicode spaces
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World Test Space");

        // Test mixed whitespace
        let input = "Hello\t\u{2000}World\n\u{2001}Test\r\u{2002}Space";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World Test Space");
    }

    #[test]
    fn test_space_consolidation() {
        // Test multiple consecutive spaces
        let input = "Hello    World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");

        // Test mixed whitespace consolidation
        let input = "Hello\t\t\n\n\r\rWorld";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");

        // Test Unicode spaces consolidation
        let input = "Hello\u{2000}\u{2001}\u{2002}World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");

        // Test mixed ASCII and Unicode spaces
        let input = "Hello \t\u{2000}\nWorld";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");
    }

    #[test]
    fn test_leading_trailing_spaces() {
        // Test leading spaces - function preserves one leading space
        let input = "   Hello World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, " Hello World");

        // Test trailing spaces - function preserves one trailing space
        let input = "Hello World   ";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World ");

        // Test both leading and trailing spaces
        let input = "   Hello World   ";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, " Hello World ");
    }

    #[test]
    fn test_prohibited_characters() {
        // // Test unassigned characters (should return Err)
        // let input = "Hello\u{0378}World"; // U+0378 is unassigned
        // let result: Result<String, char> = x520_stringprep_case_exact_str(input)
        //     .collect();
        // assert!(result.is_err());
        // assert_eq!(result.unwrap_err(), '\u{0378}');

        // Test private use characters (should return Err)
        let input = "Hello\u{E000}World"; // U+E000 is private use
        let result: Result<String, char> = x520_stringprep_case_exact_str(input).collect();
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), '\u{E000}');

        // Test non-character code points (should return Err)
        let input = "Hello\u{FDD0}World"; // U+FDD0 is non-character
        let result: Result<String, char> = x520_stringprep_case_exact_str(input).collect();
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), '\u{FDD0}');

        // Test replacement character (should return Err)
        let input = "Hello\u{FFFD}World";
        let result: Result<String, char> = x520_stringprep_case_exact_str(input).collect();
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), '\u{FFFD}');
    }

    #[test]
    fn test_control_characters() {
        // Test that control characters are mapped to spaces
        let input = "Hello\u{0009}World"; // Horizontal tab
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");

        // Test line feed
        let input = "Hello\u{000A}World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");

        // Test carriage return
        let input = "Hello\u{000D}World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");

        // Test next line
        let input = "Hello\u{0085}World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "Hello World");
    }

    #[test]
    fn test_filtered_characters() {
        // Test characters that should be filtered out (not mapped to space)
        let input = "Hello\u{00AD}World"; // Soft hyphen - should be filtered out
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "HelloWorld");

        // Test zero-width space
        let input = "Hello\u{200B}World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "HelloWorld");

        // Test object replacement character
        let input = "Hello\u{FFFC}World";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "HelloWorld");
    }

    #[test]
    fn test_complex_normalization() {
        // Test a complex case with multiple transformations
        let input = "  Hello\te\u{0301}\u{2000}Ä\u{FB03}n  ";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, " Hello é Äffin ");

        let output: String = x520_stringprep_case_ignore_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, " hello é äffin ");
    }

    #[test]
    fn test_empty_string() {
        let input = "";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "");

        let output: String = x520_stringprep_case_ignore_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, "");
    }

    #[test]
    fn test_only_spaces() {
        let input = "   \t\n\r   ";
        let output: String = x520_stringprep_case_exact_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, " ");

        let output: String = x520_stringprep_case_ignore_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output, " ");
    }

    #[test]
    fn test_case_ignore_stringprep_1() {
        let input = "Jonathan   Wilbur";
        let output: String = x520_stringprep_case_ignore_str(input)
            .map(|maybe_c| maybe_c.unwrap())
            .collect();
        assert_eq!(output.as_str(), "jonathan wilbur");
    }

    // This test is just to make sure we can use this for `BMPString`.
    #[test]
    fn test_bmp_string_1() {
        let input: Vec<u16> = "Jonathan   Wilbur".encode_utf16().collect();
        let output = x520_stringprep_case_exact_bmp(input.as_slice()).collect::<Result<String, char>>().unwrap();
        assert_eq!(output.as_str(), "Jonathan Wilbur");
    }

    // This test is just to make sure we can use this for `UniversalString`.
    #[test]
    fn test_univ_string_1() {
        let input: Vec<u32> = "Jonathan   Wilbur".chars().map(|c| c as u32).collect();
        let output = x520_stringprep_case_exact_univ_str(input.as_slice()).collect::<Result<String, char>>().unwrap();
        assert_eq!(output.as_str(), "Jonathan Wilbur");
    }

}