normalized-path 0.0.4

Opinionated cross-platform, optionally case-insensitive path normalization
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
use alloc::borrow::Cow;

use icu_casemap::CaseMapper;
use icu_normalizer::{ComposingNormalizer, DecomposingNormalizer};
use icu_properties::props::{CanonicalCombiningClass, WhiteSpace};
use icu_properties::{CodePointMapData, CodePointSetData};

/// NFD normalization (canonical decomposition).
#[must_use]
pub fn nfd(s: &str) -> Cow<'_, str> {
    DecomposingNormalizer::new_nfd().normalize(s)
}

/// NFC normalization (canonical composition).
#[must_use]
pub fn nfc(s: &str) -> Cow<'_, str> {
    ComposingNormalizer::new_nfc().normalize(s)
}

/// Unicode `toCasefold()`.
#[must_use]
pub fn case_fold(s: &str) -> Cow<'_, str> {
    CaseMapper::new().fold_string(s)
}

/// Unicode `White_Space` property check.
#[must_use]
pub fn is_whitespace(c: char) -> bool {
    CodePointSetData::new::<WhiteSpace>().contains(c)
}

/// Whether `c` has Canonical Combining Class 0 (a "starter" — not a combining mark).
#[must_use]
pub fn is_starter(c: char) -> bool {
    CodePointMapData::<CanonicalCombiningClass>::new().get(c)
        == CanonicalCombiningClass::NotReordered
}

/// Whether `c` has Canonical Combining Class 230 (Above).
#[must_use]
pub fn is_above(c: char) -> bool {
    CodePointMapData::<CanonicalCombiningClass>::new().get(c) == CanonicalCombiningClass::Above
}

#[cfg(test)]
mod tests {
    use alloc::borrow::Cow;

    #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
    use wasm_bindgen_test::wasm_bindgen_test as test;

    use super::{case_fold, is_starter, is_whitespace, nfc, nfd};

    // --- nfd / nfc ---

    #[test]
    fn nfd_decomposes() {
        assert_eq!(nfd("\u{00E9}"), "e\u{0301}");
    }

    #[test]
    fn nfc_composes() {
        assert_eq!(nfc("e\u{0301}"), "\u{00E9}");
    }

    #[test]
    fn nfd_ascii_unchanged() {
        let result = nfd("hello");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "hello");
    }

    #[test]
    fn nfc_ascii_unchanged() {
        let result = nfc("hello");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "hello");
    }

    #[test]
    fn nfd_idempotent() {
        let s = "e\u{0301}\u{00F1}\u{00E9}";
        assert_eq!(nfd(&nfd(s)), nfd(s));
    }

    #[test]
    fn nfc_idempotent() {
        let s = "e\u{0301}\u{00F1}\u{00E9}";
        assert_eq!(nfc(&nfc(s)), nfc(s));
    }

    #[test]
    fn nfd_then_nfc_equals_nfc() {
        let s = "e\u{0301}\u{00F1}\u{00E9}";
        assert_eq!(nfc(&nfd(s)), nfc(s));
    }

    #[test]
    fn nfc_then_nfd_equals_nfd() {
        let s = "e\u{0301}\u{00F1}\u{00E9}";
        assert_eq!(nfd(&nfc(s)), nfd(s));
    }

    // --- case_fold ---

    #[test]
    fn case_fold_ascii_lowercase() {
        assert_eq!(case_fold("HELLO"), "hello");
    }

    #[test]
    fn case_fold_ascii_unchanged() {
        let result = case_fold("hello");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "hello");
    }

    #[test]
    fn case_fold_mixed_case() {
        assert_eq!(case_fold("HeLLo"), "hello");
    }

    #[test]
    fn case_fold_german_eszett() {
        assert_eq!(case_fold("\u{00DF}"), "ss");
    }

    #[test]
    fn case_fold_turkish_dotted_i() {
        let folded = case_fold("\u{0130}");
        assert_eq!(folded, "i\u{0307}");
    }

    #[test]
    fn case_fold_greek_sigma() {
        assert_eq!(case_fold("\u{03A3}"), "\u{03C3}");
        assert_eq!(case_fold("\u{03C2}"), "\u{03C3}");
    }

    // --- is_whitespace ---

    #[test]
    fn is_whitespace_space() {
        assert!(is_whitespace(' '));
    }

    #[test]
    fn is_whitespace_tab() {
        assert!(is_whitespace('\t'));
    }

    #[test]
    fn is_whitespace_newline() {
        assert!(is_whitespace('\n'));
    }

    #[test]
    fn is_whitespace_ideographic_space() {
        assert!(is_whitespace('\u{3000}'));
    }

    #[test]
    fn is_whitespace_no_break_space() {
        assert!(is_whitespace('\u{00A0}'));
    }

    #[test]
    fn is_whitespace_ascii_letter() {
        assert!(!is_whitespace('a'));
    }

    #[test]
    fn is_whitespace_digit() {
        assert!(!is_whitespace('0'));
    }

    #[test]
    fn is_whitespace_combining_mark() {
        assert!(!is_whitespace('\u{0301}'));
    }

    // --- is_starter ---

    #[test]
    fn is_starter_ascii_letter() {
        assert!(is_starter('a'));
    }

    #[test]
    fn is_starter_digit() {
        assert!(is_starter('0'));
    }

    #[test]
    fn is_starter_space() {
        assert!(is_starter(' '));
    }

    #[test]
    fn is_starter_combining_acute() {
        assert!(!is_starter('\u{0301}'));
    }

    #[test]
    fn is_starter_combining_cedilla() {
        assert!(!is_starter('\u{0327}'));
    }

    #[test]
    fn is_starter_combining_dot_above() {
        assert!(!is_starter('\u{0307}'));
    }

    #[test]
    fn is_starter_cjk() {
        assert!(is_starter(''));
    }

    // --- Non-standard characters (unassigned, PUA, noncharacters) ---

    // U+0378: unassigned in Greek block
    // U+E000: Private Use Area
    // U+FDD0: noncharacter
    // U+FFFE: noncharacter
    // U+FFFF: noncharacter
    // U+10FFFF: noncharacter (last valid code point)

    #[test]
    fn nfd_unassigned_unchanged() {
        let result = nfd("\u{0378}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{0378}");
    }

    #[test]
    fn nfd_pua_unchanged() {
        let result = nfd("\u{E000}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{E000}");
    }

    #[test]
    fn nfd_noncharacter_unchanged() {
        for c in ['\u{FDD0}', '\u{FFFE}', '\u{FFFF}', '\u{10FFFF}'] {
            let s = alloc::string::String::from(c);
            let result = nfd(&s);
            assert!(matches!(result, Cow::Borrowed(_)), "NFD changed {c:?}");
            assert_eq!(result.chars().next(), Some(c));
        }
    }

    #[test]
    fn nfc_unassigned_unchanged() {
        let result = nfc("\u{0378}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{0378}");
    }

    #[test]
    fn nfc_pua_unchanged() {
        let result = nfc("\u{E000}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{E000}");
    }

    #[test]
    fn nfc_noncharacter_unchanged() {
        for c in ['\u{FDD0}', '\u{FFFE}', '\u{FFFF}', '\u{10FFFF}'] {
            let s = alloc::string::String::from(c);
            let result = nfc(&s);
            assert!(matches!(result, Cow::Borrowed(_)), "NFC changed {c:?}");
            assert_eq!(result.chars().next(), Some(c));
        }
    }

    #[test]
    fn case_fold_unassigned_unchanged() {
        let result = case_fold("\u{0378}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{0378}");
    }

    #[test]
    fn case_fold_pua_unchanged() {
        let result = case_fold("\u{E000}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{E000}");
    }

    #[test]
    fn case_fold_noncharacter_unchanged() {
        for c in ['\u{FDD0}', '\u{FFFE}', '\u{FFFF}', '\u{10FFFF}'] {
            let s = alloc::string::String::from(c);
            let result = case_fold(&s);
            assert!(
                matches!(result, Cow::Borrowed(_)),
                "case_fold changed {c:?}"
            );
            assert_eq!(result.chars().next(), Some(c));
        }
    }

    #[test]
    fn is_whitespace_unassigned() {
        assert!(!is_whitespace('\u{0378}'));
    }

    #[test]
    fn is_whitespace_pua() {
        assert!(!is_whitespace('\u{E000}'));
    }

    #[test]
    fn is_whitespace_noncharacter() {
        assert!(!is_whitespace('\u{FDD0}'));
        assert!(!is_whitespace('\u{FFFE}'));
        assert!(!is_whitespace('\u{FFFF}'));
        assert!(!is_whitespace('\u{10FFFF}'));
    }

    #[test]
    fn is_starter_unassigned() {
        // Unassigned code points have CCC=0, so they are starters.
        assert!(is_starter('\u{0378}'));
    }

    #[test]
    fn is_starter_pua() {
        // PUA code points have CCC=0, so they are starters.
        assert!(is_starter('\u{E000}'));
    }

    #[test]
    fn is_starter_noncharacter() {
        // Noncharacters have CCC=0, so they are starters.
        assert!(is_starter('\u{FDD0}'));
        assert!(is_starter('\u{FFFE}'));
        assert!(is_starter('\u{FFFF}'));
        assert!(is_starter('\u{10FFFF}'));
    }

    // --- Supplementary plane characters (outside BMP, above U+FFFF) ---

    // Case folding: Deseret script has upper/lower pairs outside BMP.
    #[test]
    fn case_fold_deseret() {
        // U+10400 DESERET CAPITAL LETTER LONG I → U+10428 DESERET SMALL LETTER LONG I
        assert_eq!(case_fold("\u{10400}"), "\u{10428}");
    }

    // Case folding: Mathematical Alphanumeric Symbols are NOT folded by toCasefold()
    // (they have no CaseFolding.txt mapping). They remain unchanged.
    #[test]
    fn case_fold_mathematical_bold_unchanged() {
        let result = case_fold("\u{1D400}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{1D400}");
    }

    // NFD: CJK Compatibility Ideographs Supplement decompose to unified ideographs.
    #[test]
    fn nfd_cjk_compat_supplement() {
        // U+2F800 CJK COMPATIBILITY IDEOGRAPH-2F800 → U+4E3D
        assert_eq!(nfd("\u{2F800}"), "\u{4E3D}");
    }

    // NFC also decomposes CJK compatibility ideographs (they have canonical decompositions).
    #[test]
    fn nfc_cjk_compat_supplement() {
        assert_eq!(nfc("\u{2F800}"), "\u{4E3D}");
    }

    // NFD: Musical symbols with canonical decompositions.
    #[test]
    fn nfd_musical_symbol() {
        // U+1D15E MUSICAL SYMBOL HALF NOTE → U+1D157 VOID NOTEHEAD + U+1D165 COMBINING STEM
        assert_eq!(nfd("\u{1D15E}"), "\u{1D157}\u{1D165}");
    }

    // NFC does NOT recompose musical symbols: U+1D15E is a composition exclusion.
    #[test]
    fn nfc_musical_symbol_composition_exclusion() {
        // The decomposed form stays decomposed under NFC.
        assert_eq!(nfc("\u{1D157}\u{1D165}"), "\u{1D157}\u{1D165}");
    }

    // Non-starters outside BMP: musical combining marks.
    #[test]
    fn is_starter_musical_combining_stem() {
        // U+1D165 MUSICAL SYMBOL COMBINING STEM (CCC=216)
        assert!(!is_starter('\u{1D165}'));
    }

    #[test]
    fn is_starter_musical_combining_augmentation_dot() {
        // U+1D16D MUSICAL SYMBOL COMBINING AUGMENTATION DOT (CCC=226)
        assert!(!is_starter('\u{1D16D}'));
    }

    // Starters outside BMP: emoji, CJK Extension B, Deseret letters.
    #[test]
    fn is_starter_supplementary() {
        assert!(is_starter('\u{1F600}')); // GRINNING FACE
        assert!(is_starter('\u{20000}')); // CJK UNIFIED IDEOGRAPH-20000
        assert!(is_starter('\u{10400}')); // DESERET CAPITAL LETTER LONG I
    }

    // No whitespace exists outside BMP.
    #[test]
    fn is_whitespace_supplementary() {
        assert!(!is_whitespace('\u{1F600}')); // emoji
        assert!(!is_whitespace('\u{20000}')); // CJK Extension B
        assert!(!is_whitespace('\u{10400}')); // Deseret
    }

    // NFD/NFC: supplementary characters without decompositions are unchanged.
    #[test]
    fn nfd_emoji_unchanged() {
        let result = nfd("\u{1F600}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{1F600}");
    }

    #[test]
    fn nfc_emoji_unchanged() {
        let result = nfc("\u{1F600}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{1F600}");
    }

    // Case fold: supplementary characters without case mappings are unchanged.
    #[test]
    fn case_fold_emoji_unchanged() {
        let result = case_fold("\u{1F600}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{1F600}");
    }

    // --- Unicode 17.0.0 characters ---

    // U+20C1 SAUDI RIYAL SIGN (new currency symbol)
    #[test]
    fn nfd_saudi_riyal_unchanged() {
        let result = nfd("\u{20C1}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{20C1}");
    }

    #[test]
    fn nfc_saudi_riyal_unchanged() {
        let result = nfc("\u{20C1}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{20C1}");
    }

    #[test]
    fn case_fold_saudi_riyal_unchanged() {
        let result = case_fold("\u{20C1}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{20C1}");
    }

    #[test]
    fn is_whitespace_saudi_riyal() {
        assert!(!is_whitespace('\u{20C1}'));
    }

    #[test]
    fn is_starter_saudi_riyal() {
        assert!(is_starter('\u{20C1}'));
    }

    // U+2B96 EQUALS SIGN WITH INFINITY ABOVE (new math symbol)
    #[test]
    fn nfd_equals_infinity_unchanged() {
        let result = nfd("\u{2B96}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{2B96}");
    }

    #[test]
    fn case_fold_equals_infinity_unchanged() {
        let result = case_fold("\u{2B96}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{2B96}");
    }

    #[test]
    fn is_starter_equals_infinity() {
        assert!(is_starter('\u{2B96}'));
    }

    // Tolong Siki script (U+11DB0..U+11DEF)
    #[test]
    fn nfd_tolong_siki_unchanged() {
        let result = nfd("\u{11DB0}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{11DB0}");
    }

    #[test]
    fn is_starter_tolong_siki() {
        assert!(is_starter('\u{11DB0}'));
    }

    #[test]
    fn is_whitespace_tolong_siki() {
        assert!(!is_whitespace('\u{11DB0}'));
    }

    // Beria Erfe script (U+16EA0..U+16EDF)
    #[test]
    fn nfd_beria_erfe_unchanged() {
        let result = nfd("\u{16EA0}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{16EA0}");
    }

    #[test]
    fn is_starter_beria_erfe() {
        assert!(is_starter('\u{16EA0}'));
    }

    // CJK Unified Ideographs Extension J (U+323B0..U+3347F)
    #[test]
    fn nfd_cjk_extension_j_unchanged() {
        let result = nfd("\u{323B0}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{323B0}");
    }

    #[test]
    fn nfc_cjk_extension_j_unchanged() {
        let result = nfc("\u{323B0}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{323B0}");
    }

    #[test]
    fn case_fold_cjk_extension_j_unchanged() {
        let result = case_fold("\u{323B0}");
        assert!(matches!(result, Cow::Borrowed(_)));
        assert_eq!(result, "\u{323B0}");
    }

    #[test]
    fn is_starter_cjk_extension_j() {
        assert!(is_starter('\u{323B0}'));
    }
}