sorug 0.5.0

Ultra-high-performance, zero-copy, WHATWG-compliant URL parser
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
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
//! WHATWG [basic URL parser](https://url.spec.whatwg.org/#url-parsing).
//!
//! Builds a CoW serialization (`Backing`) with rust-url-style component offsets.

#![allow(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss,
    clippy::doc_markdown,
    clippy::elidable_lifetime_names,
    clippy::if_not_else,
    clippy::manual_let_else,
    clippy::match_like_matches_macro,
    clippy::needless_pass_by_value,
    clippy::range_plus_one,
    clippy::redundant_closure,
    clippy::redundant_closure_for_method_calls,
    clippy::too_many_arguments,
    clippy::too_many_lines,
    clippy::unnecessary_wraps,
    clippy::unwrap_used
)]

pub(crate) mod fast;
pub(crate) mod host;
pub(crate) mod percent;
pub(crate) mod punycode;
pub(crate) mod scan;
pub(crate) mod serialization;
pub(crate) mod setters;
pub(crate) mod unicode_ranges;

use alloc::borrow::{Cow, ToOwned};
use alloc::string::String;
use core::fmt::Write as _;
use core::str::Chars;

use self::host::{AppendedHost, Host, append_host, parse_host, parse_opaque_host};
use self::percent::{
    append_fragment, append_path_segment, append_query, in_c0_encode_set, in_path_encode_set,
    in_userinfo_encode_set, percent_encode_char, utf8_percent_encode,
};
use self::scan::{
    find_authority_end, find_file_host_end, find_hash, find_host_end, find_last_at,
    find_path_delim, find_path_delim_setter, find_query_or_hash, has_ascii_tab_or_newline,
};
use crate::{Backing, ParseError};

use self::serialization::SerializationBuf;

// ---------------------------------------------------------------------------
// Flag bits (must match `crate::UrlFlags`)
// ---------------------------------------------------------------------------

pub(crate) const FLAG_SPECIAL: u8 = 1 << 0;
pub(crate) const FLAG_HAS_CREDENTIALS: u8 = 1 << 1;
pub(crate) const FLAG_HAS_EMPTY_HOST: u8 = 1 << 2;
pub(crate) const FLAG_OPAQUE_PATH: u8 = 1 << 3;
pub(crate) const FLAG_HAS_PASSWORD: u8 = 1 << 4;
pub(crate) const FLAG_HOST_IPV4: u8 = 1 << 5;
pub(crate) const FLAG_HOST_IPV6: u8 = 1 << 6;
pub(crate) const FLAG_HOST_IDNA: u8 = 1 << 7;

/// Parsed URL record with CoW serialization (rust-url layout).
#[derive(Clone, Debug, Eq)]
pub(crate) struct ParsedUrl<'a> {
    pub serialization: Backing<'a>,
    /// Exclusive end of scheme (before `:`).
    pub scheme_end: u32,
    /// Exclusive end of username in serialization.
    pub username_end: u32,
    pub host_start: u32,
    pub host_end: u32,
    /// `None` = null port (default or absent).
    pub port: Option<u16>,
    pub path_start: u32,
    /// Index of `?`, or `None`.
    pub query_start: Option<u32>,
    /// Index of `#`, or `None`.
    pub fragment_start: Option<u32>,
    pub flags: u8,
}

impl PartialEq for ParsedUrl<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.serialization.as_str() == other.serialization.as_str()
            && self.scheme_end == other.scheme_end
            && self.username_end == other.username_end
            && self.host_start == other.host_start
            && self.host_end == other.host_end
            && self.port == other.port
            && self.path_start == other.path_start
            && self.query_start == other.query_start
            && self.fragment_start == other.fragment_start
            && self.flags == other.flags
    }
}

impl ParsedUrl<'_> {
    #[inline]
    pub(crate) fn scheme(&self) -> &str {
        &self.serialization.as_str()[..self.scheme_end as usize]
    }

    #[inline]
    #[allow(dead_code)]
    pub(crate) fn is_special(&self) -> bool {
        self.flags & FLAG_SPECIAL != 0
    }

    #[inline]
    pub(crate) fn has_opaque_path(&self) -> bool {
        self.flags & FLAG_OPAQUE_PATH != 0
    }

    #[inline]
    fn slice(&self, range: core::ops::RangeTo<u32>) -> &str {
        &self.serialization.as_str()[..range.end as usize]
    }

    #[inline]
    fn host_str(&self) -> Option<&str> {
        if self.host_start == self.host_end {
            if self.flags & FLAG_HAS_EMPTY_HOST != 0 {
                Some("")
            } else {
                // Null host (no authority / opaque path without host).
                None
            }
        } else {
            Some(&self.serialization.as_str()[self.host_start as usize..self.host_end as usize])
        }
    }

    fn byte_at(&self, i: u32) -> u8 {
        self.serialization.as_bytes()[i as usize]
    }

    #[inline]
    #[allow(dead_code)]
    fn as_str(&self) -> &str {
        self.serialization.as_str()
    }
}

/// Parse `input` per the WHATWG basic URL parser.
pub(crate) fn parse<'i>(
    input: &'i str,
    base: Option<&ParsedUrl<'_>>,
) -> Result<ParsedUrl<'i>, ParseError> {
    let trimmed = input.trim_matches(|ch: char| ch <= ' ');
    if base.is_none() {
        if let Some(fast) = fast::try_fast_special_absolute(trimmed) {
            return Ok(fast);
        }
    }
    Parser {
        serialization: SerializationBuf::new(trimmed),
        base_url: base,
        for_setter: false,
    }
    .parse_url(trimmed)
}

// ---------------------------------------------------------------------------
// Scheme helpers
// ---------------------------------------------------------------------------

#[derive(Copy, Clone, PartialEq, Eq)]
pub(crate) enum SchemeType {
    File,
    SpecialNotFile,
    NotSpecial,
}

impl SchemeType {
    #[inline]
    pub(crate) fn is_special(self) -> bool {
        !matches!(self, Self::NotSpecial)
    }

    #[inline]
    pub(crate) fn is_file(self) -> bool {
        matches!(self, Self::File)
    }
}

impl From<&str> for SchemeType {
    fn from(s: &str) -> Self {
        match s {
            "http" | "https" | "ws" | "wss" | "ftp" => Self::SpecialNotFile,
            "file" => Self::File,
            _ => Self::NotSpecial,
        }
    }
}

fn default_port(scheme: &str) -> Option<u16> {
    match scheme {
        "http" | "ws" => Some(80),
        "https" | "wss" => Some(443),
        "ftp" => Some(21),
        _ => None,
    }
}

/// Public crate helper for origin serialization / setters.
#[inline]
pub(crate) fn default_port_for_scheme(scheme: &str) -> Option<u16> {
    default_port(scheme)
}

/// Parse a host for URL setters (mirrors rust-url `Parser::parse_host`).
///
/// Returns `(host, host_was_empty_domain, remaining)`.
pub(crate) fn parse_host_setter<'i>(
    mut input: Input<'i>,
    scheme_type: SchemeType,
) -> Result<(Host<'static>, bool, Input<'i>), ParseError> {
    if scheme_type.is_file() {
        let (_, host_str, remaining) = Parser::file_host(input)?;
        if host_str.is_empty() {
            return Ok((Host::Domain(Cow::Owned(String::new())), true, remaining));
        }
        let host = match parse_host(&host_str)? {
            Host::Domain(ref d) if d.as_ref() == "localhost" => {
                Host::Domain(Cow::Owned(String::new()))
            }
            Host::Domain(d) => Host::Domain(Cow::Owned(d.into_owned())),
            Host::Ipv4(ip) => Host::Ipv4(ip),
            Host::Ipv6(ip) => Host::Ipv6(ip),
        };
        let empty = matches!(&host, Host::Domain(d) if d.is_empty());
        return Ok((host, empty, remaining));
    }

    let input_str = input.as_str();
    let bytes = input_str.as_bytes();
    let (byte_end, has_ignored) = find_host_end(bytes, scheme_type.is_special());

    let host_str: Cow<'_, str> = if has_ignored {
        Cow::Owned(
            input_str[..byte_end]
                .chars()
                .filter(|c| !is_ascii_tab_or_newline(*c))
                .collect(),
        )
    } else {
        Cow::Borrowed(&input_str[..byte_end])
    };
    input.skip_bytes(byte_end);

    if scheme_type == SchemeType::SpecialNotFile && host_str.is_empty() {
        return Err(ParseError::Failure);
    }

    if !scheme_type.is_special() {
        let host = parse_opaque_host(&host_str)?;
        let empty = matches!(&host, Host::Domain(d) if d.is_empty());
        return Ok((host, empty, input));
    }

    if host_str.is_empty() {
        return Ok((Host::Domain(Cow::Owned(String::new())), true, input));
    }

    let host = match parse_host(&host_str)? {
        Host::Domain(d) => Host::Domain(Cow::Owned(d.into_owned())),
        Host::Ipv4(ip) => Host::Ipv4(ip),
        Host::Ipv6(ip) => Host::Ipv6(ip),
    };
    Ok((host, false, input))
}

fn scheme_flags(scheme_type: SchemeType) -> u8 {
    if scheme_type.is_special() {
        FLAG_SPECIAL
    } else {
        0
    }
}

// ---------------------------------------------------------------------------
// Input (skips ASCII tab / LF / CR)
// ---------------------------------------------------------------------------

#[derive(Clone)]
pub(crate) struct Input<'i> {
    chars: Chars<'i>,
}

impl<'i> Input<'i> {
    #[inline]
    pub(crate) fn new(input: &'i str) -> Self {
        Self {
            chars: input.chars(),
        }
    }

    /// Setter-friendly input: no C0/space trim; still skips ASCII tab/LF/CR while iterating.
    #[inline]
    pub(crate) fn new_no_trim(input: &'i str) -> Self {
        Self::new(input)
    }

    /// Trim leading/trailing ASCII tab/LF/CR (query setter).
    #[inline]
    pub(crate) fn new_trim_tab_and_newlines(original: &'i str) -> Self {
        let input = original.trim_matches(|ch: char| matches!(ch, '\t' | '\n' | '\r'));
        Self::new(input)
    }

    #[allow(dead_code)]
    fn new_trim_c0_control_and_space(original: &'i str) -> Self {
        let input = original.trim_matches(|ch: char| ch <= ' ');
        Self::new(input)
    }

    #[inline]
    fn is_empty(&self) -> bool {
        self.clone().next().is_none()
    }

    #[inline]
    fn starts_with_char(&self, p: char) -> bool {
        self.clone().next() == Some(p)
    }

    #[inline]
    fn starts_with_ascii_alpha(&self) -> bool {
        matches!(self.clone().next(), Some(c) if c.is_ascii_alphabetic())
    }

    #[inline]
    fn split_prefix_str(&self, s: &str) -> Option<Self> {
        let mut remaining = self.clone();
        for c in s.chars() {
            if remaining.next() != Some(c) {
                return None;
            }
        }
        Some(remaining)
    }

    #[inline]
    fn split_prefix_char(&self, c: char) -> Option<Self> {
        let mut remaining = self.clone();
        if remaining.next() == Some(c) {
            Some(remaining)
        } else {
            None
        }
    }

    #[inline]
    fn split_first(&self) -> (Option<char>, Self) {
        let mut remaining = self.clone();
        (remaining.next(), remaining)
    }

    #[inline]
    fn peek_char(&self) -> Option<char> {
        self.clone().next()
    }

    #[inline]
    fn count_matching(&self, mut f: impl FnMut(char) -> bool) -> (u32, Self) {
        let mut count = 0;
        let mut remaining = self.clone();
        loop {
            let mut input = remaining.clone();
            match input.next() {
                Some(c) if f(c) => {
                    remaining = input;
                    count += 1;
                }
                _ => return (count, remaining),
            }
        }
    }

    #[inline]
    fn next_utf8(&mut self) -> Option<(char, &'i str)> {
        loop {
            let utf8 = self.chars.as_str();
            let c = self.chars.next()?;
            if !is_ascii_tab_or_newline(c) {
                return Some((c, &utf8[..c.len_utf8()]));
            }
        }
    }

    #[inline]
    fn as_str(&self) -> &'i str {
        self.chars.as_str()
    }

    /// Advance the cursor by `nbytes` raw bytes (must be a char boundary).
    #[inline]
    fn skip_bytes(&mut self, nbytes: usize) {
        let s = self.chars.as_str();
        debug_assert!(nbytes <= s.len());
        debug_assert!(s.is_char_boundary(nbytes));
        self.chars = s[nbytes..].chars();
    }

    /// Remaining bytes without allocating.
    #[inline]
    fn as_bytes(&self) -> &'i [u8] {
        self.chars.as_str().as_bytes()
    }
}

impl Iterator for Input<'_> {
    type Item = char;

    fn next(&mut self) -> Option<char> {
        self.chars.by_ref().find(|&c| !is_ascii_tab_or_newline(c))
    }
}

#[inline]
fn is_ascii_tab_or_newline(ch: char) -> bool {
    matches!(ch, '\t' | '\n' | '\r')
}

#[inline]
pub(crate) fn to_u32(i: usize) -> Result<u32, ParseError> {
    u32::try_from(i).map_err(|_| ParseError::InputTooLong)
}

// ---------------------------------------------------------------------------
// Windows drive letter helpers
// ---------------------------------------------------------------------------

#[inline]
fn is_windows_drive_letter(segment: &str) -> bool {
    segment.len() == 2 && starts_with_windows_drive_letter(segment)
}

#[inline]
fn is_normalized_windows_drive_letter(segment: &str) -> bool {
    is_windows_drive_letter(segment) && segment.as_bytes().get(1) == Some(&b':')
}

fn path_starts_with_windows_drive_letter(s: &str) -> bool {
    match s.as_bytes().first() {
        Some(b'/' | b'\\' | b'?' | b'#') => starts_with_windows_drive_letter(&s[1..]),
        _ => false,
    }
}

fn starts_with_windows_drive_letter(s: &str) -> bool {
    let b = s.as_bytes();
    b.len() >= 2
        && (b[0] as char).is_ascii_alphabetic()
        && matches!(b[1], b':' | b'|')
        && (b.len() == 2 || matches!(b[2], b'/' | b'\\' | b'?' | b'#'))
}

fn starts_with_windows_drive_letter_segment(input: &Input<'_>) -> bool {
    let mut input = input.clone();
    match (input.next(), input.next(), input.next()) {
        (Some(a), Some(b), Some(c))
            if a.is_ascii_alphabetic()
                && matches!(b, ':' | '|')
                && matches!(c, '/' | '\\' | '?' | '#') =>
        {
            true
        }
        (Some(a), Some(b), None) if a.is_ascii_alphabetic() && matches!(b, ':' | '|') => true,
        _ => false,
    }
}

// ---------------------------------------------------------------------------
// Parser
// ---------------------------------------------------------------------------

struct Parser<'b, 'i> {
    serialization: SerializationBuf<'i>,
    base_url: Option<&'b ParsedUrl<'b>>,
    /// When true, `?` / `#` are path/query content (percent-encoded), not delimiters.
    for_setter: bool,
}

impl<'b, 'i> Parser<'b, 'i> {
    fn parse_url(mut self, input: &'i str) -> Result<ParsedUrl<'i>, ParseError> {
        // `input` is already C0/space-trimmed by `parse`.
        let input = Input::new(input);
        if let Ok(remaining) = self.parse_scheme(input.clone()) {
            return self.parse_with_scheme(remaining);
        }

        // no scheme
        if let Some(base_url) = self.base_url {
            if input.starts_with_char('#') {
                return self.fragment_only(base_url, input);
            }
            if base_url.has_opaque_path() {
                return Err(ParseError::Failure);
            }
            let scheme_type = SchemeType::from(base_url.scheme());
            if scheme_type.is_file() {
                self.parse_file(input, scheme_type, Some(base_url))
            } else {
                self.parse_relative(input, scheme_type, base_url)
            }
        } else {
            Err(ParseError::Failure)
        }
    }

    fn parse_scheme<'s>(&mut self, mut input: Input<'s>) -> Result<Input<'s>, ()> {
        if !input.starts_with_ascii_alpha() {
            return Err(());
        }
        debug_assert!(self.serialization.is_empty());
        while let Some(c) = input.next() {
            match c {
                'a'..='z' | '0'..='9' | '+' | '-' | '.' => self.serialization.push(c),
                'A'..='Z' => self.serialization.push(c.to_ascii_lowercase()),
                ':' => return Ok(input),
                _ => {
                    self.serialization.clear();
                    return Err(());
                }
            }
        }
        self.serialization.clear();
        Err(())
    }

    fn parse_with_scheme(mut self, input: Input<'i>) -> Result<ParsedUrl<'i>, ParseError> {
        let scheme_end = to_u32(self.serialization.len())?;
        let scheme_type = SchemeType::from(self.serialization.as_str());
        self.serialization.push(':');
        match scheme_type {
            SchemeType::File => {
                let base_file_url = self.base_url.filter(|base| base.scheme() == "file");
                self.serialization.clear();
                self.parse_file(input, scheme_type, base_file_url)
            }
            SchemeType::SpecialNotFile => {
                let (slashes_count, remaining) = input.count_matching(|c| matches!(c, '/' | '\\'));
                if let Some(base_url) = self.base_url {
                    if slashes_count < 2
                        && base_url.scheme() == &self.serialization.as_str()[..scheme_end as usize]
                    {
                        self.serialization.clear();
                        return self.parse_relative(input, scheme_type, base_url);
                    }
                }
                self.after_double_slash(remaining, scheme_type, scheme_end)
            }
            SchemeType::NotSpecial => self.parse_non_special(input, scheme_type, scheme_end),
        }
    }

    fn parse_non_special(
        mut self,
        input: Input<'i>,
        scheme_type: SchemeType,
        scheme_end: u32,
    ) -> Result<ParsedUrl<'i>, ParseError> {
        if let Some(input) = input.split_prefix_str("//") {
            return self.after_double_slash(input, scheme_type, scheme_end);
        }
        // No authority — opaque or hierarchical path.
        let path_start = to_u32(self.serialization.len())?;
        let username_end = path_start;
        let host_start = path_start;
        let host_end = path_start;
        let port = None;
        let mut flags = scheme_flags(scheme_type);
        let remaining = if let Some(input) = input.split_prefix_char('/') {
            self.serialization.push('/');
            self.parse_path(scheme_type, &mut false, path_start as usize, input)
        } else {
            flags |= FLAG_OPAQUE_PATH;
            self.parse_opaque_path(input)
        };
        self.with_query_and_fragment(
            scheme_type,
            scheme_end,
            username_end,
            host_start,
            host_end,
            port,
            path_start,
            flags,
            remaining,
        )
    }

    fn parse_file(
        mut self,
        input: Input<'i>,
        scheme_type: SchemeType,
        base_file_url: Option<&ParsedUrl>,
    ) -> Result<ParsedUrl<'i>, ParseError> {
        debug_assert!(self.serialization.is_empty());
        let (first_char, input_after_first_char) = input.split_first();
        if matches!(first_char, Some('/' | '\\')) {
            let (next_char, input_after_next_char) = input_after_first_char.split_first();
            if matches!(next_char, Some('/' | '\\')) {
                // file host state
                self.serialization.push_str("file://");
                let scheme_end = "file".len() as u32;
                let host_start = "file://".len() as u32;
                let (path_start_flag, host_empty, remaining) =
                    self.parse_file_host(input_after_next_char)?;
                let host_end = to_u32(self.serialization.len())?;
                let mut has_host = !host_empty;
                let remaining = if path_start_flag {
                    self.parse_path_start(SchemeType::File, &mut has_host, remaining)
                } else {
                    let path_start = self.serialization.len();
                    self.serialization.push('/');
                    self.parse_path(SchemeType::File, &mut has_host, path_start, remaining)
                };

                let (query_start, fragment_start) =
                    self.parse_query_and_fragment(scheme_type, scheme_end, remaining)?;
                let mut flags = FLAG_SPECIAL;
                if !has_host {
                    flags |= FLAG_HAS_EMPTY_HOST;
                }
                return Ok(ParsedUrl {
                    serialization: self.serialization.finish(),
                    scheme_end,
                    username_end: host_start,
                    host_start,
                    host_end,
                    port: None,
                    path_start: host_end,
                    query_start,
                    fragment_start,
                    flags,
                });
            }

            // file slash (single slash)
            self.serialization.push_str("file://");
            let scheme_end = "file".len() as u32;
            let host_start = "file://".len();
            let mut host_end = host_start;
            let mut flags = FLAG_SPECIAL | FLAG_HAS_EMPTY_HOST;
            // WHATWG file slash: always copy base host; only the drive-letter
            // append is gated on the input not starting with a Windows drive.
            if let Some(base_url) = base_file_url {
                if let Some(host_str) = base_url.host_str() {
                    self.serialization.push_str(host_str);
                    host_end = self.serialization.len();
                    flags = FLAG_SPECIAL
                        | (base_url.flags
                            & (FLAG_HOST_IPV4 | FLAG_HOST_IPV6 | FLAG_HAS_EMPTY_HOST));
                    if host_end == host_start {
                        flags |= FLAG_HAS_EMPTY_HOST;
                    } else {
                        flags &= !FLAG_HAS_EMPTY_HOST;
                    }
                }
                if !starts_with_windows_drive_letter_segment(&input_after_first_char) {
                    if let Some(first_segment) = file_first_path_segment(base_url) {
                        if is_normalized_windows_drive_letter(first_segment) {
                            self.serialization.push('/');
                            self.serialization.push_str(first_segment);
                        }
                    }
                }
            }

            let parse_path_input = if let Some(c) = first_char {
                if matches!(c, '/' | '\\' | '?' | '#') {
                    input
                } else {
                    input_after_first_char
                }
            } else {
                input_after_first_char
            };

            let remaining =
                self.parse_path(SchemeType::File, &mut false, host_end, parse_path_input);
            let host_start = host_start as u32;
            let (query_start, fragment_start) =
                self.parse_query_and_fragment(scheme_type, scheme_end, remaining)?;
            let host_end = host_end as u32;
            return Ok(ParsedUrl {
                serialization: self.serialization.finish(),
                scheme_end,
                username_end: host_start,
                host_start,
                host_end,
                port: None,
                path_start: host_end,
                query_start,
                fragment_start,
                flags,
            });
        }

        if let Some(base_url) = base_file_url {
            match first_char {
                None => {
                    let before_fragment = match base_url.fragment_start {
                        Some(i) => &base_url.serialization.as_str()[..i as usize],
                        None => base_url.serialization.as_str(),
                    };
                    self.serialization.push_str(before_fragment);
                    Ok(ParsedUrl {
                        serialization: self.serialization.finish(),
                        fragment_start: None,
                        scheme_end: base_url.scheme_end,
                        username_end: base_url.username_end,
                        host_start: base_url.host_start,
                        host_end: base_url.host_end,
                        port: base_url.port,
                        path_start: base_url.path_start,
                        query_start: base_url.query_start,
                        flags: base_url.flags,
                    })
                }
                Some('?') => {
                    let before_query = match (base_url.query_start, base_url.fragment_start) {
                        (None, None) => base_url.serialization.as_str(),
                        (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
                    };
                    self.serialization.push_str(before_query);
                    let (query_start, fragment_start) =
                        self.parse_query_and_fragment(scheme_type, base_url.scheme_end, input)?;
                    Ok(ParsedUrl {
                        serialization: self.serialization.finish(),
                        query_start,
                        fragment_start,
                        scheme_end: base_url.scheme_end,
                        username_end: base_url.username_end,
                        host_start: base_url.host_start,
                        host_end: base_url.host_end,
                        port: base_url.port,
                        path_start: base_url.path_start,
                        flags: base_url.flags,
                    })
                }
                Some('#') => self.fragment_only(base_url, input),
                _ => {
                    // Copy base host/path/query, then either shorten or reset path
                    // for a Windows drive letter quirk (host is preserved).
                    let before_query = match (base_url.query_start, base_url.fragment_start) {
                        (None, None) => base_url.serialization.as_str(),
                        (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
                    };
                    self.serialization.push_str(before_query);
                    if starts_with_windows_drive_letter_segment(&input) {
                        self.serialization.truncate(base_url.path_start as usize);
                    } else {
                        self.shorten_path(SchemeType::File, base_url.path_start as usize);
                    }
                    ensure_path_segment_boundary(
                        &mut self.serialization,
                        base_url.path_start as usize,
                        &input,
                        true,
                    );
                    let remaining = self.parse_path(
                        SchemeType::File,
                        &mut true,
                        base_url.path_start as usize,
                        input,
                    );
                    self.with_query_and_fragment(
                        SchemeType::File,
                        base_url.scheme_end,
                        base_url.username_end,
                        base_url.host_start,
                        base_url.host_end,
                        base_url.port,
                        base_url.path_start,
                        base_url.flags,
                        remaining,
                    )
                }
            }
        } else {
            self.serialization.push_str("file:///");
            let scheme_end = "file".len() as u32;
            let path_start = "file://".len();
            let remaining = self.parse_path(SchemeType::File, &mut false, path_start, input);
            let (query_start, fragment_start) =
                self.parse_query_and_fragment(SchemeType::File, scheme_end, remaining)?;
            let path_start = path_start as u32;
            Ok(ParsedUrl {
                serialization: self.serialization.finish(),
                scheme_end,
                username_end: path_start,
                host_start: path_start,
                host_end: path_start,
                port: None,
                path_start,
                query_start,
                fragment_start,
                flags: FLAG_SPECIAL | FLAG_HAS_EMPTY_HOST,
            })
        }
    }

    fn parse_relative(
        mut self,
        input: Input<'i>,
        scheme_type: SchemeType,
        base_url: &ParsedUrl,
    ) -> Result<ParsedUrl<'i>, ParseError> {
        debug_assert!(self.serialization.is_empty());
        let (first_char, input_after_first_char) = input.split_first();
        match first_char {
            None => {
                let before_fragment = match base_url.fragment_start {
                    Some(i) => &base_url.serialization.as_str()[..i as usize],
                    None => base_url.serialization.as_str(),
                };
                self.serialization.push_str(before_fragment);
                Ok(ParsedUrl {
                    serialization: self.serialization.finish(),
                    fragment_start: None,
                    scheme_end: base_url.scheme_end,
                    username_end: base_url.username_end,
                    host_start: base_url.host_start,
                    host_end: base_url.host_end,
                    port: base_url.port,
                    path_start: base_url.path_start,
                    query_start: base_url.query_start,
                    flags: base_url.flags,
                })
            }
            Some('?') => {
                let before_query = match (base_url.query_start, base_url.fragment_start) {
                    (None, None) => base_url.serialization.as_str(),
                    (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
                };
                self.serialization.push_str(before_query);
                let (query_start, fragment_start) =
                    self.parse_query_and_fragment(scheme_type, base_url.scheme_end, input)?;
                Ok(ParsedUrl {
                    serialization: self.serialization.finish(),
                    query_start,
                    fragment_start,
                    scheme_end: base_url.scheme_end,
                    username_end: base_url.username_end,
                    host_start: base_url.host_start,
                    host_end: base_url.host_end,
                    port: base_url.port,
                    path_start: base_url.path_start,
                    flags: base_url.flags,
                })
            }
            Some('#') => self.fragment_only(base_url, input),
            // Relative slash: only `/`, or `\` when the URL is special.
            Some('\\') if !scheme_type.is_special() => {
                // Non-special: `\` is a normal path code point (not a delimiter).
                self.parse_relative_path(input, scheme_type, base_url)
            }
            Some('/' | '\\') => {
                let slash_pred: fn(char) -> bool = if scheme_type.is_special() {
                    |c| matches!(c, '/' | '\\')
                } else {
                    |c| c == '/'
                };
                let (slashes_count, _) = input.count_matching(slash_pred);
                if slashes_count >= 2 {
                    let scheme_end = base_url.scheme_end;
                    debug_assert!(base_url.byte_at(scheme_end) == b':');
                    self.serialization
                        .push_str(base_url.slice(..scheme_end + 1));
                    // Consume exactly two authority slashes; leave the rest for
                    // special-authority-ignore (special) or path (non-special).
                    let mut after_two = input.clone();
                    let _ = after_two.next();
                    let _ = after_two.next();
                    return self.after_double_slash(after_two, scheme_type, scheme_end);
                }
                let path_start = base_url.path_start;
                self.serialization.push_str(base_url.slice(..path_start));
                self.serialization.push('/');
                let remaining = self.parse_path(
                    scheme_type,
                    &mut true,
                    path_start as usize,
                    input_after_first_char,
                );
                self.with_query_and_fragment(
                    scheme_type,
                    base_url.scheme_end,
                    base_url.username_end,
                    base_url.host_start,
                    base_url.host_end,
                    base_url.port,
                    base_url.path_start,
                    base_url.flags,
                    remaining,
                )
            }
            _ => self.parse_relative_path(input, scheme_type, base_url),
        }
    }

    /// Relative-state "otherwise" branch: copy base, shorten path, continue in path state.
    fn parse_relative_path(
        mut self,
        input: Input<'i>,
        scheme_type: SchemeType,
        base_url: &ParsedUrl<'_>,
    ) -> Result<ParsedUrl<'i>, ParseError> {
        let before_query = match (base_url.query_start, base_url.fragment_start) {
            (None, None) => base_url.serialization.as_str(),
            (Some(i), _) | (None, Some(i)) => base_url.slice(..i),
        };
        self.serialization.push_str(before_query);
        self.shorten_path(scheme_type, base_url.path_start as usize);
        ensure_path_segment_boundary(
            &mut self.serialization,
            base_url.path_start as usize,
            &input,
            scheme_type.is_special(),
        );
        let remaining = match input.split_first() {
            (Some('/'), remaining) => self.parse_path(
                scheme_type,
                &mut true,
                base_url.path_start as usize,
                remaining,
            ),
            _ => self.parse_path(scheme_type, &mut true, base_url.path_start as usize, input),
        };
        self.with_query_and_fragment(
            scheme_type,
            base_url.scheme_end,
            base_url.username_end,
            base_url.host_start,
            base_url.host_end,
            base_url.port,
            base_url.path_start,
            base_url.flags,
            remaining,
        )
    }

    fn after_double_slash(
        mut self,
        input: Input<'i>,
        scheme_type: SchemeType,
        scheme_end: u32,
    ) -> Result<ParsedUrl<'i>, ParseError> {
        self.serialization.push('/');
        self.serialization.push('/');
        // special authority ignore slashes state: consume extra `/` and `\`.
        let mut input = input;
        if scheme_type.is_special() {
            while matches!(input.peek_char(), Some('/' | '\\')) {
                let _ = input.next();
            }
        }
        let before_authority = self.serialization.len();
        let (username_end, remaining, cred_flags) = self.parse_userinfo(input, scheme_type)?;
        let has_authority = before_authority != self.serialization.len();
        let host_start = to_u32(self.serialization.len())?;
        let (host_end, host_kind, port, remaining, empty_host) =
            self.parse_host_and_port(remaining, scheme_end, scheme_type)?;
        if empty_host && has_authority {
            return Err(ParseError::Failure);
        }
        let path_start = to_u32(self.serialization.len())?;
        let mut has_host = !empty_host;
        let remaining = self.parse_path_start(scheme_type, &mut has_host, remaining);
        let mut flags = scheme_flags(scheme_type) | cred_flags;
        if empty_host || !has_host {
            if scheme_type.is_special() {
                flags |= FLAG_HAS_EMPTY_HOST;
            }
        } else {
            flags |= host_flags_from_kind(host_kind);
            let host_ser = &self.serialization.as_str()[host_start as usize..host_end as usize];
            if host_ser.contains("xn--") {
                flags |= FLAG_HOST_IDNA;
            }
        }
        self.with_query_and_fragment(
            scheme_type,
            scheme_end,
            username_end,
            host_start,
            host_end,
            port,
            path_start,
            flags,
            remaining,
        )
    }

    /// Returns `(username_end, remaining, credential flags)`.
    fn parse_userinfo<'s>(
        &mut self,
        mut input: Input<'s>,
        scheme_type: SchemeType,
    ) -> Result<(u32, Input<'s>, u8), ParseError> {
        let bytes = input.as_bytes();
        let special = scheme_type.is_special();
        let authority_end = find_authority_end(bytes, special);

        let Some(at) = find_last_at(bytes, authority_end) else {
            return Ok((to_u32(self.serialization.len())?, input, 0));
        };

        // Empty credentials (`@host`) — no username/password written.
        // Tabs/LF/CR before `@` count as empty too (`\t@host`).
        let userinfo_prefix = &input.as_str()[..at];
        let userinfo_empty = userinfo_prefix
            .bytes()
            .all(|b| matches!(b, b'\t' | b'\n' | b'\r'));
        if at == 0 || userinfo_empty {
            input.skip_bytes(at + 1); // consume optional ws + '@'
            let (c, _) = input.split_first();
            if matches!(c, Some('/' | '?' | '#')) || (special && c == Some('\\')) || c.is_none() {
                return Err(ParseError::Failure);
            }
            return Ok((to_u32(self.serialization.len())?, input, 0));
        }

        // Encode userinfo bytes before `@`, skipping ASCII tab/LF/CR.
        let userinfo = userinfo_prefix;
        let mut username_end = None;
        let mut has_password = false;
        let mut has_username = false;
        let mut i = 0;
        let ub = userinfo.as_bytes();
        while i < ub.len() {
            let b = ub[i];
            if matches!(b, b'\t' | b'\n' | b'\r') {
                i += 1;
                continue;
            }
            if b == b':' && username_end.is_none() {
                username_end = Some(to_u32(self.serialization.len())?);
                // Peek whether any non-ignored byte remains after this `:`.
                let rest_has_char = ub[i + 1..]
                    .iter()
                    .any(|&c| !matches!(c, b'\t' | b'\n' | b'\r'));
                if rest_has_char {
                    self.serialization.push(':');
                    has_password = true;
                }
                i += 1;
                continue;
            }
            if !has_password {
                has_username = true;
            }
            // Multi-byte UTF-8: take full char for percent-encode.
            let ch = userinfo[i..].chars().next().unwrap();
            let len = ch.len_utf8();
            percent_encode_char(ch, in_userinfo_encode_set, &mut self.serialization);
            i += len;
        }

        let username_end = match username_end {
            Some(i) => i,
            None => to_u32(self.serialization.len())?,
        };
        let mut flags = 0u8;
        if has_username || has_password {
            self.serialization.push('@');
            flags |= FLAG_HAS_CREDENTIALS;
        }
        if has_password {
            flags |= FLAG_HAS_PASSWORD;
        }
        input.skip_bytes(at + 1); // past `@`
        Ok((username_end, input, flags))
    }

    fn parse_host_and_port<'s>(
        &mut self,
        input: Input<'s>,
        scheme_end: u32,
        scheme_type: SchemeType,
    ) -> Result<(u32, HostKind, Option<u16>, Input<'s>, bool), ParseError> {
        let (appended, remaining) = self.parse_host_input_append(input, scheme_type)?;
        let empty_host = matches!(appended, AppendedHost::EmptyDomain);
        if empty_host {
            if remaining.starts_with_char(':') {
                return Err(ParseError::Failure);
            }
            if scheme_type.is_special() {
                return Err(ParseError::Failure);
            }
        }
        let host_end = to_u32(self.serialization.len())?;
        let host_kind = HostKind::from(appended);

        let (port, remaining) = if let Some(remaining) = remaining.split_prefix_char(':') {
            let scheme = &self.serialization.as_str()[..scheme_end as usize];
            let default = default_port(scheme);
            let (port, remaining) = Self::parse_port(remaining, default)?;
            if let Some(port) = port {
                self.serialization.push(':');
                let buf = itoa_buf(port);
                self.serialization.push_str(buf.as_str());
            }
            (port, remaining)
        } else {
            (None, remaining)
        };
        Ok((host_end, host_kind, port, remaining, empty_host))
    }

    /// Parse host from `input` and append its serialization form (special hosts
    /// write ACE/IPv4/IPv6 once; opaque hosts still go through [`Host`] Display).
    fn parse_host_input_append<'h>(
        &mut self,
        mut input: Input<'h>,
        scheme_type: SchemeType,
    ) -> Result<(AppendedHost, Input<'h>), ParseError> {
        debug_assert!(!scheme_type.is_file());
        let input_str = input.as_str();
        let bytes = input_str.as_bytes();
        let (byte_end, has_ignored) = find_host_end(bytes, scheme_type.is_special());

        if has_ignored {
            // Slow path: rebuild host without tab/LF/CR (may include non-ASCII).
            let mut rebuilt = String::with_capacity(byte_end);
            for c in input_str[..byte_end].chars() {
                if !is_ascii_tab_or_newline(c) {
                    rebuilt.push(c);
                }
            }
            input.skip_bytes(byte_end);
            if scheme_type == SchemeType::SpecialNotFile && rebuilt.is_empty() {
                return Err(ParseError::Failure);
            }
            if !scheme_type.is_special() {
                return self.append_opaque_host(&rebuilt).map(|h| (h, input));
            }
            if rebuilt.is_empty() {
                return Ok((AppendedHost::EmptyDomain, input));
            }
            return append_host(&rebuilt, &mut self.serialization).map(|h| (h, input));
        }

        let host_str = &input_str[..byte_end];
        input.skip_bytes(byte_end);

        if scheme_type == SchemeType::SpecialNotFile && host_str.is_empty() {
            return Err(ParseError::Failure);
        }
        if !scheme_type.is_special() {
            return self.append_opaque_host(host_str).map(|h| (h, input));
        }
        if host_str.is_empty() {
            return Ok((AppendedHost::EmptyDomain, input));
        }
        append_host(host_str, &mut self.serialization).map(|h| (h, input))
    }

    fn append_opaque_host(&mut self, host_str: &str) -> Result<AppendedHost, ParseError> {
        let host = parse_opaque_host(host_str)?;
        match &host {
            Host::Domain(d) if d.is_empty() => Ok(AppendedHost::EmptyDomain),
            Host::Domain(d) => {
                self.serialization.push_str(d.as_ref());
                Ok(AppendedHost::Domain)
            }
            Host::Ipv4(ip) => {
                let _ = write!(&mut self.serialization, "{ip}");
                Ok(AppendedHost::Ipv4)
            }
            Host::Ipv6(_) => {
                let _ = write!(&mut self.serialization, "{host}");
                Ok(AppendedHost::Ipv6)
            }
        }
    }

    /// Returns `(use_path_start, host_is_empty, remaining)`.
    fn parse_file_host<'s>(
        &mut self,
        input: Input<'s>,
    ) -> Result<(bool, bool, Input<'s>), ParseError> {
        let (has_host_buffer, host_str, remaining) = Self::file_host(input)?;
        // Windows drive letter quirk: buffer is reused in path state (not path start).
        if !has_host_buffer {
            return Ok((false, true, remaining));
        }
        if host_str.is_empty() {
            // Empty host → path start state (preserves leading empty path segments).
            return Ok((true, true, remaining));
        }
        match parse_host(&host_str)? {
            Host::Domain(ref d) if d.as_ref() == "localhost" || d.is_empty() => {
                Ok((true, true, remaining))
            }
            host => {
                let _ = write!(&mut self.serialization, "{host}");
                Ok((true, false, remaining))
            }
        }
    }

    pub(crate) fn file_host(input: Input<'_>) -> Result<(bool, String, Input<'_>), ParseError> {
        let input_str = input.as_str();
        let bytes = input_str.as_bytes();
        let (byte_end, has_ignored) = find_file_host_end(bytes);

        let host_str = if has_ignored {
            let mut out = String::with_capacity(byte_end);
            for c in input_str[..byte_end].chars() {
                if !is_ascii_tab_or_newline(c) {
                    out.push(c);
                }
            }
            out
        } else {
            input_str[..byte_end].to_owned()
        };

        if is_windows_drive_letter(&host_str) {
            return Ok((false, String::new(), input));
        }
        let mut remaining = input;
        remaining.skip_bytes(byte_end);
        Ok((true, host_str, remaining))
    }

    fn parse_port(
        mut input: Input<'_>,
        default: Option<u16>,
    ) -> Result<(Option<u16>, Input<'_>), ParseError> {
        let mut port: u32 = 0;
        let mut has_any_digit = false;
        while let (Some(c), remaining) = input.split_first() {
            if let Some(digit) = c.to_digit(10) {
                port = port * 10 + digit;
                if port > u16::MAX as u32 {
                    return Err(ParseError::Failure);
                }
                has_any_digit = true;
                input = remaining;
            } else if !matches!(c, '/' | '\\' | '?' | '#') {
                return Err(ParseError::Failure);
            } else {
                break;
            }
        }
        let mut opt_port = Some(port as u16);
        if !has_any_digit || opt_port == default {
            opt_port = None;
        }
        Ok((opt_port, input))
    }
}

/// Port parser for URL component setters (`Context::Setter` in rust-url).
///
/// Digits are consumed until a non-digit; non-digits do **not** fail (unlike the
/// URL parser). Overflow fails. Empty input → null port. Empty-with-remaining
/// (leading non-digit) fails. Default port → `None`.
pub(crate) fn parse_port_setter(
    mut input: Input<'_>,
    default: Option<u16>,
) -> Result<(Option<u16>, Input<'_>), ParseError> {
    let mut port: u32 = 0;
    let mut has_any_digit = false;
    while let (Some(c), remaining) = input.split_first() {
        if let Some(digit) = c.to_digit(10) {
            port = port * 10 + digit;
            if port > u16::MAX as u32 {
                return Err(ParseError::Failure);
            }
            has_any_digit = true;
            input = remaining;
        } else {
            break;
        }
    }
    // No digits → failure (caller treats empty raw string separately for clear).
    if !has_any_digit {
        return Err(ParseError::Failure);
    }
    let mut opt_port = Some(port as u16);
    if opt_port == default {
        opt_port = None;
    }
    Ok((opt_port, input))
}

// Re-open Parser impl block for path / query helpers used by setters.
impl<'b, 'i> Parser<'b, 'i> {
    pub(crate) fn for_setter(serialization: SerializationBuf<'i>) -> Self {
        Self {
            serialization,
            base_url: None,
            for_setter: true,
        }
    }

    /// Scheme parse for setters: EOF before `:` is success (unlike the URL parser).
    pub(crate) fn parse_scheme_setter<'s>(
        &mut self,
        mut input: Input<'s>,
    ) -> Result<Input<'s>, ()> {
        if !input.starts_with_ascii_alpha() {
            return Err(());
        }
        debug_assert!(self.serialization.is_empty());
        while let Some(c) = input.next() {
            match c {
                'a'..='z' | '0'..='9' | '+' | '-' | '.' => self.serialization.push(c),
                'A'..='Z' => self.serialization.push(c.to_ascii_lowercase()),
                ':' => return Ok(input),
                _ => {
                    self.serialization.clear();
                    return Err(());
                }
            }
        }
        // Setter context: EOF is OK.
        Ok(input)
    }

    pub(crate) fn parse_path_start<'s>(
        &mut self,
        scheme_type: SchemeType,
        has_host: &mut bool,
        input: Input<'s>,
    ) -> Input<'s> {
        let path_start = self.serialization.len();
        let (maybe_c, remaining) = input.split_first();
        if scheme_type.is_special() {
            // Special URLs always have a non-empty path. Note that `file://` /
            // `https://` already end with `/` from the authority delimiter —
            // that slash is *before* path_start, so we still append a path `/`.
            self.serialization.push('/');
            if matches!(maybe_c, Some('/' | '\\')) {
                return self.parse_path(scheme_type, has_host, path_start, remaining);
            }
            return self.parse_path(scheme_type, has_host, path_start, input);
        } else if !self.for_setter && matches!(maybe_c, Some('?' | '#')) {
            return input;
        }

        if maybe_c.is_some() && maybe_c != Some('/') {
            self.serialization.push('/');
        }
        self.parse_path(scheme_type, has_host, path_start, input)
    }

    pub(crate) fn parse_path<'s>(
        &mut self,
        scheme_type: SchemeType,
        _has_host: &mut bool,
        path_start: usize,
        mut input: Input<'s>,
    ) -> Input<'s> {
        let special = scheme_type.is_special();
        loop {
            let segment_start = self.serialization.len();
            let mut ends_with_slash = false;

            // SIMD fast path: no tab/LF/CR in the upcoming segment → bulk copy.
            let raw = input.as_str();
            let bytes = raw.as_bytes();
            let delim = if self.for_setter {
                find_path_delim_setter(bytes, special)
            } else {
                find_path_delim(bytes, special)
            };
            let seg_end = delim.map_or(bytes.len(), |(i, _)| i);
            let chunk = &bytes[..seg_end];

            if !(has_ascii_tab_or_newline(chunk)
                || (scheme_type.is_file()
                    && self.serialization.len() > path_start
                    && is_normalized_windows_drive_letter(&self.serialization[path_start + 1..])))
            {
                // Bulk-append the segment (percent-encode only when needed).
                let segment_str = &raw[..seg_end];
                append_path_segment(segment_str, &mut self.serialization);
                input.skip_bytes(seg_end);

                if let Some((_, d)) = delim {
                    match d {
                        b'/' => {
                            self.serialization.push('/');
                            ends_with_slash = true;
                            input.skip_bytes(1);
                        }
                        b'\\' if special => {
                            self.serialization.push('/');
                            ends_with_slash = true;
                            input.skip_bytes(1);
                        }
                        b'?' | b'#' => {
                            // Leave delimiter for the caller (URL parser only).
                        }
                        _ => unreachable!(),
                    }
                }
            } else {
                // Slow path: char-wise (tabs / Windows-drive mid-segment quirks).
                loop {
                    let input_before_c = input.clone();
                    let c = match input.next() {
                        Some(c) => c,
                        None => break,
                    };
                    match c {
                        '/' => {
                            self.serialization.push(c);
                            ends_with_slash = true;
                            break;
                        }
                        '\\' if special => {
                            self.serialization.push('/');
                            ends_with_slash = true;
                            break;
                        }
                        '?' | '#' if !self.for_setter => {
                            input = input_before_c;
                            break;
                        }
                        _ => {
                            // Do not inject `/` after a Windows drive letter. WHATWG /
                            // Chrome keep `file:///p:foo` and `file:///p:./x` without an
                            // extra slash (injecting one left a stale `/./` when tabs
                            // delayed the `.` segment recognition).
                            percent_encode_char(c, in_path_encode_set, &mut self.serialization);
                        }
                    }
                }
            }

            let seg_action = {
                let segment_before_slash = if ends_with_slash {
                    &self.serialization[segment_start..self.serialization.len() - 1]
                } else {
                    &self.serialization[segment_start..self.serialization.len()]
                };
                match segment_before_slash {
                    ".." | "%2e%2e" | "%2e%2E" | "%2E%2e" | "%2E%2E" | "%2e." | "%2E." | ".%2e"
                    | ".%2E" => SegAction::DotDot,
                    "." | "%2e" | "%2E" => SegAction::Dot,
                    s if scheme_type.is_file()
                        && is_windows_drive_letter(s)
                        && self.serialization[path_start..segment_start]
                            .bytes()
                            .all(|b| b == b'/') =>
                    {
                        SegAction::WinDrive(s.chars().next().unwrap())
                    }
                    _ => SegAction::Other,
                }
            };
            match seg_action {
                SegAction::DotDot => {
                    debug_assert!(
                        segment_start == 0
                            || self.serialization.as_bytes().get(segment_start - 1) == Some(&b'/')
                    );
                    self.serialization.truncate(segment_start);
                    if self.serialization.ends_with('/')
                        && last_slash_can_be_removed(
                            &self.serialization,
                            path_start,
                            scheme_type.is_file(),
                        )
                    {
                        self.serialization.pop();
                    }
                    self.shorten_path(scheme_type, path_start);
                    if !self.serialization.ends_with('/') {
                        self.serialization.push('/');
                    }
                }
                SegAction::Dot => {
                    self.serialization.truncate(segment_start);
                    if !self.serialization.ends_with('/') {
                        self.serialization.push('/');
                    }
                }
                SegAction::WinDrive(c) => {
                    self.serialization.truncate(segment_start);
                    self.serialization.push(c);
                    self.serialization.push(':');
                    if ends_with_slash {
                        self.serialization.push('/');
                    }
                }
                SegAction::Other => {}
            }
            if !ends_with_slash {
                break;
            }
        }

        // NOTE: Older parsers collapsed leading empty file path segments
        // (`trim_start_matches('/')`). Current WHATWG + WPT preserve them
        // (e.g. `file:////`, `file://spider///`).

        input
    }

    fn shorten_path(&mut self, scheme_type: SchemeType, path_start: usize) {
        if self.serialization.len() == path_start {
            return;
        }
        if scheme_type.is_file()
            && is_normalized_windows_drive_letter(&self.serialization[path_start..])
        {
            return;
        }
        self.pop_path(scheme_type, path_start);
    }

    fn pop_path(&mut self, scheme_type: SchemeType, path_start: usize) {
        if self.serialization.len() > path_start {
            let slash_position = self.serialization[path_start..].rfind('/').unwrap();
            let segment_start = path_start + slash_position + 1;
            // Don’t pop a Windows drive letter (file URLs only).
            if !(scheme_type.is_file()
                && is_normalized_windows_drive_letter(&self.serialization[segment_start..]))
            {
                self.serialization.truncate(segment_start);
            }
        }
    }

    pub(crate) fn parse_opaque_path<'s>(&mut self, mut input: Input<'s>) -> Input<'s> {
        let path_begin = self.serialization.len();
        loop {
            let raw = input.as_str();
            let bytes = raw.as_bytes();

            // Fast path when the next stretch has no tab/LF/CR.
            match find_query_or_hash(bytes) {
                Some((i, _)) if !has_ascii_tab_or_newline(&bytes[..i]) => {
                    // Trailing spaces before `?`/`#`: all but the last stay literal;
                    // the last becomes `%20` (WHATWG opaque-path round-trip rule).
                    let mut space_start = i;
                    while space_start > 0 && bytes[space_start - 1] == b' ' {
                        space_start -= 1;
                    }
                    if space_start > 0 {
                        utf8_percent_encode(
                            &raw[..space_start],
                            in_c0_encode_set,
                            &mut self.serialization,
                        );
                    }
                    if i > space_start {
                        let n = i - space_start;
                        for _ in 0..n.saturating_sub(1) {
                            self.serialization.push(' ');
                        }
                        self.serialization.push_str("%20");
                    }
                    input.skip_bytes(i);
                    return input;
                }
                None if !has_ascii_tab_or_newline(bytes) => {
                    // EOF: strip trailing spaces, bulk-encode the rest.
                    let mut end = bytes.len();
                    while end > 0 && bytes[end - 1] == b' ' {
                        end -= 1;
                    }
                    if end > 0 {
                        utf8_percent_encode(&raw[..end], in_c0_encode_set, &mut self.serialization);
                    }
                    input.skip_bytes(bytes.len());
                    return input;
                }
                _ => {}
            }

            // Slow path (tabs / mixed): char-wise with the trailing-space rule.
            let input_before_c = input.clone();
            match input.next_utf8() {
                Some(('?' | '#', _)) => return input_before_c,
                Some((' ', _)) => {
                    let remaining = input.clone();
                    if matches!(remaining.peek_char(), Some('?' | '#')) {
                        self.serialization.push_str("%20");
                    } else {
                        self.serialization.push(' ');
                    }
                }
                Some((_, utf8_c)) => {
                    utf8_percent_encode(utf8_c, in_c0_encode_set, &mut self.serialization);
                }
                None => {
                    while self.serialization.len() > path_begin && self.serialization.ends_with(' ')
                    {
                        self.serialization.pop();
                    }
                    return input;
                }
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn with_query_and_fragment(
        mut self,
        scheme_type: SchemeType,
        scheme_end: u32,
        username_end: u32,
        host_start: u32,
        host_end: u32,
        port: Option<u16>,
        mut path_start: u32,
        flags: u8,
        remaining: Input<'i>,
    ) -> Result<ParsedUrl<'i>, ParseError> {
        // Anarchist URL empty-path-segment fix (rust-url / WHATWG).
        let scheme_end_usize = scheme_end as usize;
        let path_start_usize = path_start as usize;
        if path_start_usize == scheme_end_usize + 1 {
            if self.serialization[path_start_usize..].starts_with("//") {
                self.serialization.insert_str(path_start_usize, "/.");
                path_start += 2;
            }
        } else if path_start_usize == scheme_end_usize + 3
            && &self.serialization[scheme_end_usize..path_start_usize] == ":/."
        {
            debug_assert_eq!(self.serialization.as_bytes()[path_start_usize], b'/');
            if self
                .serialization
                .as_bytes()
                .get(path_start_usize + 1)
                .copied()
                != Some(b'/')
            {
                self.serialization
                    .replace_range(scheme_end_usize..path_start_usize, ":");
                path_start -= 2;
            }
        }

        let (query_start, fragment_start) =
            self.parse_query_and_fragment(scheme_type, scheme_end, remaining)?;
        Ok(ParsedUrl {
            serialization: self.serialization.finish(),
            scheme_end,
            username_end,
            host_start,
            host_end,
            port,
            path_start,
            query_start,
            fragment_start,
            flags,
        })
    }

    fn parse_query_and_fragment(
        &mut self,
        scheme_type: SchemeType,
        _scheme_end: u32,
        mut input: Input<'i>,
    ) -> Result<(Option<u32>, Option<u32>), ParseError> {
        let mut query_start = None;
        match input.next() {
            Some('#') => {}
            Some('?') => {
                query_start = Some(to_u32(self.serialization.len())?);
                self.serialization.push('?');
                if let Some(remaining) = self.parse_query(scheme_type, input) {
                    input = remaining;
                } else {
                    return Ok((query_start, None));
                }
            }
            None => return Ok((None, None)),
            _ => panic!("parse_query_and_fragment called without ? or #"),
        }

        let fragment_start = to_u32(self.serialization.len())?;
        self.serialization.push('#');
        self.parse_fragment(input);
        Ok((query_start, Some(fragment_start)))
    }

    pub(crate) fn parse_query<'s>(
        &mut self,
        scheme_type: SchemeType,
        mut input: Input<'s>,
    ) -> Option<Input<'s>> {
        let special = scheme_type.is_special();
        loop {
            let raw = input.as_str();
            let bytes = raw.as_bytes();
            if self.for_setter {
                // Setters: `#` is query content (percent-encoded), not a delimiter.
                if !has_ascii_tab_or_newline(bytes) {
                    append_query(raw, special, &mut self.serialization);
                    input.skip_bytes(bytes.len());
                    return None;
                }
                match input.next_utf8() {
                    Some((_, utf8_c)) => {
                        append_query(utf8_c, special, &mut self.serialization);
                    }
                    None => return None,
                }
                continue;
            }
            match find_hash(bytes) {
                Some(i) if !has_ascii_tab_or_newline(&bytes[..i]) => {
                    append_query(&raw[..i], special, &mut self.serialization);
                    input.skip_bytes(i + 1); // consume `#`
                    return Some(input);
                }
                None if !has_ascii_tab_or_newline(bytes) => {
                    append_query(raw, special, &mut self.serialization);
                    input.skip_bytes(bytes.len());
                    return None;
                }
                _ => {
                    // Slow path with ignored chars.
                    let input_before = input.clone();
                    match input.next_utf8() {
                        Some(('#', _)) => {
                            let _ = input_before;
                            return Some(input);
                        }
                        Some((_, utf8_c)) => {
                            append_query(utf8_c, special, &mut self.serialization);
                        }
                        None => return None,
                    }
                }
            }
        }
    }

    fn fragment_only(
        mut self,
        base_url: &ParsedUrl,
        mut input: Input<'i>,
    ) -> Result<ParsedUrl<'i>, ParseError> {
        let before_fragment = match base_url.fragment_start {
            Some(i) => base_url.slice(..i),
            None => base_url.serialization.as_str(),
        };
        debug_assert!(self.serialization.is_empty());
        self.serialization
            .reserve(before_fragment.len() + input.as_str().len());
        self.serialization.push_str(before_fragment);
        self.serialization.push('#');
        let next = input.next();
        debug_assert_eq!(next, Some('#'));
        self.parse_fragment(input);
        Ok(ParsedUrl {
            serialization: self.serialization.finish(),
            fragment_start: Some(to_u32(before_fragment.len())?),
            scheme_end: base_url.scheme_end,
            username_end: base_url.username_end,
            host_start: base_url.host_start,
            host_end: base_url.host_end,
            port: base_url.port,
            path_start: base_url.path_start,
            query_start: base_url.query_start,
            flags: base_url.flags,
        })
    }

    pub(crate) fn parse_fragment(&mut self, mut input: Input<'_>) {
        loop {
            let raw = input.as_str();
            let bytes = raw.as_bytes();
            if !has_ascii_tab_or_newline(bytes) {
                append_fragment(raw, &mut self.serialization);
                input.skip_bytes(bytes.len());
                return;
            }
            match input.next_utf8() {
                Some((_, utf8_c)) => {
                    append_fragment(utf8_c, &mut self.serialization);
                }
                None => return,
            }
        }
    }
}

fn last_slash_can_be_removed(serialization: &str, path_start: usize, is_file: bool) -> bool {
    let url_before_segment = &serialization[..serialization.len() - 1];
    if let Some(segment_before_start) = url_before_segment.rfind('/') {
        // Only file URLs protect the slash after a Windows drive letter.
        segment_before_start >= path_start
            && !(is_file
                && path_starts_with_windows_drive_letter(&serialization[segment_before_start..]))
    } else {
        false
    }
}

fn file_first_path_segment<'a>(url: &'a ParsedUrl<'_>) -> Option<&'a str> {
    let s = url.serialization.as_str();
    let path = &s[url.path_start as usize..];
    let path = match (url.query_start, url.fragment_start) {
        (Some(q), _) => &s[url.path_start as usize..q as usize],
        (None, Some(f)) => &s[url.path_start as usize..f as usize],
        (None, None) => path,
    };
    let path = path.strip_prefix('/').unwrap_or(path);
    if path.is_empty() {
        return None;
    }
    Some(path.split('/').next().unwrap_or(path))
}

/// After shortening a path, ensure the next path-state segment starts on a
/// boundary (list-model append), not glued onto the previous segment.
fn ensure_path_segment_boundary(
    serialization: &mut SerializationBuf<'_>,
    path_start: usize,
    input: &Input<'_>,
    special_or_nonempty_ok: bool,
) {
    if input.is_empty() && !special_or_nonempty_ok {
        return;
    }
    if serialization.len() == path_start {
        if special_or_nonempty_ok || !input.is_empty() {
            serialization.push('/');
        }
        return;
    }
    // New path-state buffer must not concatenate onto the last segment.
    if !input.is_empty() && !serialization.ends_with('/') && !matches!(input.peek_char(), Some('/'))
    {
        serialization.push('/');
    }
}

#[derive(Copy, Clone)]
enum SegAction {
    DotDot,
    Dot,
    WinDrive(char),
    Other,
}

#[derive(Copy, Clone)]
enum HostKind {
    Domain,
    Ipv4,
    Ipv6,
}

impl From<&Host<'_>> for HostKind {
    fn from(h: &Host) -> Self {
        match h {
            Host::Domain(_) => Self::Domain,
            Host::Ipv4(_) => Self::Ipv4,
            Host::Ipv6(_) => Self::Ipv6,
        }
    }
}

impl From<AppendedHost> for HostKind {
    fn from(h: AppendedHost) -> Self {
        match h {
            AppendedHost::EmptyDomain | AppendedHost::Domain => Self::Domain,
            AppendedHost::Ipv4 => Self::Ipv4,
            AppendedHost::Ipv6 => Self::Ipv6,
        }
    }
}

fn host_flags_from_kind(kind: HostKind) -> u8 {
    match kind {
        HostKind::Domain => 0,
        HostKind::Ipv4 => FLAG_HOST_IPV4,
        HostKind::Ipv6 => FLAG_HOST_IPV6,
    }
}

struct ItoaBuf {
    buf: [u8; 5],
    start: usize,
}

impl ItoaBuf {
    fn as_str(&self) -> &str {
        // Only ASCII digits.
        // SAFETY: buf is filled with ASCII digit bytes only.
        core::str::from_utf8(&self.buf[self.start..]).unwrap_or("0")
    }
}

fn itoa_buf(mut value: u16) -> ItoaBuf {
    let mut buf = [b'0'; 5];
    let mut i = 5;
    if value == 0 {
        return ItoaBuf { buf, start: 4 };
    }
    while value > 0 {
        i -= 1;
        buf[i] = b'0' + (value % 10) as u8;
        value /= 10;
    }
    ItoaBuf { buf, start: i }
}

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

    #[test]
    fn parse_https_absolute() {
        let url = parse("https://example.com/foo", None).unwrap();
        assert_eq!(url.scheme(), "https");
        assert_eq!(url.serialization.as_str(), "https://example.com/foo");
        assert_eq!(url.port, None);
        assert!(url.flags & FLAG_SPECIAL != 0);
        assert_eq!(
            &url.serialization.as_str()[url.host_start as usize..url.host_end as usize],
            "example.com"
        );
    }

    #[test]
    fn parse_default_port_null() {
        let url = parse("http://example.com:80/", None).unwrap();
        assert_eq!(url.port, None);
        assert_eq!(url.serialization.as_str(), "http://example.com/");
    }

    #[test]
    fn parse_explicit_port() {
        let url = parse("http://example.com:8080/", None).unwrap();
        assert_eq!(url.port, Some(8080));
        assert_eq!(url.serialization.as_str(), "http://example.com:8080/");
    }

    #[test]
    fn parse_no_scheme_without_base_fails() {
        assert!(parse("/path", None).is_err());
    }

    #[test]
    fn parse_file_simple() {
        let url = parse("file:///foo/bar", None).unwrap();
        assert_eq!(url.scheme(), "file");
        assert!(url.flags & FLAG_HAS_EMPTY_HOST != 0);
        assert_eq!(url.serialization.as_str(), "file:///foo/bar");
    }

    #[test]
    fn parse_opaque_path() {
        let url = parse("mailto:user@example.com", None).unwrap();
        assert!(url.flags & FLAG_OPAQUE_PATH != 0);
        assert_eq!(url.serialization.as_str(), "mailto:user@example.com");
    }

    #[test]
    fn parse_path_dot_segments() {
        let url = parse("https://example.com/a/./b/../c", None).unwrap();
        assert_eq!(url.serialization.as_str(), "https://example.com/a/c");
    }

    #[test]
    fn parse_ipv6() {
        let url = parse("http://[::1]/", None).unwrap();
        assert!(url.flags & FLAG_HOST_IPV6 != 0);
        assert_eq!(
            &url.serialization.as_str()[url.host_start as usize..url.host_end as usize],
            "[::1]"
        );
    }

    #[test]
    fn parse_userinfo() {
        let url = parse("http://user:pass@example.com/", None).unwrap();
        assert!(url.flags & FLAG_HAS_CREDENTIALS != 0);
        assert!(url.flags & FLAG_HAS_PASSWORD != 0);
        assert_eq!(url.serialization.as_str(), "http://user:pass@example.com/");
    }

    #[test]
    fn relative_against_base() {
        let base = parse("https://example.com/dir/page", None).unwrap();
        let url = parse("../x", Some(&base)).unwrap();
        assert_eq!(url.serialization.as_str(), "https://example.com/x");
    }

    #[test]
    fn parse_empty_host_fails() {
        assert!(parse("http://", None).is_err());
        assert!(parse("http:///", None).is_err());
        // Extra slashes are ignored; host becomes "path".
        let url = parse("http:///path", None).unwrap();
        assert_eq!(url.serialization.as_str(), "http://path/");
    }

    #[test]
    fn parse_query_and_fragment() {
        let url = parse("https://ex.com/p?q=1#frag", None).unwrap();
        assert_eq!(url.serialization.as_str(), "https://ex.com/p?q=1#frag");
        assert_eq!(url.query_start, Some("https://ex.com/p".len() as u32));
        assert!(url.fragment_start.is_some());
    }

    #[test]
    fn parse_non_special_with_authority() {
        let url = parse("foo://example.com:99/bar", None).unwrap();
        assert_eq!(url.serialization.as_str(), "foo://example.com:99/bar");
        assert_eq!(url.port, Some(99));
        assert_eq!(url.flags & FLAG_SPECIAL, 0);
        assert_eq!(url.flags & FLAG_OPAQUE_PATH, 0);
    }

    #[test]
    fn parse_special_backslash_as_slash() {
        let url = parse(r"http:\\example.com\foo", None).unwrap();
        assert_eq!(url.serialization.as_str(), "http://example.com/foo");
    }

    #[test]
    fn parse_file_localhost_empty_host() {
        let url = parse("file://localhost/tmp", None).unwrap();
        assert_eq!(url.serialization.as_str(), "file:///tmp");
        assert!(url.flags & FLAG_HAS_EMPTY_HOST != 0);
    }

    #[test]
    fn relative_dotdot_pops_nonfile_drive_segment() {
        let base = parse("abc://x/y/z/C:/", None).unwrap();
        let url = parse("..", Some(&base)).unwrap();
        assert_eq!(url.serialization.as_str(), "abc://x/y/z/");
    }
}

#[cfg(test)]
mod fuzz_regressions {
    use super::*;
    #[test]
    fn opaque_ipv6_leading_newlines() {
        let url = parse("fihttp://\n\n[2001:db8::1]\n\n", None).unwrap();
        assert_eq!(url.serialization.as_str(), "fihttp://[2001:db8::1]");
    }
}

#[cfg(test)]
mod triage_tick2 {
    use super::*;
    #[test]
    fn at_then_nonascii_host() {
        let url = parse("https://@\u{624}0", None).unwrap();
        assert_eq!(url.serialization.as_str(), "https://xn--0-smc/");
    }
    #[test]
    fn scheme_with_tab() {
        let url = parse("w\ts://example.com/", None).unwrap();
        assert_eq!(url.serialization.as_str(), "ws://example.com/");
    }
    #[test]
    fn file_dot_roundtrip() {
        let input = "file:/p:\n\n\n./%2e0/";
        let url = parse(input, None).unwrap();
        assert_eq!(url.serialization.as_str(), "file:///p:./%2e0/");
        let href = url.serialization.as_str().to_owned();
        let again = parse(&href, None).unwrap();
        assert_eq!(again.serialization.as_str(), href);
    }
    #[test]
    fn ws_arabic_path() {
        let url = parse("w\ts:/\u{688}0/\u{688}0.0.0.0~/../)C", None).unwrap();
        assert_eq!(url.serialization.as_str(), "ws://xn--0-isc/)C");
    }
    #[test]
    fn https_backslash_host() {
        let url = parse("https:///\\\\\\\u{7e3}2\\\\\\\\\\\u{0}\\]\\", None).unwrap();
        assert!(url.serialization.as_str().starts_with("https://xn--2-cdd"));
    }
    #[test]
    fn superscript_ipv4_rejected() {
        assert!(parse("http:255.255.255.2\u{2075}85", None).is_err());
    }
}