redox-path 0.4.1

Path parsing primitives for Redox
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
#![no_std]

extern crate alloc;

use alloc::{
    borrow::Cow,
    format,
    string::{String, ToString},
};
use core::{ffi::CStr, fmt};

/// The default scheme. Currently hardcoded.
const DEFAULT_SCHEME: &str = "file";

/// `PATH_MAX` constant used in this crate
pub const PATH_MAX: usize = 4096;

/// The name of a scheme
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RedoxScheme<'a>(Cow<'a, str>);

impl<'a> RedoxScheme<'a> {
    /// Create a new [`RedoxScheme`], ensuring there are no invalid characters
    pub fn new<S: Into<Cow<'a, str>>>(scheme: S) -> Option<Self> {
        let scheme = scheme.into();
        // Scheme cannot have NUL, `/`, `:` or non ascii characters
        // SIMD: schemes are short, so don't use memchr to avoid warmup penalty
        //       is_ascii() from std is SSE2 optimized only if str is long enough
        if !scheme.is_ascii()
            || scheme
                .as_bytes()
                .iter()
                .any(|b| matches!(b, b'\0' | b'/' | b':'))
        {
            return None;
        }
        Some(Self(scheme))
    }

    /// Similar to [`canonicalize_using_scheme`]
    pub fn canonicalize_as_scheme<'b>(&self, path: RedoxStr<'b>) -> RedoxPath<'b> {
        canonicalize_using_scheme_checked(self, path)
    }

    pub fn into_owned<'b>(self) -> RedoxScheme<'b> {
        RedoxScheme(self.0.into_owned().into())
    }
}

impl<'a> AsRef<str> for RedoxScheme<'a> {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl<'a> fmt::Display for RedoxScheme<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// The part of a path that is sent to each scheme
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RedoxReference<'a>(Cow<'a, str>);

impl<'a> RedoxReference<'a> {
    /// Create a new [`RedoxReference`], ensuring there are no invalid characters.
    /// An empty path is considered valid and equivalent to '/'.
    /// A path not starting with '/' will be considered not canonical.
    pub fn new<S: Into<Cow<'a, str>>>(reference: S) -> Option<Self> {
        let reference = reference.into();
        // Reference cannot have NUL
        // as_bytes() usage is safe: https://stackoverflow.com/a/6907327/3908409
        #[cfg(not(feature = "simd"))]
        if reference.as_bytes().iter().any(|&b| b == b'\0') {
            return None;
        }
        #[cfg(feature = "simd")]
        if memchr::memchr(b'\0', reference.as_bytes()).is_some() {
            return None;
        }
        Some(Self(reference))
    }

    /// Create a new [`RedoxReference`] from C string, ensuring it is a valid UTF-8
    pub fn new_from_c<S: Into<&'a CStr>>(reference: S) -> Option<Self> {
        let reference = reference.into();
        #[cfg(feature = "simd")]
        let s = simdutf8::basic::from_utf8(reference.to_bytes()).ok();
        #[cfg(not(feature = "simd"))]
        let s = reference.to_str().ok();
        Some(Self(Cow::Borrowed(s?)))
    }

    /// Create a [`RedoxReference`] from a buf, returns [`None`] if it not a valid UTF-8 or valid from NUL
    pub fn new_from_buf(path: &'a [u8], len: usize) -> Option<Self> {
        let buf = path.get(..len)?;
        #[cfg(not(feature = "simd"))]
        let s = str::from_utf8(buf).ok()?;
        #[cfg(feature = "simd")]
        let s = simdutf8::basic::from_utf8(buf).ok()?;
        Self::new(s)
    }

    /// SAFETY: Caller ensures that `path` contains no NUL character
    pub unsafe fn new_unchecked<S: Into<Cow<'a, str>>>(reference: S) -> Self {
        Self(reference.into())
    }

    /// Join a [`RedoxReference`] with a path. Relative paths will be joined, absolute paths will
    /// be returned as-is.
    ///
    /// Returns `Some` on success and `None` if the path is not valid
    pub fn join<'b, S: Into<Cow<'b, str>>>(&self, path: S) -> Option<RedoxReference<'b>> {
        let path = path.into();
        Some(self.join_checked(RedoxReference::new(path)?))
    }

    /// Join, but checked from NUL
    pub fn join_checked<'b>(&self, path: RedoxReference<'b>) -> RedoxReference<'b> {
        if !path.is_relative() {
            // Absolute path, referenced
            path
        } else if path.0.is_empty() {
            // Empty path, return prior reference cloned
            RedoxReference(self.0.to_string().into())
        } else {
            // Relative path, append to reference
            let mut reference = self.0.clone().into_owned();
            if !reference.is_empty() && !reference.ends_with('/') {
                reference.push('/');
            }
            reference.push_str(&path.0);
            RedoxReference(reference.into())
        }
    }

    /// Canonicalize [`RedoxReference`], removing `.`, `..` and empty subpaths.
    /// In this function, relative path will be remain relative and the path remain not canon.
    ///
    /// Paths that goes upward beyond the root path will be truncated
    /// i.e. `/..` converted to `/` and `../foo/` converted to `foo`.
    pub fn canonical(self) -> Self {
        let mut canonicalized = true;
        let mut is_absolute = false;
        let mut parts = [""; PATH_MAX];
        let mut parts_len = 0;
        for (i, part) in self.0.split('/').enumerate() {
            if part.is_empty() {
                if i == 0 {
                    is_absolute = true;
                } else {
                    canonicalized = false;
                }
            } else if part == "." {
                canonicalized = false;
            } else if part == ".." {
                if parts_len > 0 {
                    parts_len -= 1;
                }
                canonicalized = false;
            } else {
                if parts_len >= PATH_MAX {
                    break;
                }
                parts[parts_len] = part;
                parts_len += 1;
            }
        }

        if canonicalized {
            return self;
        }
        let mut string = String::with_capacity(self.0.len());
        if is_absolute {
            string.push('/');
        }
        for (i, part) in parts[..parts_len].iter().enumerate() {
            if i > 0 {
                string.push('/');
            }
            string.push_str(part);
        }

        // Does not use ::new() as &self is checked from NUL
        Self(Cow::Owned(string))
    }

    /// Normalize [`RedoxReference`], removing `.`, `..` (if possible) and empty subpaths.
    /// This function will return [`None`] if the path is absolute or escapes root.
    ///
    /// This function is useful to validate if the path still remain in scope within root.
    /// To make this works within symlink, `max_upward` is provided, and also returning an integer
    /// for next `canonical_max_upward(max_upward)`. The amount of `..` will never exceed `max_upward`.
    pub fn canonical_max_upward(self, max_upward: usize) -> Option<(Self, usize)> {
        let mut canonicalized = true;
        let mut parts = [""; PATH_MAX];
        let mut parts_len = 0;
        let mut upward_depth = 0;
        for (i, part) in self.0.split('/').enumerate() {
            if part.is_empty() {
                if i == 0 {
                    // absolute path
                    return None;
                }
                canonicalized = false;
            } else if part == "." {
                canonicalized = false;
            } else if part == ".." {
                if parts_len == 0 {
                    upward_depth += 1;
                    if upward_depth > max_upward {
                        // escapes root
                        return None;
                    }
                } else {
                    parts_len -= 1;
                }

                canonicalized = false;
            } else {
                if parts_len + upward_depth >= PATH_MAX {
                    break;
                }
                parts[parts_len] = part;
                parts_len += 1;
            }
        }

        if canonicalized {
            return Some((self, max_upward + parts_len));
        }
        let mut string = String::with_capacity(self.0.len());
        for _ in 0..upward_depth {
            if !string.is_empty() {
                string.push('/');
            }
            string.push_str("..");
        }
        for part in parts[..parts_len].iter() {
            if !string.is_empty() {
                string.push('/');
            }
            string.push_str(part);
        }

        // Does not use ::new() as &self is checked from NUL
        Some((Self(string.into()), max_upward + parts_len - upward_depth))
    }

    /// Convert [`RedoxReference`] into relative path if it's not already.
    ///
    /// Converting the path to relative allows using [`Self::canonical_max_upward`]
    pub fn to_relative(self) -> Self {
        if self.is_relative() {
            return self;
        }
        match self.0 {
            Cow::Borrowed(s) => Self(s[1..].into()),
            Cow::Owned(s) => Self(s[1..].to_string().into()),
        }
    }

    /// Verify that the reference is canonicalized
    pub fn is_canon(&self) -> bool {
        (self.0.is_empty() || self.0 == "/")
            || (!self.is_relative()
                && self
                    .0
                    .split('/')
                    .skip(1)
                    .all(|seg| seg != ".." && seg != "." && seg != ""))
    }

    /// Get path upward one time
    pub fn dirname<'b>(&self) -> RedoxReference<'b> {
        self.dirname_split().0.into_owned()
    }

    /// Get path upward one time and the file name
    pub fn dirname_split(&'a self) -> (RedoxReference<'a>, Option<RedoxReference<'a>>) {
        let mut slice = &*self.0;
        let mut was_upward = false;
        loop {
            // SIMD: not eligible, filenames are often short
            let Some((dir, file)) = slice.rsplit_once('/') else {
                let file = if matches!(slice, "." | "..") {
                    None
                } else {
                    Some(RedoxReference(slice.into()))
                };
                return (RedoxReference("".into()), file);
            };
            slice = dir;
            was_upward = match file {
                "" | "." => false,
                ".." => true,
                _ if was_upward => false,
                file => {
                    return (
                        RedoxReference(slice.into()),
                        Some(RedoxReference(file.into())),
                    );
                }
            };
            if slice.is_empty() {
                return (RedoxReference("/".into()), None);
            }
        }
    }

    /// Is this path relative?
    pub fn is_relative(&self) -> bool {
        !self.0.starts_with('/')
    }

    /// Copy the content into owned
    pub fn into_owned<'b>(self) -> RedoxReference<'b> {
        RedoxReference(self.0.into_owned().into())
    }
}

impl<'a> AsRef<str> for RedoxReference<'a> {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl<'a> fmt::Display for RedoxReference<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<'a> From<RedoxReference<'a>> for Cow<'a, str> {
    fn from(value: RedoxReference<'a>) -> Self {
        value.0
    }
}

/// A fully qualified Redox path
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum RedoxPath<'a> {
    /// Standard UNIX compatible format
    Standard(RedoxReference<'a>),
    /// Legacy URI format
    Legacy(RedoxScheme<'a>, RedoxReference<'a>),
}

impl<'a> RedoxPath<'a> {
    /// Create [`RedoxPath`] from absolute path
    ///
    /// Returns `Some` on success and `None` if the path contains NUL
    pub fn from_absolute<S: Into<Cow<'a, str>>>(path: S) -> Option<Self> {
        RedoxStr::new(path).and_then(|s| s.abs())
    }

    /// Create a new [`RedoxPath`] from C string, ensuring it is a valid UTF-8
    pub fn from_absolute_c<S: Into<&'a CStr>>(path: S) -> Option<Self> {
        RedoxStr::new_from_c(path).and_then(|s| s.abs())
    }

    /// Create a new [`RedoxPath`] from a buffer, ensuring it is a valid UTF-8 and valid from NUL
    pub fn from_absolute_buf(path: &'a [u8], len: usize) -> Option<Self> {
        RedoxStr::new_from_buf(path, len).and_then(|s| s.abs())
    }

    /// Create a new [`RedoxPath`] from [`RedoxReference`] if the reference is absolute
    pub fn from_reference(path: RedoxReference<'a>) -> Option<Self> {
        RedoxStr::from(path).abs()
    }

    /// Join a [`RedoxPath`] with a &str path. Relative paths will be appended to self,
    /// absolute paths will be returned as a RedoxPath, ignoring self.
    ///
    /// Returns `Some` on success and `None` if the path is not valid
    pub fn join<'b, S: Into<Cow<'b, str>>>(&self, path: S) -> Option<RedoxPath<'b>> {
        let path = path.into();
        Some(self.join_checked(RedoxReference::new(path)?))
    }

    /// Join, but checked from NUL
    pub fn join_checked<'b>(&self, path: RedoxReference<'b>) -> RedoxPath<'b> {
        if !path.is_relative() {
            // Absolute path, replaces reference
            RedoxPath::Standard(path)
        } else {
            match self {
                Self::Standard(reference) => RedoxPath::Standard(reference.join_checked(path)),
                Self::Legacy(scheme, reference) => RedoxPath::Legacy(
                    RedoxScheme(scheme.0.to_string().into()),
                    reference.join_checked(path),
                ),
            }
        }
    }

    /// Canonicalize path, see [`RedoxReference::canonical`]
    pub fn canonical(self) -> Self {
        match self {
            Self::Standard(reference) => Self::Standard(reference.canonical()),
            Self::Legacy(scheme, reference) => {
                // We cannot canonicalize legacy paths since they may need to preserve dots and
                // slashes
                Self::Legacy(scheme, reference)
            }
        }
    }

    /// Verify that the path is canonicalized.
    ///
    /// Returns false if any segment is ".", ".." or "".
    /// A path that is empty is allowed and is interpreted as "/".
    /// Legacy paths are assumed to be canonical.
    pub fn is_canon(&self) -> bool {
        match self {
            Self::Standard(reference) => reference.is_canon(),
            Self::Legacy(_scheme, _reference) => true,
        }
    }

    /// Convert into a [`RedoxScheme`] and [`RedoxReference`].
    /// - Standard paths will parse `/scheme/scheme_name/reference`, and anything not starting
    ///   with `/scheme` will be parsed as being part of the `file` scheme
    /// - Legacy paths can be instantly converted
    pub fn as_parts(&'a self) -> Option<(RedoxScheme<'a>, RedoxReference<'a>)> {
        let (scheme, path) = self.get_scheme_prefixed(true)?;
        Some((scheme, RedoxReference(path.into())))
    }

    /// Get a [`RedoxScheme`] if the reference contained `/scheme/scheme_name`.
    /// Does not fallback to default scheme if absent.
    pub fn get_scheme(&'a self) -> Option<RedoxScheme<'a>> {
        Some(self.get_scheme_prefixed(false)?.0)
    }

    fn get_scheme_prefixed(&'a self, fallback: bool) -> Option<(RedoxScheme<'a>, &'a str)> {
        const SCHEME_PREFIX_LENGTH: usize = "/scheme/".len();
        match self {
            Self::Standard(reference) => {
                // SIMD: not eligible, splitted into multiple parts and often short
                let mut parts = reference.0.split('/');
                if parts.next() == Some("") && parts.next() == Some("scheme") {
                    match parts.next() {
                        Some(scheme_name) => {
                            // Path is in /scheme/scheme_name
                            let scheme_length = SCHEME_PREFIX_LENGTH + scheme_name.len() + 1;
                            let remainder = reference.0.get(scheme_length..).unwrap_or("");

                            return Some((RedoxScheme::new(scheme_name)?, remainder));
                        }
                        None => {
                            // Path is the root scheme
                            return Some((RedoxScheme(Cow::from("")), ""));
                        }
                    }
                }
                if !fallback {
                    return None;
                }
                // If path has no special processing, it is inside the file scheme
                let remainder = reference.0.get(1..).unwrap_or("");
                return Some((RedoxScheme(Cow::from(DEFAULT_SCHEME)), remainder));
            }
            Self::Legacy(scheme, reference) => {
                // Legacy paths are already split
                Some((scheme.clone(), reference.as_ref()))
            }
        }
    }

    /// Is the scheme for this path the same as the given string?
    pub fn matches_scheme(&self, other: &str) -> bool {
        if let Some((scheme, _)) = self.get_scheme_prefixed(true) {
            scheme.0 == other
        } else {
            false
        }
    }

    /// Does the scheme match the given category, e.g. "disk-"
    pub fn is_scheme_category(&self, category: &str) -> bool {
        if let Some((scheme, _)) = self.get_scheme_prefixed(true) {
            let mut parts = scheme.0.splitn(2, '.');
            if let Some(cat) = parts.next() {
                cat == category && parts.next().is_some()
            } else {
                false
            }
        } else {
            false
        }
    }

    /// Is this the default scheme, "/scheme/file"?
    pub fn is_default_scheme(&self) -> bool {
        self.matches_scheme(DEFAULT_SCHEME)
    }

    /// Is this a Legacy format path?
    pub fn is_legacy(&self) -> bool {
        matches!(self, RedoxPath::Legacy(_, _))
    }

    /// Format a [`RedoxPath`] into a UNIX style path
    pub fn to_standard(self) -> RedoxReference<'a> {
        match self {
            RedoxPath::Standard(reference) => reference,
            RedoxPath::Legacy(scheme, reference) => {
                RedoxReference(format!("/scheme/{}/{}", scheme.0, reference.0).into())
            }
        }
    }

    /// Format a [`RedoxPath`] into a UNIX style path,
    /// ensuring it is canonicalized
    pub fn to_standard_canon(self) -> RedoxPath<'a> {
        match self {
            RedoxPath::Standard(reference) => RedoxPath::Standard(reference.canonical()),
            RedoxPath::Legacy(scheme, reference) => {
                canonicalize_using_scheme_checked(&scheme, RedoxStr::Relative(reference))
            }
        }
    }

    /// Get path upward one time
    pub fn dirname<'b>(&self) -> RedoxPath<'b> {
        match self {
            RedoxPath::Standard(redox_reference) => RedoxPath::Standard(redox_reference.dirname()),
            RedoxPath::Legacy(redox_scheme, redox_reference) => RedoxPath::Legacy(
                RedoxScheme(redox_scheme.to_string().into()),
                redox_reference.dirname(),
            ),
        }
    }

    /// Get path upward one time and the file name
    pub fn dirname_split(&'a self) -> (RedoxPath<'a>, Option<RedoxReference<'a>>) {
        match self {
            RedoxPath::Standard(redox_reference) => {
                let (dir, name) = redox_reference.dirname_split();
                (RedoxPath::Standard(dir), name)
            }
            RedoxPath::Legacy(redox_scheme, redox_reference) => {
                let (dir, name) = redox_reference.dirname_split();
                (RedoxPath::Legacy(redox_scheme.clone(), dir), name)
            }
        }
    }

    /// Convert into [`RedoxReference`], if it an old scheme, the scheme part will be discarded.
    pub fn to_reference(self) -> RedoxReference<'a> {
        match self {
            RedoxPath::Standard(redox_reference) => redox_reference,
            RedoxPath::Legacy(_, redox_reference) => redox_reference,
        }
    }

    /// Get [`RedoxReference`], if it an old scheme, the scheme part will be discarded.
    pub fn as_reference(&self) -> &RedoxReference<'a> {
        match self {
            RedoxPath::Standard(redox_reference) => redox_reference,
            RedoxPath::Legacy(_, redox_reference) => redox_reference,
        }
    }

    /// Similar to [`canonicalize_using_cwd`]
    pub fn canonicalize_as_cwd<'b>(&self, path: RedoxStr<'b>) -> RedoxPath<'b> {
        // SAFETY: canonicalize_using_cwd_checked will None only if cwd_opt is None
        canonicalize_using_cwd_checked(Some(self), path, false).unwrap()
    }

    /// Similar to [`canonicalize_to_standard`]
    pub fn canonicalize_as_cwd_standard<'b>(&self, path: RedoxStr<'b>) -> RedoxPath<'b> {
        // SAFETY: canonicalize_to_standard_checked will None only if cwd_opt is None
        canonicalize_to_standard_checked(Some(self), path).unwrap()
    }

    /// Similar to [`canonicalize_using_cwd_with_max_upward`]
    pub fn canonicalize_as_cwd_with_max_upward<'b>(
        &self,
        relpath: RedoxReference<'b>,
        max_upward: usize,
    ) -> Option<(RedoxPath<'b>, usize)> {
        let (relpath, max_upward) = relpath.canonical_max_upward(max_upward)?;
        let newpath = self.join_checked(relpath);
        Some((newpath, max_upward))
    }

    /// Copy the content into owned
    pub fn into_owned<'b>(self) -> RedoxPath<'b> {
        match self {
            RedoxPath::Standard(redox_reference) => {
                RedoxPath::Standard(redox_reference.into_owned())
            }
            RedoxPath::Legacy(redox_scheme, redox_reference) => {
                RedoxPath::Legacy(redox_scheme.into_owned(), redox_reference.into_owned())
            }
        }
    }
}

impl<'a> fmt::Display for RedoxPath<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RedoxPath::Standard(reference) => {
                write!(f, "{}", reference.0)
            }
            RedoxPath::Legacy(scheme, reference) => {
                write!(f, "{}:{}", scheme.0, reference.0)
            }
        }
    }
}

impl<'a> From<RedoxPath<'a>> for Cow<'a, str> {
    fn from(value: RedoxPath<'a>) -> Self {
        match value {
            RedoxPath::Standard(redox_reference) => redox_reference.0,
            a => Cow::Owned(a.to_string()),
        }
    }
}

/// A valid path (checked against NUL) that can be fully qualified [`RedoxPath`] or relative [`RedoxReference`]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum RedoxStr<'a> {
    Absolute(RedoxPath<'a>),
    Relative(RedoxReference<'a>),
}

impl<'a> RedoxStr<'a> {
    /// Create a [`RedoxStr`] from a string, returns [`None`] if it contains NUL
    pub fn new<S: Into<Cow<'a, str>>>(path: S) -> Option<Self> {
        let path = path.into();
        let r = if path.starts_with('/') {
            // New /path or /scheme/scheme_name/path format, referenced
            RedoxPath::Standard(RedoxReference::new(path)?)
        } else {
            if let Some((scheme, reference)) = Self::split_scheme(&path) {
                // Old scheme_name:path format, referenced
                RedoxPath::Legacy(scheme, RedoxReference::new(reference)?)
            } else {
                // Neither an old scheme, so it's relative, referenced
                return Some(RedoxStr::Relative(RedoxReference::new(path)?));
            }
        };
        Some(RedoxStr::Absolute(r))
    }

    /// Create a [`RedoxStr`] from a buf, returns [`None`] if it not a valid UTF-8 or valid from NUL
    pub fn new_from_c<S: Into<&'a CStr>>(path: S) -> Option<Self> {
        let path = path.into();
        if matches!(path.to_bytes_with_nul().first(), Some(b'/')) {
            // New /path or /scheme/scheme_name/path format
            Some(RedoxStr::Absolute(RedoxPath::Standard(
                RedoxReference::new_from_c(path)?,
            )))
        } else {
            // Old scheme_name:path or relative format
            #[cfg(feature = "simd")]
            let s = simdutf8::basic::from_utf8(path.to_bytes()).ok()?;
            #[cfg(not(feature = "simd"))]
            let s = path.to_str().ok()?;
            // SAFETY: CStr::to_bytes() stop at NUL
            unsafe { Some(RedoxStr::new_unchecked(s)) }
        }
    }

    /// Create a [`RedoxStr`] from a buf, returns [`None`] if it not a valid UTF-8 or contains NUL
    pub fn new_from_buf(path: &'a [u8], len: usize) -> Option<RedoxStr<'a>> {
        let buf = path.get(..len)?;
        #[cfg(not(feature = "simd"))]
        let s = str::from_utf8(buf).ok()?;
        #[cfg(feature = "simd")]
        let s = simdutf8::basic::from_utf8(buf).ok()?;
        Self::new(s)
    }

    /// SAFETY: Caller ensures that `path` contains no NUL character
    pub unsafe fn new_unchecked<S: Into<Cow<'a, str>>>(path: S) -> Self {
        let path = path.into();
        let r = if path.starts_with('/') {
            // New /path or /scheme/scheme_name/path format, referenced
            RedoxPath::Standard(RedoxReference(path))
        } else {
            if let Some((scheme, reference)) = Self::split_scheme(&path) {
                // Old scheme_name:path format, referenced
                RedoxPath::Legacy(scheme, RedoxReference(reference))
            } else {
                // Neither an old scheme, so it's relative, referenced
                return RedoxStr::Relative(RedoxReference(path));
            }
        };
        RedoxStr::Absolute(r)
    }

    /// Split with scheme algorithm, retain reference if possible
    fn split_scheme(path: &Cow<'a, str>) -> Option<(RedoxScheme<'a>, Cow<'a, str>)> {
        #[cfg(not(feature = "simd"))]
        let i = {
            let mut i = 0;
            for &byte in path.as_bytes().iter() {
                if byte == b'/' {
                    return None;
                }
                if byte == b':' {
                    break;
                }
                i += 1;
            }
            i
        };
        #[cfg(feature = "simd")]
        let i = {
            let path_b = path.as_bytes();
            let chr = memchr::memchr2(b'/', b':', path_b);
            match chr {
                Some(c) if path_b.get(c).filter(|&&b| b == b':').is_some() => c,
                _ => return None,
            }
        };
        let (scheme, path): (Cow<'a, str>, Cow<'a, str>) = match path {
            Cow::Borrowed(s) => (s.get(..i)?.into(), s.get(i + 1..)?.into()),
            Cow::Owned(s) => (
                s.get(..i)?.to_string().into(),
                s.get(i + 1..)?.to_string().into(),
            ),
        };
        Some((RedoxScheme::new(scheme)?, path))
    }

    /// Get the reference to absolute path
    pub fn as_abs(&'a self) -> Option<&'a RedoxPath<'a>> {
        match self {
            RedoxStr::Absolute(redox_path) => Some(redox_path),
            _ => None,
        }
    }
    /// Get the reference to relative path
    pub fn as_rel(&'a self) -> Option<&'a RedoxReference<'a>> {
        match self {
            RedoxStr::Relative(redox_reference) => Some(redox_reference),
            _ => None,
        }
    }
    /// Get the absolute path
    pub fn abs(self) -> Option<RedoxPath<'a>> {
        match self {
            RedoxStr::Absolute(redox_path) => Some(redox_path),
            _ => None,
        }
    }
    /// Get the relative path
    pub fn rel(self) -> Option<RedoxReference<'a>> {
        match self {
            RedoxStr::Relative(redox_reference) => Some(redox_reference),
            _ => None,
        }
    }
    /// Is this path canonicalized?
    pub fn is_canon(&self) -> bool {
        match self {
            RedoxStr::Absolute(redox_path) => redox_path.is_canon(),
            RedoxStr::Relative(redox_reference) => redox_reference.is_canon(),
        }
    }
    /// Convert into canonicalized path
    pub fn canonical(self) -> Self {
        match self {
            RedoxStr::Absolute(redox_path) => RedoxStr::from(redox_path.canonical()),
            RedoxStr::Relative(redox_reference) => RedoxStr::from(redox_reference.canonical()),
        }
    }
    /// Convert into standard UNIX style path
    pub fn to_standard(self) -> RedoxReference<'a> {
        match self {
            RedoxStr::Absolute(redox_path) => redox_path.to_standard(),
            RedoxStr::Relative(redox_reference) => redox_reference,
        }
    }
    /// Get path upward one time
    pub fn dirname<'b>(&self) -> RedoxStr<'b> {
        match self {
            RedoxStr::Absolute(redox_path) => RedoxStr::from(redox_path.dirname()),
            RedoxStr::Relative(redox_reference) => RedoxStr::from(redox_reference.dirname()),
        }
    }
    /// Get path upward one time and the file name
    pub fn dirname_split(&'a self) -> (RedoxStr<'a>, Option<RedoxReference<'a>>) {
        match self {
            RedoxStr::Absolute(redox_path) => {
                let (dir, name) = redox_path.dirname_split();
                (RedoxStr::from(dir), name)
            }
            RedoxStr::Relative(redox_reference) => {
                let (dir, name) = redox_reference.dirname_split();
                (RedoxStr::from(dir), name)
            }
        }
    }
    /// This is path empty?
    pub fn is_empty(&self) -> bool {
        match self {
            RedoxStr::Absolute(_) => false, // Absolute path always have `/` for POSIX path or `:` for legacy path
            RedoxStr::Relative(redox_reference) => redox_reference.as_ref().is_empty(),
        }
    }
    /// Join two path
    pub fn join<'b, S: Into<Cow<'b, str>>>(&self, path: S) -> Option<RedoxStr<'b>> {
        match self {
            RedoxStr::Absolute(redox_path) => Some(RedoxStr::from(redox_path.join(path)?)),
            RedoxStr::Relative(redox_reference) => {
                Some(RedoxStr::from(redox_reference.join(path)?))
            }
        }
    }
    /// Join two path
    pub fn join_checked<'b>(&self, path: RedoxReference<'b>) -> RedoxStr<'b> {
        match self {
            RedoxStr::Absolute(redox_path) => RedoxStr::from(redox_path.join_checked(path)),
            RedoxStr::Relative(redox_reference) => {
                RedoxStr::from(redox_reference.join_checked(path))
            }
        }
    }
    /// Is this path relative?
    pub fn is_relative(&self) -> bool {
        // Does no need to check RedoxReference::is_relative because it's checked from From or new function
        matches!(self, RedoxStr::Relative(_))
    }

    /// Copy the content into owned
    pub fn into_owned<'b>(self) -> RedoxStr<'b> {
        match self {
            RedoxStr::Absolute(redox_path) => RedoxStr::Absolute(redox_path.into_owned()),
            RedoxStr::Relative(redox_reference) => RedoxStr::Relative(redox_reference.into_owned()),
        }
    }
}

impl<'a> From<RedoxPath<'a>> for RedoxStr<'a> {
    fn from(value: RedoxPath<'a>) -> Self {
        Self::Absolute(value)
    }
}

impl<'a> From<RedoxReference<'a>> for RedoxStr<'a> {
    fn from(value: RedoxReference<'a>) -> Self {
        // try to check if it absolute
        if !value.is_relative() {
            RedoxStr::Absolute(RedoxPath::Standard(value))
        } else {
            RedoxStr::Relative(value)
        }
    }
}
impl<'a> From<RedoxStr<'a>> for Cow<'a, str> {
    fn from(value: RedoxStr<'a>) -> Self {
        match value {
            RedoxStr::Absolute(redox_path) => redox_path.into(),
            RedoxStr::Relative(redox_reference) => redox_reference.into(),
        }
    }
}

/// Make a relative path absolute using an optional current working directory.
///
/// Given a cwd of "/scheme/scheme_name/dir_name", this function will turn
/// path "foo" into /scheme/scheme_name/dir_name/foo".
/// "/foo" will be left as is, because it is already absolute.
/// "." and empty segments "//" will be removed.
/// ".." will be resolved by backing up one directory, except at the root,
/// where ".." will be ignored and removed.
///
/// For old format schemes,
/// given a cwd of "scheme:/path", this function will turn "foo" into "scheme:/path/foo".
/// "/foo" will turn into "file:/foo". "bar:/foo" will be used directly, as it is already
/// absolute.
pub fn canonicalize_using_cwd<'a>(cwd_opt: Option<&'a str>, path: &'a str) -> Option<Cow<'a, str>> {
    let cwd_opt = cwd_opt.and_then(|s| RedoxPath::from_absolute(Cow::Borrowed(s)));
    let absolute = canonicalize_using_cwd_checked(cwd_opt.as_ref(), RedoxStr::new(path)?, false)?;
    Some(absolute.into())
}

/// `canonicalize_using_cwd` but for internal use.
/// `in_scheme_root`: Treat cwd_opt as the root of scheme, similar to
///  openat RESOLVE_IN_ROOT but still allowing access to other scheme root.
fn canonicalize_using_cwd_checked<'a, 'b>(
    cwd_opt: Option<&RedoxPath<'a>>,
    path: RedoxStr<'b>,
    in_scheme_root: bool,
) -> Option<RedoxPath<'b>> {
    let absolute = match (path, in_scheme_root) {
        (RedoxStr::Absolute(absolute), true) if absolute.get_scheme().is_some() => absolute,
        (RedoxStr::Absolute(absolute), true) => {
            cwd_opt?.join_checked(absolute.to_standard().to_relative())
        }
        (RedoxStr::Absolute(absolute), false) => absolute,
        (RedoxStr::Relative(relative), true) => cwd_opt?.join_checked(relative.canonical()),
        (RedoxStr::Relative(relative), false) => cwd_opt?.join_checked(relative),
    };
    Some(absolute.canonical())
}

/// Canonicalize as in [`canonicalize_using_cwd`], but Legacy format paths
/// are canonicalized into UNIX format.
pub fn canonicalize_to_standard<'a>(
    cwd_opt: Option<&'a str>,
    path: &'a str,
) -> Option<Cow<'a, str>> {
    let absolute = match RedoxPath::from_absolute(path) {
        Some(absolute) => absolute,
        None => {
            let absolute = RedoxPath::from_absolute(cwd_opt?)?;
            absolute.join(path)?
        }
    };
    Some(absolute.to_standard_canon().into())
}

/// [`canonicalize_to_standard`] but for internal use.
fn canonicalize_to_standard_checked<'a, 'b>(
    cwd_opt: Option<&RedoxPath<'a>>,
    path: RedoxStr<'b>,
) -> Option<RedoxPath<'b>> {
    let path = match path {
        RedoxStr::Absolute(absolute) => absolute,
        RedoxStr::Relative(relative) => cwd_opt?.join_checked(relative),
    }
    .to_standard_canon();
    Some(path)
}

/// Make a path that is relative to the root of a scheme into a full path,
/// following the rules of [`canonicalize_using_cwd`].
///
/// Returns the Some if a valid path can be constructed,
/// None if the scheme name is not valid or some other error occurs.
/// The returned value is guaranteed to be UNIX, but not guaranteed to be equal with given scheme.
pub fn canonicalize_using_scheme<'a>(scheme: &'a str, path: &'a str) -> Option<Cow<'a, str>> {
    let path = RedoxStr::new(path)?;
    Some(canonicalize_using_scheme_checked(&RedoxScheme::new(scheme)?, path).into())
}

/// [`canonicalize_using_scheme`] but for internal use.
fn canonicalize_using_scheme_checked<'a, 'b>(
    scheme: &RedoxScheme<'_>,
    path: RedoxStr<'b>,
) -> RedoxPath<'b> {
    let path = match path {
        RedoxStr::Absolute(s) => {
            if !s.is_legacy() && s.matches_scheme(&scheme.0) {
                // Canonicalized and being in the same scheme, return early
                // to avoid allocation from scheme_path_checked
                return s;
            }
            RedoxStr::Absolute(s)
        }
        path => path,
    };
    // SAFETY: canonicalize_using_cwd_checked will only None if cwd_opt is None
    canonicalize_using_cwd_checked(Some(&scheme_path_checked(&scheme)), path, true).unwrap()
}

/// Make a path that is relative to the root of a scheme into a full path,
/// following the rules of [`canonicalize_using_cwd`], but with upward_max to ensure
/// the returned path stays within the true root.
pub fn canonicalize_using_cwd_with_max_upward<'a>(
    cwd: &str,
    relpath: &'a str,
    max_upward: usize,
) -> Option<(Cow<'a, str>, usize)> {
    // Using RedoxStr so cwd using relative path (as in scheme) is also accepted
    let cwd = RedoxStr::new(Cow::Borrowed(cwd))?;
    let (absolute, max_upward) = canonicalize_using_cwd_with_max_upward_checked(
        &cwd,
        RedoxStr::new(relpath)?.rel()?,
        max_upward,
    )?;
    Some((absolute.into(), max_upward))
}

/// [`canonicalize_using_cwd_with_max_upward`] but for internal use.
fn canonicalize_using_cwd_with_max_upward_checked<'a, 'b>(
    cwd: &RedoxStr<'a>,
    relpath: RedoxReference<'b>,
    max_upward: usize,
) -> Option<(RedoxStr<'b>, usize)> {
    let (relpath, max_upward) = relpath.canonical_max_upward(max_upward)?;
    let relnew = cwd.join_checked(relpath);
    Some((relnew, max_upward))
}

/// The standard path for a given scheme name
///
/// Returns Some if the scheme name is valid, None otherwise
pub fn scheme_path(name: &str) -> Option<RedoxPath<'_>> {
    let scheme = RedoxScheme::new(name)?;
    Some(scheme_path_checked(&scheme))
}

/// [`scheme_path`] but for internal use.
fn scheme_path_checked<'a, 'b>(scheme: &'a RedoxScheme<'a>) -> RedoxPath<'b> {
    if scheme.0.is_empty() {
        return RedoxPath::Standard(RedoxReference("/scheme".into()));
    }
    RedoxPath::Standard(RedoxReference(format!("/scheme/{}", scheme.0).into()))
}

/// Make a scheme name (not a full path) for a device of a given category
///
/// Returns Some if the resulting scheme name is valid, None otherwise
pub fn make_scheme_name<'a, 'b>(category: &'a str, detail: &'a str) -> Option<RedoxScheme<'b>> {
    let name = format!("{}.{}", category, detail);
    RedoxScheme::new(name)
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::format;

    // Tests absolute paths without scheme
    #[test]
    fn test_absolute() {
        let cwd_opt = None;
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/"),
            Some(Cow::Borrowed("/"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/file"),
            Some(Cow::Borrowed("/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/folder/file"),
            Some(Cow::Borrowed("/folder/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/folder/file/"),
            Some(Cow::Borrowed("/folder/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/folder/file//"),
            Some(Cow::Borrowed("/folder/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/folder/../file"),
            Some(Cow::Borrowed("/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/folder/../.."),
            Some(Cow::Borrowed("/"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/folder/../../../.."),
            Some(Cow::Borrowed("/"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/.."),
            Some(Cow::Borrowed("/"))
        );
    }

    // Test relative paths using new scheme
    #[test]
    fn test_new_relative() {
        let cwd_opt = Some("/scheme/foo");
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "file"),
            Some(Cow::Borrowed("/scheme/foo/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/file"),
            Some(Cow::Borrowed("/scheme/foo/folder/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/../file"),
            Some(Cow::Borrowed("/scheme/foo/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/../.."),
            Some(Cow::Borrowed("/scheme"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/../../../.."),
            Some(Cow::Borrowed("/"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, ".."),
            Some(Cow::Borrowed("/scheme"))
        );
    }

    // Tests paths prefixed with /scheme/
    #[test]
    fn test_new_scheme() {
        let cwd_opt = None;
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/scheme/bar/"),
            Some(Cow::Borrowed("/scheme/bar"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/scheme/bar/file"),
            Some(Cow::Borrowed("/scheme/bar/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/scheme/bar/folder/file"),
            Some(Cow::Borrowed("/scheme/bar/folder/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/scheme/bar/folder/../file"),
            Some(Cow::Borrowed("/scheme/bar/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/scheme/bar/folder/../.."),
            Some(Cow::Borrowed("/scheme"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/scheme/bar/folder/../../../.."),
            Some(Cow::Borrowed("/"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "/scheme/bar/.."),
            Some(Cow::Borrowed("/scheme"))
        );

        assert_eq!(
            canonicalize_using_scheme("bar", ""),
            Some(Cow::Borrowed("/scheme/bar"))
        );
        assert_eq!(
            canonicalize_using_scheme("bar", "foo"),
            Some(Cow::Borrowed("/scheme/bar/foo"))
        );
        assert_eq!(
            canonicalize_using_scheme("foo", "/bar"),
            Some(Cow::Borrowed("/scheme/foo/bar"))
        );
        assert_eq!(
            canonicalize_using_scheme("bar", ".."),
            Some(Cow::Borrowed("/scheme/bar"))
        );
        assert_eq!(
            canonicalize_using_scheme("file", "/scheme/foo/bar"),
            Some(Cow::Borrowed("/scheme/foo/bar"))
        );
    }

    // Test relative paths using old scheme
    #[test]
    fn test_old_relative() {
        let cwd_opt = Some("foo:");
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "file"),
            Some(Cow::Borrowed("foo:file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/file"),
            Some(Cow::Borrowed("foo:folder/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/../file"),
            Some(Cow::Borrowed("foo:folder/../file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/../.."),
            Some(Cow::Borrowed("foo:folder/../.."))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "folder/../../../.."),
            Some(Cow::Borrowed("foo:folder/../../../.."))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, ".."),
            Some(Cow::Borrowed("foo:.."))
        );
    }

    // Tests paths prefixed with scheme_name:
    #[test]
    fn test_old_scheme() {
        let cwd_opt = None;
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "bar:"),
            Some(Cow::Borrowed("bar:"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "bar:file"),
            Some(Cow::Borrowed("bar:file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "bar:folder/file"),
            Some(Cow::Borrowed("bar:folder/file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "bar:folder/../file"),
            Some(Cow::Borrowed("bar:folder/../file"))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "bar:folder/../.."),
            Some(Cow::Borrowed("bar:folder/../.."))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "bar:folder/../../../.."),
            Some(Cow::Borrowed("bar:folder/../../../.."))
        );
        assert_eq!(
            canonicalize_using_cwd(cwd_opt, "bar:.."),
            Some(Cow::Borrowed("bar:.."))
        );
    }

    // Tests max upward for symlink
    #[test]
    fn test_max_upward() {
        // new format
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("/", "foo/bar", 0),
            Some((Cow::Borrowed("/foo/bar"), 2))
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("/foo/", "bar/../baz/qux", 0),
            Some((Cow::Borrowed("/foo/baz/qux"), 2))
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("foo/../bar", "baz/./../qux", 0),
            Some((Cow::Borrowed("foo/../bar/qux"), 1))
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("/foo", "../bar", 1),
            Some((Cow::Borrowed("/foo/../bar"), 1))
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("foo/..", "../bar/..", 2),
            Some((Cow::Borrowed("foo/../.."), 1))
        );
        // old format
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("foo:", "bar/baz", 0),
            Some((Cow::Borrowed("foo:bar/baz"), 2))
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("foo:", "bar/baz//../../", 0),
            Some((Cow::Borrowed("foo:"), 0))
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("foo:/bar/..", "baz/./qux/..", 0),
            Some((Cow::Borrowed("foo:/bar/../baz"), 1))
        );
        // neg tests
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("/foo", "../bar", 0),
            None
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("/foo", "/bar", 0),
            None
        );
        assert_eq!(
            canonicalize_using_cwd_with_max_upward("/foo/..", "../bar/..", 0),
            None
        );
    }
    // Test whether paths are borrowed or owned
    #[test]
    fn test_cow_borrowing() {
        let cwd_opt = None;
        for (path, should_owned) in [
            ("/foo/bar/baz", false),
            ("/scheme/foo/bar/baz", false),
            ("foo/bar/baz", false), // this is None(Cow::Borrowed)
            ("/scheme/foo/../baz", true),
            ("foo:bar", true),
        ] {
            assert_eq!(
                matches!(canonicalize_using_cwd(cwd_opt, path), Some(Cow::Owned(_))),
                should_owned
            );
        }
    }
    // Test whether dirname works
    #[test]
    fn test_dirname() {
        for (a, b) in [
            ("/foo/bar/baz", "/foo/bar"),
            ("/baz/foo/bar/", "/baz/foo"),
            ("/", "/"),
            ("/scheme/foo", "/scheme"),
            ("foo:bar/baz/", "foo:bar"),
            ("foo:bar", "foo:"),
            ("foo:", "foo:"),
        ] {
            assert_eq!(
                RedoxPath::from_absolute(a).unwrap().dirname(),
                RedoxPath::from_absolute(b).unwrap()
            );
        }
        for (a, b) in [
            ("foo/bar/.", "foo"),
            ("./foo/bar/..", "."),
            ("./.", ""),
            (".", ""),
            ("", ""),
        ] {
            assert_eq!(
                RedoxStr::new(a).unwrap().dirname(),
                RedoxStr::new(b).unwrap()
            );
        }
    }

    // Tests paths that may be used with orbital:
    #[test]
    fn test_orbital_scheme() {
        for flag_str in &["", "abflrtu"] {
            for x in &[-1, 0, 1] {
                for y in &[-1, 0, 1] {
                    for w in &[0, 1] {
                        for h in &[0, 1] {
                            for title in &[
                                "",
                                "title",
                                "title/with/slashes",
                                "title:with:colons",
                                "title/../with/../dots/..",
                            ] {
                                let path = format!(
                                    "orbital:{}/{}/{}/{}/{}/{}",
                                    flag_str, x, y, w, h, title
                                );
                                assert_eq!(
                                    canonicalize_using_cwd(None, &path),
                                    Some((&path).into())
                                );
                            }
                        }
                    }
                }
            }
        }
    }

    // Tests path splitting to parts
    #[test]
    fn test_parts() {
        for (path, scheme, reference) in [
            ("/foo/bar/baz", "file", "foo/bar/baz"),
            ("/scheme/foo/bar/baz", "foo", "bar/baz"),
            ("/", "file", ""),
            ("/bar", "file", "bar"),
            ("/...", "file", "..."),
            ("//double/slash", "file", "/double/slash"),
            ("/ending/in/slash/", "file", "ending/in/slash/"),
            ("/contains/dot/.", "file", "contains/dot/."),
            ("/contains/dotdot/..", "file", "contains/dotdot/.."),
        ] {
            let redox_path = RedoxPath::from_absolute(path).unwrap();
            let parts = redox_path.as_parts();
            assert_eq!(
                (path, parts),
                (
                    path,
                    Some((
                        RedoxScheme::new(scheme).unwrap(),
                        RedoxReference::new(reference).unwrap()
                    ))
                )
            );
            if path.starts_with("/scheme") {
                let to_string = format!("/scheme/{scheme}");
                let joined_path = RedoxPath::from_absolute(&to_string)
                    .unwrap()
                    .join(reference)
                    .unwrap();
                assert_eq!(path, &format!("{joined_path}"));
            } else {
                assert_eq!(path, &format!("/{reference}"));
            }
        }

        // fail if the path is not absolute
        assert_eq!(RedoxPath::from_absolute("not/absolute"), None);

        // fail if the scheme characters are not valid
        for path in ["/scheme/use:colon/", "/scheme/βeta/"] {
            let redox_path = RedoxPath::from_absolute(path).unwrap();
            let parts = redox_path.as_parts();
            assert_eq!((path, parts), (path, None));
        }
    }

    #[test]
    fn test_old_scheme_parts() {
        for (path, scheme, reference) in [
            ("foo:bar/baz", "foo", "bar/baz"),
            ("emptyref:", "emptyref", ""),
            (":emptyscheme", "", "emptyscheme"),
        ] {
            let redox_path = RedoxPath::from_absolute(path).unwrap();
            let parts = redox_path.as_parts();
            assert_eq!(
                (path, parts),
                (
                    path,
                    Some((
                        RedoxScheme::new(scheme).unwrap(),
                        RedoxReference::new(reference).unwrap()
                    ))
                )
            );
        }

        // slash is not allowed in scheme names
        assert_eq!(RedoxPath::from_absolute("scheme/withslash:path"), None);
        // empty path is not allowed for from_absolute
        assert_eq!(RedoxPath::from_absolute(""), None)
    }

    #[test]
    fn test_matches() {
        assert!(RedoxPath::from_absolute("/scheme/foo")
            .unwrap()
            .matches_scheme("foo"));
        assert!(RedoxPath::from_absolute("/scheme/foo/bar")
            .unwrap()
            .matches_scheme("foo"));
        assert!(!RedoxPath::from_absolute("/scheme/foo")
            .unwrap()
            .matches_scheme("bar"));
        assert!(RedoxPath::from_absolute("foo:")
            .unwrap()
            .matches_scheme("foo"));
        assert!(RedoxPath::from_absolute(
            canonicalize_using_cwd(Some("/scheme/foo"), "bar").unwrap()
        )
        .unwrap()
        .matches_scheme("foo"));
        assert!(
            RedoxPath::from_absolute(canonicalize_using_cwd(Some("/foo"), "bar").unwrap())
                .unwrap()
                .matches_scheme("file")
        );
        assert!(RedoxPath::from_absolute(
            canonicalize_using_cwd(Some("/scheme"), "foo/bar").unwrap()
        )
        .unwrap()
        .matches_scheme("foo"));
        assert!(RedoxPath::from_absolute("foo:/bar")
            .unwrap()
            .matches_scheme("foo"));
        assert!(!RedoxPath::from_absolute("foo:/bar")
            .unwrap()
            .matches_scheme("bar"));
        assert!(RedoxPath::from_absolute("/scheme/file")
            .unwrap()
            .is_default_scheme());
        assert!(!RedoxPath::from_absolute("/scheme/foo")
            .unwrap()
            .is_default_scheme());
        assert!(RedoxPath::from_absolute("file:bar")
            .unwrap()
            .is_default_scheme());
        assert!(RedoxPath::from_absolute("file:")
            .unwrap()
            .is_default_scheme());
        assert!(!RedoxPath::from_absolute("foo:bar")
            .unwrap()
            .is_default_scheme());
        assert!(RedoxPath::from_absolute("foo:bar").unwrap().is_legacy());
        assert!(!RedoxPath::from_absolute("/foo/bar").unwrap().is_legacy());
    }

    #[test]
    fn test_to_standard() {
        assert_eq!(
            RedoxPath::from_absolute("foo:bar")
                .unwrap()
                .to_standard()
                .as_ref(),
            "/scheme/foo/bar"
        );
        assert_eq!(
            RedoxPath::from_absolute("file:bar")
                .unwrap()
                .to_standard()
                .as_ref(),
            "/scheme/file/bar"
        );
        assert_eq!(
            RedoxPath::from_absolute("/scheme/foo/bar")
                .unwrap()
                .to_standard()
                .as_ref(),
            "/scheme/foo/bar"
        );
        assert_eq!(
            RedoxPath::from_absolute("/foo/bar")
                .unwrap()
                .to_standard()
                .as_ref(),
            "/foo/bar"
        );
        assert_eq!(
            &RedoxPath::from_absolute("foo:bar/../bar2")
                .unwrap()
                .to_standard_canon()
                .to_string(),
            "/scheme/foo/bar2"
        );
        assert_eq!(
            &RedoxPath::from_absolute("file:bar/./../bar2")
                .unwrap()
                .to_standard_canon()
                .to_string(),
            "/scheme/file/bar2"
        );
        assert_eq!(
            &RedoxPath::from_absolute("/scheme/file/bar/./../../foo/bar")
                .unwrap()
                .to_standard_canon()
                .to_string(),
            "/scheme/foo/bar"
        );
        assert_eq!(
            &RedoxPath::from_absolute("/foo/bar")
                .unwrap()
                .to_standard_canon()
                .to_string(),
            "/foo/bar"
        );
        assert_eq!(
            &canonicalize_to_standard(None, "/scheme/foo/bar").unwrap(),
            "/scheme/foo/bar"
        );
        assert_eq!(
            &canonicalize_to_standard(None, "foo:bar").unwrap(),
            "/scheme/foo/bar"
        );
        assert_eq!(
            &canonicalize_to_standard(None, "foo:bar/../..").unwrap(),
            "/scheme/foo"
        );
        assert_eq!(
            &canonicalize_to_standard(None, "/scheme/foo/bar/..").unwrap(),
            "/scheme/foo"
        );
        assert_eq!(
            &canonicalize_to_standard(None, "foo:bar/bar2/..").unwrap(),
            "/scheme/foo/bar"
        );
    }

    #[test]
    fn test_scheme_path() {
        assert_eq!(
            scheme_path("foo"),
            Some(RedoxPath::from_absolute("/scheme/foo").unwrap())
        );
        assert_eq!(
            scheme_path(""),
            Some(RedoxPath::from_absolute("/scheme").unwrap())
        );
        assert_eq!(scheme_path("/foo"), None);
        assert_eq!(scheme_path("foo/bar"), None);
        assert_eq!(scheme_path("foo:"), None);
    }

    #[test]
    fn test_category() {
        assert_eq!(
            make_scheme_name("foo", "bar"),
            Some(RedoxScheme::new("foo.bar").unwrap())
        );
        assert_eq!(
            RedoxPath::from_absolute(
                scheme_path(make_scheme_name("foo", "bar").unwrap().as_ref()).unwrap()
            )
            .unwrap(),
            RedoxPath::Standard(RedoxReference::new("/scheme/foo.bar").unwrap())
        );
        assert_eq!(make_scheme_name("foo", "/bar"), None);
        assert_eq!(make_scheme_name("foo", ":bar"), None);
        assert!(RedoxPath::from_absolute(
            scheme_path(make_scheme_name("foo", "bar").unwrap().as_ref()).unwrap()
        )
        .unwrap()
        .is_scheme_category("foo"));
        assert!(RedoxPath::from_absolute("/scheme/foo.bar/bar2")
            .unwrap()
            .is_scheme_category("foo"));
        assert!(!RedoxPath::from_absolute("/scheme/foo/bar")
            .unwrap()
            .is_scheme_category("foo"));
        assert!(!RedoxPath::from_absolute("/foo.bar/bar2")
            .unwrap()
            .is_scheme_category("foo"));
    }
}