ferromark 0.1.3

Ultra-high-performance Markdown to HTML compiler
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
//! Inline parser for Markdown.
//!
//! Uses a three-phase approach for efficiency:
//! 1. Mark Collection: Single pass collecting delimiter positions
//! 2. Mark Resolution: Process by precedence (code spans → links → emphasis)
//! 3. Event Emission: Walk resolved marks and emit events

mod code_span;
mod emphasis;
pub mod event;
mod links;
pub mod marks;
mod math;
mod simd;
mod strikethrough;

pub use event::InlineEvent;
pub use links::AutolinkLiteralKind;

use crate::Range;
use crate::footnote::{FootnoteStore, normalize_footnote_label};
use crate::link_ref::LinkRefStore;
use code_span::{CodeSpan, extract_code_spans, resolve_code_spans};
use emphasis::{EmphasisMatch, EmphasisStacks, resolve_emphasis_with_stacks_into};
use links::{
    Autolink, AutolinkLiteral, Link, RefLink, find_autolink_literals_into, find_autolinks_into,
    resolve_links_into, resolve_reference_links_into,
};
use marks::{Mark, MarkBuffer, collect_marks, flags};
use math::{MathSpan, resolve_math_spans};
use memchr::memchr;
use strikethrough::{StrikethroughMatch, resolve_strikethrough_into};

/// Inline parser state.
pub struct InlineParser {
    /// Reusable mark buffer.
    mark_buffer: MarkBuffer,
    open_brackets: Vec<(u32, bool)>,
    close_brackets: Vec<u32>,
    autolinks: Vec<Autolink>,
    html_spans: Vec<HtmlSpan>,
    html_ranges: Vec<(u32, u32)>,
    link_dest_ranges: Vec<(u32, u32)>,
    autolink_ranges: Vec<(u32, u32)>,
    code_spans: Vec<CodeSpan>,
    link_boundaries: Vec<(u32, u32)>,
    resolved_links: Vec<Link>,
    link_formed_opens: Vec<bool>,
    link_inactive_opens: Vec<bool>,
    link_used_closes: Vec<bool>,
    ref_links: Vec<RefLink>,
    ref_label_buf: String,
    ref_formed_opens: Vec<bool>,
    ref_used_closes: Vec<bool>,
    ref_occupied: Vec<(u32, u32)>,
    autolink_literals: Vec<AutolinkLiteral>,
    emphasis_stacks: EmphasisStacks,
    emphasis_matches: Vec<EmphasisMatch>,
    strikethrough_matches: Vec<StrikethroughMatch>,
    al_code_span_ranges: Vec<(u32, u32)>,
    al_link_ranges: Vec<(u32, u32)>,
    emit_points: Vec<EmitPoint>,
    emit_suppress_ranges: Vec<(u32, u32)>,
    html_code_ranges: Vec<(usize, usize)>,
    html_autolink_ranges: Vec<(usize, usize)>,
    footnote_refs: Vec<FootnoteRef>,
    math_spans: Vec<MathSpan>,
}

impl InlineParser {
    /// Create a new inline parser.
    pub fn new() -> Self {
        Self {
            mark_buffer: MarkBuffer::new(),
            open_brackets: Vec::with_capacity(32),
            close_brackets: Vec::with_capacity(32),
            autolinks: Vec::with_capacity(8),
            html_spans: Vec::with_capacity(8),
            html_ranges: Vec::with_capacity(8),
            link_dest_ranges: Vec::with_capacity(8),
            autolink_ranges: Vec::with_capacity(8),
            code_spans: Vec::with_capacity(8),
            link_boundaries: Vec::with_capacity(16),
            resolved_links: Vec::with_capacity(8),
            link_formed_opens: Vec::with_capacity(32),
            link_inactive_opens: Vec::with_capacity(32),
            link_used_closes: Vec::with_capacity(32),
            ref_links: Vec::with_capacity(8),
            ref_label_buf: String::with_capacity(64),
            ref_formed_opens: Vec::with_capacity(32),
            ref_used_closes: Vec::with_capacity(32),
            ref_occupied: Vec::with_capacity(8),
            autolink_literals: Vec::with_capacity(8),
            emphasis_stacks: EmphasisStacks::default(),
            emphasis_matches: Vec::with_capacity(16),
            strikethrough_matches: Vec::with_capacity(8),
            al_code_span_ranges: Vec::with_capacity(8),
            al_link_ranges: Vec::with_capacity(8),
            emit_points: Vec::with_capacity(64),
            emit_suppress_ranges: Vec::with_capacity(8),
            html_code_ranges: Vec::with_capacity(8),
            html_autolink_ranges: Vec::with_capacity(8),
            footnote_refs: Vec::with_capacity(4),
            math_spans: Vec::with_capacity(4),
        }
    }

    /// Parse inline content and emit events.
    pub fn parse(
        &mut self,
        text: &[u8],
        link_refs: Option<&LinkRefStore>,
        allow_html: bool,
        events: &mut Vec<InlineEvent>,
    ) {
        self.parse_with_options(text, link_refs, allow_html, true, true, false, None, events);
    }

    /// Parse inline content with full GFM options.
    #[allow(clippy::too_many_arguments)]
    pub fn parse_with_options(
        &mut self,
        text: &[u8],
        link_refs: Option<&LinkRefStore>,
        allow_html: bool,
        strikethrough: bool,
        autolink_literals: bool,
        math: bool,
        footnote_store: Option<&FootnoteStore>,
        events: &mut Vec<InlineEvent>,
    ) {
        let has_specials = has_inline_specials(text);

        // Check for potential autolink literal triggers when enabled
        let may_have_autolinks = autolink_literals && has_autolink_candidates(text);

        if !has_specials && !may_have_autolinks {
            if !text.is_empty() {
                events.push(InlineEvent::Text(Range::from_usize(0, text.len())));
            }
            return;
        }

        // Phase 1: Collect marks
        self.mark_buffer.reserve_for_text(text.len());
        if has_specials {
            collect_marks(text, &mut self.mark_buffer);
        } else {
            self.mark_buffer.clear();
        }

        if self.mark_buffer.is_empty() && !may_have_autolinks {
            // No special characters and no autolink candidates, emit as plain text
            if !text.is_empty() {
                events.push(InlineEvent::Text(Range::from_usize(0, text.len())));
            }
            return;
        }

        // Phase 2: Resolve marks by precedence
        // First: autolinks + raw HTML (skip if no '<' present)
        let has_lt = memchr(b'<', text).is_some();
        self.autolinks.clear();
        if has_lt {
            find_autolinks_into(text, &mut self.autolinks);
        }

        // Second: raw inline HTML (ignore code spans for now; we'll filter after resolving them)
        self.html_spans.clear();
        if has_lt && allow_html {
            find_html_spans_into(
                text,
                &[],
                &self.autolinks,
                &mut self.html_code_ranges,
                &mut self.html_autolink_ranges,
                &mut self.html_spans,
            );
        }
        self.html_ranges.clear();
        self.html_ranges.reserve(self.html_spans.len());
        self.html_ranges
            .extend(self.html_spans.iter().map(|s| (s.start, s.end)));

        // Third: code spans (highest precedence, but should not start inside HTML tags)
        resolve_code_spans(self.mark_buffer.marks_mut(), text, &self.html_ranges);

        // Get code span ranges for filtering
        self.code_spans.clear();
        self.code_spans
            .extend(extract_code_spans(self.mark_buffer.marks()));
        filter_html_spans_in_code_spans(&mut self.html_spans, &self.code_spans);

        // Math spans (after code spans, before links; gated on math option)
        if math {
            self.math_spans = resolve_math_spans(self.mark_buffer.marks_mut(), text);
        } else {
            self.math_spans.clear();
        }

        // Filter autolinks that start inside code spans
        self.autolinks.retain(|al| {
            !self
                .code_spans
                .iter()
                .any(|cs| al.start >= cs.opener_pos && al.start < cs.closer_end)
        });

        // Fourth: collect bracket positions and detect emphasis/strikethrough candidates in one pass
        let (has_emphasis_marks, has_strikethrough_marks) =
            Self::collect_brackets_and_scan_emphasis(
                self.mark_buffer.marks(),
                &mut self.open_brackets,
                &mut self.close_brackets,
            );
        let has_brackets = !self.open_brackets.is_empty() && !self.close_brackets.is_empty();
        self.open_brackets
            .retain(|&(pos, _)| !pos_in_spans(pos, &self.html_spans));
        // Filter out close brackets that are inside autolinks - they can't close links
        self.close_brackets.retain(|&pos| {
            !self
                .autolinks
                .iter()
                .any(|al| pos > al.start && pos < al.end)
                && !pos_in_spans(pos, &self.html_spans)
        });
        let has_inline_link_candidate = has_brackets && has_inline_link_opener(text);
        if has_inline_link_candidate {
            resolve_links_into(
                text,
                &self.open_brackets,
                &self.close_brackets,
                &mut self.resolved_links,
                &mut self.link_formed_opens,
                &mut self.link_inactive_opens,
                &mut self.link_used_closes,
            );
        } else {
            self.resolved_links.clear();
        }
        let resolved_links = &self.resolved_links;

        if has_brackets {
            if let Some(defs) = link_refs.filter(|defs| !defs.is_empty()) {
                resolve_reference_links_into(
                    text,
                    &self.open_brackets,
                    &self.close_brackets,
                    resolved_links,
                    defs,
                    &mut self.ref_links,
                    &mut self.ref_label_buf,
                    &mut self.ref_formed_opens,
                    &mut self.ref_used_closes,
                    &mut self.ref_occupied,
                );
            } else {
                self.ref_links.clear();
            }
            filter_html_spans_in_link_destinations(&mut self.html_spans, resolved_links);
        } else {
            self.ref_links.clear();
        }
        let resolved_ref_links = &self.ref_links;

        self.link_dest_ranges.clear();
        self.link_dest_ranges
            .extend(resolved_links.iter().filter_map(|link| {
                let start = link.text_end + 1;
                let end = link.end;
                (start < end).then_some((start, end))
            }));

        self.autolink_ranges.clear();
        self.autolink_ranges
            .extend(self.autolinks.iter().map(|al| (al.start, al.end)));

        // Fifth: emphasis (lowest precedence)
        // Pass link and autolink boundaries so emphasis can't cross them
        self.link_boundaries.clear();
        self.link_boundaries.reserve(
            resolved_links.len()
                + resolved_ref_links.len()
                + self.autolinks.len()
                + self.html_spans.len(),
        );
        self.link_boundaries
            .extend(resolved_links.iter().map(|l| (l.start, l.text_end)));
        for link in resolved_ref_links {
            self.link_boundaries.push((link.start, link.text_end));
        }
        // Also include autolinks - delimiters inside <url> should not form emphasis
        for autolink in &self.autolinks {
            self.link_boundaries.push((autolink.start, autolink.end));
        }
        // Also include raw HTML spans
        for span in &self.html_spans {
            self.link_boundaries.push((span.start, span.end));
        }
        let emphasis_matches = if has_emphasis_marks {
            resolve_emphasis_with_stacks_into(
                self.mark_buffer.marks_mut(),
                &self.link_boundaries,
                &mut self.emphasis_stacks,
                &mut self.emphasis_matches,
            );
            self.emphasis_matches.as_slice()
        } else {
            self.emphasis_matches.clear();
            &[]
        };

        // Sixth: strikethrough (after emphasis, since they share the mark buffer)
        if has_strikethrough_marks && strikethrough {
            resolve_strikethrough_into(
                self.mark_buffer.marks_mut(),
                &self.link_boundaries,
                &mut self.strikethrough_matches,
            );
        } else {
            self.strikethrough_matches.clear();
        }
        let strikethrough_matches = self.strikethrough_matches.as_slice();

        // Seventh: autolink literals (bare URLs, www, emails)
        if may_have_autolinks {
            // Build code span ranges for overlap checking (reuse Vec)
            self.al_code_span_ranges.clear();
            self.al_code_span_ranges.extend(
                self.code_spans
                    .iter()
                    .map(|cs| (cs.opener_pos, cs.closer_end)),
            );
            // Build link ranges (inline links + ref links) (reuse Vec)
            self.al_link_ranges.clear();
            self.al_link_ranges
                .reserve(resolved_links.len() + resolved_ref_links.len());
            for link in resolved_links {
                self.al_link_ranges.push((link.start, link.end));
            }
            for link in resolved_ref_links {
                self.al_link_ranges.push((link.start, link.end));
            }
            find_autolink_literals_into(
                text,
                &self.al_code_span_ranges,
                &self.html_ranges,
                &self.autolink_ranges,
                &self.al_link_ranges,
                &mut self.autolink_literals,
            );
        } else {
            self.autolink_literals.clear();
        }

        // Eighth: footnote references (`[^label]`)
        self.footnote_refs.clear();
        if let Some(fn_store) = footnote_store {
            Self::resolve_footnote_refs(
                text,
                &self.open_brackets,
                &self.close_brackets,
                resolved_links,
                resolved_ref_links,
                &self.code_spans,
                fn_store,
                &mut self.footnote_refs,
            );
        }

        // Phase 3: Emit events
        let marks = self.mark_buffer.marks();
        Self::emit_events(
            marks,
            text,
            &self.code_spans,
            &self.math_spans,
            emphasis_matches,
            strikethrough_matches,
            &self.autolink_literals,
            resolved_links,
            resolved_ref_links,
            &self.autolinks,
            &self.html_spans,
            &self.link_dest_ranges,
            &self.autolink_ranges,
            &self.html_ranges,
            &self.footnote_refs,
            &mut self.emit_points,
            &mut self.emit_suppress_ranges,
            events,
        );
    }

    /// Resolve footnote references: `[^label]` patterns not consumed by links/images.
    #[allow(clippy::too_many_arguments)]
    fn resolve_footnote_refs(
        text: &[u8],
        open_brackets: &[(u32, bool)],
        close_brackets: &[u32],
        resolved_links: &[Link],
        resolved_ref_links: &[RefLink],
        code_spans: &[CodeSpan],
        footnote_store: &FootnoteStore,
        out: &mut Vec<FootnoteRef>,
    ) {
        out.clear();

        // For each open bracket, check if it's followed by `^label]`
        for &(open_pos, is_image) in open_brackets {
            if is_image {
                continue; // `![^` is an image, not a footnote
            }

            // Check if this bracket is inside a code span
            let in_code = code_spans
                .iter()
                .any(|cs| open_pos >= cs.opener_pos && open_pos < cs.closer_end);
            if in_code {
                continue;
            }

            // Check if this bracket is already consumed by a link/image
            let in_link = resolved_links.iter().any(|l| l.start == open_pos)
                || resolved_ref_links.iter().any(|l| l.start == open_pos);
            if in_link {
                continue;
            }

            // Check for `^` after `[`
            let caret_pos = (open_pos + 1) as usize;
            if caret_pos >= text.len() || text[caret_pos] != b'^' {
                continue;
            }

            // Read label: alphanumeric, dash, underscore
            let label_start = caret_pos + 1;
            let mut label_end = label_start;
            while label_end < text.len() {
                let b = text[label_end];
                if b.is_ascii_alphanumeric() || b == b'-' || b == b'_' {
                    label_end += 1;
                } else {
                    break;
                }
            }

            if label_end == label_start {
                continue; // Empty label
            }

            // Must be followed by `]`
            if label_end >= text.len() || text[label_end] != b']' {
                continue;
            }

            // Check that this `]` is in the close_brackets list (validates it's not escaped)
            let close_pos = label_end as u32;
            if !close_brackets.contains(&close_pos) {
                continue;
            }

            let label_bytes = &text[label_start..label_end];
            if let Some(normalized) = normalize_footnote_label(label_bytes) {
                if let Some(idx) = footnote_store.get_index(&normalized) {
                    out.push(FootnoteRef {
                        start: open_pos,
                        end: close_pos + 1,
                        def_index: idx as u32,
                    });
                }
            }
        }
    }

    /// Collect bracket positions for link parsing.
    /// Returns (has_emphasis_marks, has_strikethrough_marks).
    fn collect_brackets_and_scan_emphasis(
        marks: &[Mark],
        open_brackets: &mut Vec<(u32, bool)>,
        close_brackets: &mut Vec<u32>,
    ) -> (bool, bool) {
        let estimated = (marks.len() / 4).max(4);
        open_brackets.clear();
        close_brackets.clear();
        open_brackets.reserve(estimated);
        close_brackets.reserve(estimated);

        let mut has_emphasis_marks = false;
        let mut has_strikethrough_marks = false;
        for mark in marks {
            if mark.flags & flags::IN_CODE == 0 {
                if mark.ch == b'*' || mark.ch == b'_' {
                    has_emphasis_marks = true;
                } else if mark.ch == b'~' {
                    has_strikethrough_marks = true;
                }
            }
            // Skip brackets inside code spans
            if mark.flags & flags::IN_CODE != 0 && mark.ch != b'[' {
                continue;
            }

            match mark.ch {
                b'[' => {
                    // IN_CODE flag repurposed for "is_image" for brackets
                    let is_image = mark.flags & flags::IN_CODE != 0;
                    open_brackets.push((mark.pos, is_image));
                }
                b']' => {
                    close_brackets.push(mark.pos);
                }
                _ => {}
            }
        }
        (has_emphasis_marks, has_strikethrough_marks)
    }

    /// Emit events based on resolved marks.
    #[allow(clippy::too_many_arguments)]
    fn emit_events(
        marks: &[Mark],
        text: &[u8],
        code_spans: &[CodeSpan],
        math_spans: &[MathSpan],
        emphasis_matches: &[EmphasisMatch],
        strikethrough_matches: &[StrikethroughMatch],
        autolink_literals: &[AutolinkLiteral],
        resolved_links: &[Link],
        resolved_ref_links: &[RefLink],
        autolinks: &[Autolink],
        html_spans: &[HtmlSpan],
        link_dest_ranges: &[(u32, u32)],
        autolink_ranges: &[(u32, u32)],
        html_ranges: &[(u32, u32)],
        footnote_refs: &[FootnoteRef],
        emit_points: &mut Vec<EmitPoint>,
        suppress_ranges: &mut Vec<(u32, u32)>,
        events: &mut Vec<InlineEvent>,
    ) {
        let mut pos = 0u32;
        let text_len = text.len() as u32;

        let mut link_dest_idx = 0usize;
        let mut autolink_idx = 0usize;
        let mut html_idx = 0usize;
        let mut autolink_span_idx = 0usize;

        // Build sorted list of events to emit
        let estimated_events = marks.len()
            + (code_spans.len() * 3)
            + (resolved_links.len() * 4)
            + (resolved_ref_links.len() * 4)
            + autolinks.len()
            + autolink_literals.len()
            + html_spans.len()
            + (emphasis_matches.len() * 2)
            + (strikethrough_matches.len() * 2);
        emit_points.clear();
        emit_points.reserve(estimated_events.max(8));
        events.reserve(estimated_events.max(8) + 4);

        // Add code span events (filter out spans whose opener is inside an autolink)
        for span in code_spans {
            // Skip code spans whose opener starts inside an autolink
            let inside_autolink = !autolink_ranges.is_empty()
                && pos_in_ranges_u32(span.opener_pos, autolink_ranges, &mut autolink_span_idx);
            if inside_autolink {
                continue;
            }

            emit_points.push(EmitPoint {
                pos: span.opener_pos,
                kind: EmitKind::CodeSpanStart,
                end: span.opener_end,
            });
            emit_points.push(EmitPoint {
                pos: span.closer_pos,
                kind: EmitKind::CodeSpanEnd,
                end: span.closer_end,
            });
            // Code content
            let (content_start, content_end) = span.content_range();
            emit_points.push(EmitPoint {
                pos: content_start,
                kind: EmitKind::CodeContent(content_end),
                end: content_end,
            });
        }

        // Add math span events
        for span in math_spans {
            let (content_start, content_end) = span.content_range();
            // Strip leading/trailing space like code spans
            let mut cs = content_start as usize;
            let mut ce = content_end as usize;
            if ce > cs + 1 {
                let content = &text[cs..ce];
                let first_is_space = content[0] == b' ' || content[0] == b'\n';
                let last_is_space =
                    content[content.len() - 1] == b' ' || content[content.len() - 1] == b'\n';
                let not_all_space = content.iter().any(|&b| b != b' ' && b != b'\n');
                if first_is_space && last_is_space && not_all_space {
                    cs += 1;
                    ce -= 1;
                }
            }
            if span.is_display {
                emit_points.push(EmitPoint {
                    pos: span.opener_pos,
                    kind: EmitKind::MathDisplay {
                        content_start: cs as u32,
                        content_end: ce as u32,
                    },
                    end: span.closer_end,
                });
            } else {
                emit_points.push(EmitPoint {
                    pos: span.opener_pos,
                    kind: EmitKind::MathInline {
                        content_start: cs as u32,
                        content_end: ce as u32,
                    },
                    end: span.closer_end,
                });
            }
        }

        // Add link events
        for link in resolved_links {
            if link.is_image {
                emit_points.push(EmitPoint {
                    pos: link.start,
                    kind: EmitKind::ImageStart {
                        url_start: link.url_start,
                        url_end: link.url_end,
                        title_start: link.title_start,
                        title_end: link.title_end,
                    },
                    end: link.start + 2, // ![
                });
                emit_points.push(EmitPoint {
                    pos: link.text_end,
                    kind: EmitKind::ImageEnd,
                    end: link.end,
                });
            } else {
                emit_points.push(EmitPoint {
                    pos: link.start,
                    kind: EmitKind::LinkStart {
                        url_start: link.url_start,
                        url_end: link.url_end,
                        title_start: link.title_start,
                        title_end: link.title_end,
                    },
                    end: link.start + 1, // [
                });
                emit_points.push(EmitPoint {
                    pos: link.text_end,
                    kind: EmitKind::LinkEnd,
                    end: link.end,
                });
            }
        }

        // Add reference-style link events
        for link in resolved_ref_links {
            if link.is_image {
                emit_points.push(EmitPoint {
                    pos: link.start,
                    kind: EmitKind::ImageStartRef {
                        def_index: link.def_index as u32,
                    },
                    end: link.start + 2, // ![
                });
                emit_points.push(EmitPoint {
                    pos: link.text_end,
                    kind: EmitKind::ImageEnd,
                    end: link.end,
                });
            } else {
                emit_points.push(EmitPoint {
                    pos: link.start,
                    kind: EmitKind::LinkStartRef {
                        def_index: link.def_index as u32,
                    },
                    end: link.start + 1, // [
                });
                emit_points.push(EmitPoint {
                    pos: link.text_end,
                    kind: EmitKind::LinkEnd,
                    end: link.end,
                });
            }
        }
        // Add autolink events
        for autolink in autolinks {
            if autolink.is_email {
                emit_points.push(EmitPoint {
                    pos: autolink.start,
                    kind: EmitKind::AutolinkEmail {
                        content_start: autolink.content_start,
                        content_end: autolink.content_end,
                    },
                    end: autolink.end,
                });
            } else {
                emit_points.push(EmitPoint {
                    pos: autolink.start,
                    kind: EmitKind::AutolinkUrl {
                        content_start: autolink.content_start,
                        content_end: autolink.content_end,
                    },
                    end: autolink.end,
                });
            }
        }

        // Add raw HTML events
        for span in html_spans {
            emit_points.push(EmitPoint {
                pos: span.start,
                kind: EmitKind::HtmlRaw { end: span.end },
                end: span.end,
            });
        }

        // Add autolink literal events
        for al in autolink_literals {
            emit_points.push(EmitPoint {
                pos: al.start,
                kind: EmitKind::AutolinkLiteral {
                    end: al.end,
                    kind: al.kind,
                },
                end: al.end,
            });
        }

        // Add emphasis events
        for m in emphasis_matches {
            let is_strong = m.count == 2;

            if is_strong {
                emit_points.push(EmitPoint {
                    pos: m.opener_start,
                    kind: EmitKind::StrongStart,
                    end: m.opener_end,
                });
                emit_points.push(EmitPoint {
                    pos: m.closer_start,
                    kind: EmitKind::StrongEnd,
                    end: m.closer_end,
                });
            } else {
                emit_points.push(EmitPoint {
                    pos: m.opener_start,
                    kind: EmitKind::EmphasisStart,
                    end: m.opener_end,
                });
                emit_points.push(EmitPoint {
                    pos: m.closer_start,
                    kind: EmitKind::EmphasisEnd,
                    end: m.closer_end,
                });
            }
        }

        // Add strikethrough events
        for m in strikethrough_matches {
            emit_points.push(EmitPoint {
                pos: m.opener_start,
                kind: EmitKind::StrikethroughStart,
                end: m.opener_end,
            });
            emit_points.push(EmitPoint {
                pos: m.closer_start,
                kind: EmitKind::StrikethroughEnd,
                end: m.closer_end,
            });
        }

        // Add footnote reference events
        for fref in footnote_refs {
            emit_points.push(EmitPoint {
                pos: fref.start,
                kind: EmitKind::FootnoteRef {
                    def_index: fref.def_index,
                },
                end: fref.end,
            });
        }

        // Add backslash escapes and hard breaks
        // Note: Hard breaks inside code spans should not be processed
        for mark in marks {
            // Skip marks inside code spans
            let in_code = mark.flags & flags::IN_CODE != 0;

            if mark.ch == b'\\' && mark.flags & flags::POTENTIAL_OPENER != 0 {
                // Check if mark is inside a link's destination area (the (...) part)
                // This includes URL, title, and any whitespace between them
                let in_link_dest = !link_dest_ranges.is_empty()
                    && pos_in_ranges_u32(mark.pos, link_dest_ranges, &mut link_dest_idx);
                // Check if mark is inside an autolink
                let in_autolink = !autolink_ranges.is_empty()
                    && pos_in_ranges_u32(mark.pos, autolink_ranges, &mut autolink_idx);
                let in_html = !html_ranges.is_empty()
                    && pos_in_ranges_u32(mark.pos, html_ranges, &mut html_idx);

                let escaped_char = text[(mark.pos + 1) as usize];
                if escaped_char == b'\n' && !in_code && !in_autolink && !in_html {
                    // Backslash before newline is a hard break (but not in code or autolinks)
                    emit_points.push(EmitPoint {
                        pos: mark.pos,
                        kind: EmitKind::HardBreak,
                        end: mark.end,
                    });
                } else if !in_code && !in_link_dest && !in_autolink && !in_html {
                    // Skip escapes inside link URLs/titles and autolinks (they're processed by renderer)
                    emit_points.push(EmitPoint {
                        pos: mark.pos,
                        kind: EmitKind::Escape(escaped_char),
                        end: mark.end,
                    });
                }
            } else if mark.ch == b'\n' && mark.flags & flags::POTENTIAL_OPENER != 0 {
                let in_link_dest = !link_dest_ranges.is_empty()
                    && pos_in_ranges_u32(mark.pos, link_dest_ranges, &mut link_dest_idx);
                let in_html = !html_ranges.is_empty()
                    && pos_in_ranges_u32(mark.pos, html_ranges, &mut html_idx);
                if in_code || in_link_dest || in_html {
                    continue;
                }
                // Two spaces before newline is a hard break (but not in code or link destinations)
                emit_points.push(EmitPoint {
                    pos: mark.pos,
                    kind: EmitKind::HardBreak,
                    end: mark.end,
                });
            } else if mark.ch == b'\n' && mark.flags & flags::POTENTIAL_CLOSER != 0 {
                let in_link_dest = !link_dest_ranges.is_empty()
                    && pos_in_ranges_u32(mark.pos, link_dest_ranges, &mut link_dest_idx);
                let in_html = !html_ranges.is_empty()
                    && pos_in_ranges_u32(mark.pos, html_ranges, &mut html_idx);
                if in_code || in_link_dest || in_html {
                    continue;
                }
                // Soft break (newline without 2+ spaces) - also not in code or link destinations
                emit_points.push(EmitPoint {
                    pos: mark.pos,
                    kind: EmitKind::SoftBreak,
                    end: mark.end,
                });
            }
        }

        // Sort by position (end events come after start events at same position)
        emit_points.sort_unstable_by_key(|p| {
            (
                p.pos,
                matches!(
                    p.kind,
                    EmitKind::CodeSpanEnd
                        | EmitKind::StrongEnd
                        | EmitKind::EmphasisEnd
                        | EmitKind::StrikethroughEnd
                        | EmitKind::LinkEnd
                        | EmitKind::ImageEnd
                ),
            )
        });

        // Build ranges to suppress (reference labels after link text)
        suppress_ranges.clear();
        suppress_ranges.reserve(resolved_ref_links.len());
        for link in resolved_ref_links {
            if link.end > link.text_end {
                suppress_ranges.push((link.text_end, link.end));
            }
        }

        // Emit events in order
        let mut skip_until = 0u32;
        let mut suppress_idx = 0usize;

        for point in emit_points.iter() {
            // Skip events inside suppressed ranges (e.g., reference labels)
            while suppress_idx < suppress_ranges.len()
                && point.pos >= suppress_ranges[suppress_idx].1
            {
                suppress_idx += 1;
            }
            if suppress_idx < suppress_ranges.len() {
                let (s, e) = suppress_ranges[suppress_idx];
                if point.pos > s && point.pos < e {
                    pos = pos.max(point.end);
                    continue;
                }
            }
            // Emit text before this point
            if point.pos > pos && point.pos > skip_until {
                let text_start = pos.max(skip_until);
                if point.pos > text_start {
                    events.push(InlineEvent::Text(Range::from_usize(
                        text_start as usize,
                        point.pos as usize,
                    )));
                }
            }

            match point.kind {
                EmitKind::CodeSpanStart => {
                    // Skip the opening backticks
                    pos = point.end;
                }
                EmitKind::CodeSpanEnd => {
                    // Skip the closing backticks
                    skip_until = point.end;
                }
                EmitKind::CodeContent(end) => {
                    // Emit code content (strip leading/trailing space if single space)
                    let mut start = point.pos as usize;
                    let mut end = end as usize;

                    // CommonMark: line endings are converted to spaces first,
                    // then if the string both begins AND ends with a space,
                    // and doesn't consist entirely of spaces, strip one space from each end.
                    // Note: we treat \n as equivalent to space for stripping purposes.
                    if end > start + 1 {
                        let content = &text[start..end];
                        let first_is_space = content[0] == b' ' || content[0] == b'\n';
                        let last_is_space = content[content.len() - 1] == b' '
                            || content[content.len() - 1] == b'\n';
                        let not_all_space = content.iter().any(|&b| b != b' ' && b != b'\n');

                        if first_is_space && last_is_space && not_all_space {
                            start += 1;
                            end -= 1;
                        }
                    }

                    // Normalize line endings to spaces
                    events.push(InlineEvent::Code(Range::from_usize(start, end)));
                    skip_until = end as u32;
                }
                EmitKind::EmphasisStart => {
                    events.push(InlineEvent::EmphasisStart);
                    pos = point.end;
                    skip_until = point.end;
                }
                EmitKind::EmphasisEnd => {
                    events.push(InlineEvent::EmphasisEnd);
                    skip_until = point.end;
                }
                EmitKind::StrongStart => {
                    events.push(InlineEvent::StrongStart);
                    pos = point.end;
                    skip_until = point.end;
                }
                EmitKind::StrongEnd => {
                    events.push(InlineEvent::StrongEnd);
                    skip_until = point.end;
                }
                EmitKind::StrikethroughStart => {
                    events.push(InlineEvent::StrikethroughStart);
                    pos = point.end;
                    skip_until = point.end;
                }
                EmitKind::StrikethroughEnd => {
                    events.push(InlineEvent::StrikethroughEnd);
                    skip_until = point.end;
                }
                EmitKind::Escape(ch) => {
                    events.push(InlineEvent::EscapedChar(ch));
                    skip_until = point.end;
                }
                EmitKind::HardBreak => {
                    events.push(InlineEvent::HardBreak);
                    skip_until = point.end;
                }
                EmitKind::SoftBreak => {
                    events.push(InlineEvent::SoftBreak);
                    skip_until = point.end;
                }
                EmitKind::LinkStart {
                    url_start,
                    url_end,
                    title_start,
                    title_end,
                } => {
                    events.push(InlineEvent::LinkStart {
                        url: Range::from_usize(url_start as usize, url_end as usize),
                        title: title_start
                            .map(|s| Range::from_usize(s as usize, title_end.unwrap() as usize)),
                    });
                    pos = point.end;
                    skip_until = point.end;
                }
                EmitKind::LinkStartRef { def_index } => {
                    events.push(InlineEvent::LinkStartRef { def_index });
                    pos = point.end;
                    skip_until = point.end;
                }
                EmitKind::LinkEnd => {
                    events.push(InlineEvent::LinkEnd);
                    skip_until = point.end;
                }
                EmitKind::ImageStart {
                    url_start,
                    url_end,
                    title_start,
                    title_end,
                } => {
                    events.push(InlineEvent::ImageStart {
                        url: Range::from_usize(url_start as usize, url_end as usize),
                        title: title_start
                            .map(|s| Range::from_usize(s as usize, title_end.unwrap() as usize)),
                    });
                    pos = point.end;
                    skip_until = point.end;
                }
                EmitKind::ImageStartRef { def_index } => {
                    events.push(InlineEvent::ImageStartRef { def_index });
                    pos = point.end;
                    skip_until = point.end;
                }
                EmitKind::ImageEnd => {
                    events.push(InlineEvent::ImageEnd);
                    skip_until = point.end;
                }
                EmitKind::AutolinkLiteral { end, kind } => {
                    events.push(InlineEvent::AutolinkLiteral {
                        url: Range::from_usize(point.pos as usize, end as usize),
                        kind,
                    });
                    skip_until = end;
                }
                EmitKind::AutolinkUrl {
                    content_start,
                    content_end,
                } => {
                    events.push(InlineEvent::Autolink {
                        url: Range::from_usize(content_start as usize, content_end as usize),
                        is_email: false,
                    });
                    skip_until = point.end;
                }
                EmitKind::AutolinkEmail {
                    content_start,
                    content_end,
                } => {
                    events.push(InlineEvent::Autolink {
                        url: Range::from_usize(content_start as usize, content_end as usize),
                        is_email: true,
                    });
                    skip_until = point.end;
                }
                EmitKind::HtmlRaw { end } => {
                    events.push(InlineEvent::Html(Range::from_usize(
                        point.pos as usize,
                        end as usize,
                    )));
                    skip_until = end;
                }
                EmitKind::FootnoteRef { def_index } => {
                    events.push(InlineEvent::FootnoteRef { def_index });
                    skip_until = point.end;
                }
                EmitKind::MathInline {
                    content_start,
                    content_end,
                } => {
                    events.push(InlineEvent::MathInline(Range::from_usize(
                        content_start as usize,
                        content_end as usize,
                    )));
                    skip_until = point.end;
                }
                EmitKind::MathDisplay {
                    content_start,
                    content_end,
                } => {
                    events.push(InlineEvent::MathDisplay(Range::from_usize(
                        content_start as usize,
                        content_end as usize,
                    )));
                    skip_until = point.end;
                }
            }

            pos = pos.max(point.end);
        }

        // Emit remaining text
        if pos < text_len {
            let start = pos.max(skip_until);
            if start < text_len {
                events.push(InlineEvent::Text(Range::from_usize(
                    start as usize,
                    text_len as usize,
                )));
            }
        }
    }
}

#[inline]
fn has_inline_specials(input: &[u8]) -> bool {
    #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
    {
        if let Some(result) = unsafe { simd::has_inline_specials_simd(input) } {
            return result;
        }
    }
    for &b in input {
        match b {
            b'*' | b'_' | b'`' | b'[' | b']' | b'<' | b'\\' | b'\n' | b'~' | b'$' => {
                return true;
            }
            _ => {}
        }
    }
    false
}

/// Check if text might contain autolink literal triggers.
/// Uses memchr for SIMD-accelerated scanning of rare byte patterns
/// instead of matching common letters like 'h' and 'w'.
#[inline]
fn has_autolink_candidates(input: &[u8]) -> bool {
    // Check for @ (email autolinks) — rare in normal prose
    if memchr(b'@', input).is_some() {
        return true;
    }
    // Check for :// (URL autolinks: http://, https://, ftp://) — colon is rare
    let mut pos = 0;
    while let Some(offset) = memchr(b':', &input[pos..]) {
        let idx = pos + offset;
        if idx + 2 < input.len() && input[idx + 1] == b'/' && input[idx + 2] == b'/' {
            return true;
        }
        pos = idx + 1;
    }
    // Check for www. / WWW. (www autolinks)
    pos = 0;
    while pos + 3 < input.len() {
        if let Some(offset) = memchr(b'.', &input[pos + 1..]) {
            let dot = pos + 1 + offset;
            if dot >= 3 {
                let s = dot - 3;
                if (input[s] | 0x20) == b'w'
                    && (input[s + 1] | 0x20) == b'w'
                    && (input[s + 2] | 0x20) == b'w'
                {
                    return true;
                }
            }
            pos = dot + 1;
        } else {
            break;
        }
    }
    false
}

#[inline]
fn has_inline_link_opener(input: &[u8]) -> bool {
    let mut pos = 0usize;
    while let Some(offset) = memchr(b']', &input[pos..]) {
        let idx = pos + offset;
        if idx + 1 < input.len() && input[idx + 1] == b'(' {
            return true;
        }
        pos = idx + 1;
    }
    false
}

impl Default for InlineParser {
    fn default() -> Self {
        Self::new()
    }
}

/// A resolved footnote reference.
#[derive(Debug, Clone, Copy)]
struct FootnoteRef {
    /// Start position (the `[`).
    start: u32,
    /// End position (after `]`).
    end: u32,
    /// Index into the footnote store.
    def_index: u32,
}

#[derive(Debug, Clone, Copy)]
struct EmitPoint {
    pos: u32,
    kind: EmitKind,
    end: u32,
}

#[derive(Debug, Clone, Copy)]
enum EmitKind {
    CodeSpanStart,
    CodeSpanEnd,
    CodeContent(u32), // end position
    EmphasisStart,
    EmphasisEnd,
    StrongStart,
    StrongEnd,
    StrikethroughStart,
    StrikethroughEnd,
    Escape(u8),
    HardBreak,
    SoftBreak,
    LinkStart {
        url_start: u32,
        url_end: u32,
        title_start: Option<u32>,
        title_end: Option<u32>,
    },
    LinkStartRef {
        def_index: u32,
    },
    LinkEnd,
    ImageStart {
        url_start: u32,
        url_end: u32,
        title_start: Option<u32>,
        title_end: Option<u32>,
    },
    ImageStartRef {
        def_index: u32,
    },
    ImageEnd,
    AutolinkUrl {
        content_start: u32,
        content_end: u32,
    },
    AutolinkEmail {
        content_start: u32,
        content_end: u32,
    },
    AutolinkLiteral {
        end: u32,
        kind: links::AutolinkLiteralKind,
    },
    HtmlRaw {
        end: u32,
    },
    FootnoteRef {
        def_index: u32,
    },
    MathInline {
        content_start: u32,
        content_end: u32,
    },
    MathDisplay {
        content_start: u32,
        content_end: u32,
    },
}

#[derive(Debug, Clone, Copy)]
struct HtmlSpan {
    start: u32,
    end: u32,
}

fn pos_in_spans(pos: u32, spans: &[HtmlSpan]) -> bool {
    let mut lo = 0usize;
    let mut hi = spans.len();
    while lo < hi {
        let mid = (lo + hi) / 2;
        let span = spans[mid];
        if pos < span.start {
            hi = mid;
        } else if pos >= span.end {
            lo = mid + 1;
        } else {
            return true;
        }
    }
    false
}

#[allow(clippy::too_many_arguments)]
fn find_html_spans_into(
    text: &[u8],
    code_spans: &[CodeSpan],
    autolinks: &[Autolink],
    code_ranges: &mut Vec<(usize, usize)>,
    autolink_ranges: &mut Vec<(usize, usize)>,
    spans: &mut Vec<HtmlSpan>,
) {
    spans.clear();
    let len = text.len();

    code_ranges.clear();
    code_ranges.reserve(code_spans.len().saturating_sub(code_ranges.capacity()));
    code_ranges.extend(
        code_spans
            .iter()
            .map(|cs| (cs.opener_pos as usize, cs.closer_end as usize)),
    );
    code_ranges.sort_by_key(|(s, _)| *s);

    autolink_ranges.clear();
    autolink_ranges.reserve(autolinks.len().saturating_sub(autolink_ranges.capacity()));
    autolink_ranges.extend(
        autolinks
            .iter()
            .map(|al| (al.start as usize, al.end as usize)),
    );
    autolink_ranges.sort_by_key(|(s, _)| *s);

    let mut code_idx = 0usize;
    let mut autolink_idx = 0usize;

    let mut pos = 0usize;
    while pos < len {
        if text[pos] != b'<' {
            pos += 1;
            continue;
        }
        if is_escaped(text, pos) {
            pos += 1;
            continue;
        }

        if pos_in_ranges(pos, code_ranges, &mut code_idx)
            || pos_in_ranges(pos, autolink_ranges, &mut autolink_idx)
        {
            pos += 1;
            continue;
        }

        if let Some(end) = parse_inline_html(text, pos) {
            spans.push(HtmlSpan {
                start: pos as u32,
                end: end as u32,
            });
            pos = end;
        } else {
            pos += 1;
        }
    }
}

fn filter_html_spans_in_link_destinations(spans: &mut Vec<HtmlSpan>, links: &[Link]) {
    if spans.is_empty() || links.is_empty() {
        return;
    }
    spans.retain(|span| {
        !links.iter().any(|link| {
            let dest_start = link.text_end + 1;
            span.start >= dest_start && span.start < link.end
        })
    });
}

fn filter_html_spans_in_code_spans(spans: &mut Vec<HtmlSpan>, code_spans: &[CodeSpan]) {
    if spans.is_empty() || code_spans.is_empty() {
        return;
    }
    spans.retain(|span| {
        !code_spans
            .iter()
            .any(|cs| span.start >= cs.opener_pos && span.start < cs.closer_end)
    });
}

fn pos_in_ranges(pos: usize, ranges: &[(usize, usize)], idx: &mut usize) -> bool {
    while *idx < ranges.len() && pos >= ranges[*idx].1 {
        *idx += 1;
    }
    *idx < ranges.len() && pos >= ranges[*idx].0
}

fn pos_in_ranges_u32(pos: u32, ranges: &[(u32, u32)], idx: &mut usize) -> bool {
    while *idx < ranges.len() && pos >= ranges[*idx].1 {
        *idx += 1;
    }
    *idx < ranges.len() && pos >= ranges[*idx].0
}

#[inline]
fn is_escaped(text: &[u8], pos: usize) -> bool {
    if pos == 0 {
        return false;
    }
    let mut backslashes = 0usize;
    let mut i = pos;
    while i > 0 && text[i - 1] == b'\\' {
        backslashes += 1;
        i -= 1;
    }
    backslashes % 2 == 1
}

fn parse_inline_html(text: &[u8], start: usize) -> Option<usize> {
    if text.get(start) != Some(&b'<') {
        return None;
    }
    if text.get(start + 1) == Some(&b'!') {
        if text[start..].starts_with(b"<!--") {
            return parse_html_comment(text, start);
        }
        if text[start..].starts_with(b"<![CDATA[") {
            return find_subsequence(text, start + 9, b"]]>").map(|end| end + 3);
        }
        return parse_html_declaration(text, start);
    }
    if text.get(start + 1) == Some(&b'?') {
        return find_subsequence(text, start + 2, b"?>").map(|end| end + 2);
    }
    parse_html_tag(text, start)
}

fn parse_html_comment(text: &[u8], start: usize) -> Option<usize> {
    let i = start + 4;
    if i >= text.len() {
        return None;
    }
    if text[i] == b'>' {
        return Some(i + 1);
    }
    if text[i] == b'-' && text.get(i + 1) == Some(&b'>') {
        return Some(i + 2);
    }
    find_subsequence(text, i, b"-->").map(|end| end + 3)
}

fn parse_html_declaration(text: &[u8], start: usize) -> Option<usize> {
    if text.get(start + 2).is_none_or(|b| !b.is_ascii_alphabetic()) {
        return None;
    }
    let mut i = start + 2;
    while i < text.len() && (text[i].is_ascii_alphanumeric() || text[i] == b'-') {
        i += 1;
    }
    while i < text.len() && is_html_whitespace(text[i]) {
        i += 1;
    }
    find_subsequence_byte(text, i, b'>').map(|end| end + 1)
}

fn parse_html_tag(text: &[u8], start: usize) -> Option<usize> {
    let len = text.len();
    let mut i = start + 1;
    if i >= len {
        return None;
    }

    let mut is_closing = false;
    if text[i] == b'/' {
        is_closing = true;
        i += 1;
    }

    if i >= len || !text[i].is_ascii_alphabetic() {
        return None;
    }
    i += 1;
    while i < len && (text[i].is_ascii_alphanumeric() || text[i] == b'-') {
        i += 1;
    }

    if is_closing {
        while i < len && is_html_whitespace(text[i]) {
            i += 1;
        }
        if i < len && text[i] == b'>' {
            return Some(i + 1);
        }
        return None;
    }

    loop {
        if i >= len {
            return None;
        }
        if text[i] == b'>' {
            return Some(i + 1);
        }
        if text[i] == b'/' {
            i += 1;
            return if i < len && text[i] == b'>' {
                Some(i + 1)
            } else {
                None
            };
        }
        if !is_html_whitespace(text[i]) {
            return None;
        }
        while i < len && is_html_whitespace(text[i]) {
            i += 1;
        }
        if i >= len {
            return None;
        }
        if text[i] == b'>' {
            return Some(i + 1);
        }
        if text[i] == b'/' {
            i += 1;
            return if i < len && text[i] == b'>' {
                Some(i + 1)
            } else {
                None
            };
        }

        if !is_attr_name_start(text[i]) {
            return None;
        }
        i += 1;
        while i < len && is_attr_name_char(text[i]) {
            i += 1;
        }

        let ws_start = i;
        while i < len && is_html_whitespace(text[i]) {
            i += 1;
        }

        if i < len && text[i] == b'=' {
            i += 1;
            while i < len && is_html_whitespace(text[i]) {
                i += 1;
            }
            if i >= len {
                return None;
            }
            let quote = text[i];
            if quote == b'"' || quote == b'\'' {
                i += 1;
                let value_start = i;
                while i < len && text[i] != quote {
                    i += 1;
                }
                if i >= len {
                    return None;
                }
                if i > value_start && text[i - 1] == b'\\' {
                    return None;
                }
                i += 1;
            } else {
                let mut had = false;
                while i < len && !is_html_whitespace(text[i]) {
                    let b = text[i];
                    if b == b'"' || b == b'\'' || b == b'=' || b == b'<' || b == b'>' || b == b'`' {
                        break;
                    }
                    had = true;
                    i += 1;
                }
                if !had {
                    return None;
                }
            }
        } else {
            i = ws_start;
        }
    }
}

#[inline]
fn is_html_whitespace(b: u8) -> bool {
    matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'\x0c')
}

#[inline]
fn is_attr_name_start(b: u8) -> bool {
    b.is_ascii_alphabetic() || b == b'_' || b == b':'
}

#[inline]
fn is_attr_name_char(b: u8) -> bool {
    is_attr_name_start(b) || b.is_ascii_digit() || b == b'.' || b == b'-'
}

fn find_subsequence(text: &[u8], start: usize, needle: &[u8]) -> Option<usize> {
    if needle.is_empty() || start >= text.len() || text.len() < needle.len() {
        return None;
    }
    text[start..]
        .windows(needle.len())
        .position(|w| w == needle)
        .map(|idx| start + idx)
}

fn find_subsequence_byte(text: &[u8], start: usize, needle: u8) -> Option<usize> {
    if start >= text.len() {
        return None;
    }
    text[start..]
        .iter()
        .position(|&b| b == needle)
        .map(|idx| start + idx)
}

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

    fn parse_inline(text: &str) -> Vec<InlineEvent> {
        let mut parser = InlineParser::new();
        let mut events = Vec::new();
        parser.parse(text.as_bytes(), None, true, &mut events);
        events
    }

    #[test]
    fn test_plain_text() {
        let events = parse_inline("hello world");
        assert_eq!(events.len(), 1);
        assert!(matches!(&events[0], InlineEvent::Text(_)));
    }

    #[test]
    fn test_code_span() {
        let input = "hello `code` world";
        let events = parse_inline(input);

        // Should have: Text("hello "), Code("code"), Text(" world")
        assert!(events.iter().any(|e| matches!(e, InlineEvent::Code(_))));

        for event in &events {
            if let InlineEvent::Code(range) = event {
                assert_eq!(range.slice(input.as_bytes()), b"code");
            }
        }
    }

    #[test]
    fn test_emphasis() {
        let events = parse_inline("hello *world*");

        assert!(
            events
                .iter()
                .any(|e| matches!(e, InlineEvent::EmphasisStart))
        );
        assert!(events.iter().any(|e| matches!(e, InlineEvent::EmphasisEnd)));
    }

    #[test]
    fn test_strong() {
        let events = parse_inline("hello **world**");

        assert!(events.iter().any(|e| matches!(e, InlineEvent::StrongStart)));
        assert!(events.iter().any(|e| matches!(e, InlineEvent::StrongEnd)));
    }

    #[test]
    fn test_backslash_escape() {
        let events = parse_inline("hello \\*world\\*");

        // Should have escaped characters instead of emphasis
        let escaped_count = events
            .iter()
            .filter(|e| matches!(e, InlineEvent::EscapedChar(_)))
            .count();
        assert_eq!(escaped_count, 2);
    }

    #[test]
    fn test_code_span_with_backticks() {
        let input = "`` `code` ``";
        let events = parse_inline(input);

        // Double backticks should contain single backticks
        for event in &events {
            if let InlineEvent::Code(range) = event {
                let content = std::str::from_utf8(range.slice(input.as_bytes())).unwrap();
                assert!(content.contains('`'));
            }
        }
    }

    #[test]
    fn test_emphasis_not_in_code() {
        let events = parse_inline("`*not emphasis*`");

        // Should not have emphasis events
        assert!(
            !events
                .iter()
                .any(|e| matches!(e, InlineEvent::EmphasisStart))
        );
    }

    #[test]
    fn test_image() {
        let input = "![alt](image.png)";
        let events = parse_inline(input);

        // Should have ImageStart and ImageEnd
        assert!(
            events
                .iter()
                .any(|e| matches!(e, InlineEvent::ImageStart { .. })),
            "No ImageStart found"
        );
        assert!(
            events.iter().any(|e| matches!(e, InlineEvent::ImageEnd)),
            "No ImageEnd found"
        );

        // Should NOT have a text event with just "!"
        for event in &events {
            if let InlineEvent::Text(range) = event {
                let text = range.slice(input.as_bytes());
                assert!(text != b"!", "Found standalone ! as text");
            }
        }
    }

    #[test]
    fn test_html_tag_with_newline_in_attributes() {
        let input = "<a foo=\"bar\" bam = 'baz <em>\"</em>'\n_boolean zoop:33=zoop:33 />";
        assert!(
            parse_inline_html(b"<a foo=\"bar\"\n_boolean />", 0).is_some(),
            "Expected basic multiline tag to parse"
        );
        assert!(
            parse_inline_html(b"<a foo=\"bar\" bam = 'baz <em>\"</em>'\n_boolean />", 0).is_some(),
            "Expected quoted attribute with inline tags to parse"
        );
        let end = parse_inline_html(input.as_bytes(), 0);
        assert!(
            end.is_some(),
            "Expected inline HTML parser to match the tag"
        );
        let events = parse_inline(input);
        let mut found = false;
        for event in events {
            if let InlineEvent::Html(range) = event {
                if range.start == 0 {
                    let slice = range.slice(input.as_bytes());
                    assert!(
                        slice.starts_with(b"<a "),
                        "Expected HTML span to start at <a>"
                    );
                    found = true;
                    break;
                }
            }
        }
        assert!(found, "Expected raw HTML tag to be parsed at start");
    }
}