exml 0.7.2

Pure Rust XML library based on libxml2
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
//! Provide methods and data structures for handling pattern expression.  
//!
//! This module is based on `libxml/pattern.h`, `pattern.c`, and so on in `libxml2-v2.11.8`.  
//! Please refer to original libxml2 documents also.

// Copyright of the original code is the following.
// --------
// Summary: pattern expression handling
// Description: allows to compile and test pattern expressions for nodes
//              either in a tree or based on a parser state.
//
// Copy: See Copyright for the status of this software.
//
// Author: Daniel Veillard
// --------
// pattern.c: Implementation of selectors for nodes
//
// Reference:
//   http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/
//   to some extent
//   http://www.w3.org/TR/1999/REC-xml-19991116
//
// See Copyright for the status of this software.
//
// daniel@veillard.com

use std::{os::raw::c_void, ptr::null_mut, rc::Rc};

use crate::{
    chvalid::XmlCharValid,
    parser::xml_is_letter,
    tree::{
        NodeCommon, XML_XML_NAMESPACE, XmlAttrPtr, XmlElementType, XmlGenericNodePtr, XmlNodePtr,
    },
};

const XML_STREAM_STEP_DESC: usize = 1;
const XML_STREAM_STEP_FINAL: usize = 2;
const XML_STREAM_STEP_ROOT: usize = 4;
const XML_STREAM_STEP_ATTR: usize = 8;
const XML_STREAM_STEP_NODE: usize = 16;
const XML_STREAM_STEP_IN_SET: usize = 32;

// NOTE: Those private flags (XML_STREAM_xxx) are used
//   in xmlStreamCtxt->flag. They extend the public
//   XmlPatternFlags, so be careful not to interfere with the
//   reserved values for XmlPatternFlags.
const XML_STREAM_FINAL_IS_ANY_NODE: usize = 1 << 14;
const XML_STREAM_FROM_ROOT: usize = 1 << 15;
const XML_STREAM_DESC: usize = 1 << 16;

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum XmlPatOp {
    XmlOpEnd = 0,
    XmlOpRoot,
    XmlOpElem,
    XmlOpChild,
    XmlOpAttr,
    XmlOpParent,
    XmlOpAncestor,
    XmlOpNs,
    XmlOpAll,
}

#[repr(C)]
#[derive(Debug, Clone)]
pub struct XmlStepOp {
    op: XmlPatOp,
    value: Option<Rc<str>>,
    value2: Option<Rc<str>>, /* The namespace name */
}

#[repr(C)]
#[derive(Debug, Clone)]
pub struct XmlStreamStep {
    flags: i32,            /* properties of that step */
    name: Option<Rc<str>>, /* first string value if NULL accept all */
    ns: Option<Rc<str>>,   /* second string value */
    node_type: i32,        /* type of node */
}

#[repr(C)]
#[derive(Default)]
pub struct XmlStreamComp {
    steps: Vec<XmlStreamStep>, /* the array of steps */
    flags: i32,
}

impl XmlStreamComp {
    /// Build a new compiled pattern for streaming
    ///
    /// Returns the new structure or NULL in case of error.
    #[doc(alias = "xmlNewStreamComp")]
    fn new(size: usize) -> Self {
        let mut res = Self::default();
        res.steps.reserve(size.min(4));
        res
    }

    /// Add a new step to the compiled pattern
    ///
    /// Returns -1 in case of error or the step index if successful
    #[doc(alias = "xmlStreamCompAddStep")]
    fn add_step(
        &mut self,
        name: Option<Rc<str>>,
        ns: Option<Rc<str>>,
        node_type: i32,
        flags: i32,
    ) -> i32 {
        self.steps.push(XmlStreamStep {
            flags,
            name,
            ns,
            node_type,
        });
        self.steps.len() as i32 - 1
    }
}

/// A compiled (XPath based) pattern to select nodes
#[doc(alias = "xmlPattern")]
#[repr(C)]
pub struct XmlPattern {
    data: *mut c_void,                 /* the associated template */
    next: Option<Box<XmlPattern>>,     /* next pattern if | is used */
    pattern: Option<Rc<str>>,          /* the pattern */
    flags: i32,                        /* flags */
    steps: Vec<XmlStepOp>,             /* ops for computation */
    stream: Option<Rc<XmlStreamComp>>, /* the streaming data if any */
}

impl XmlPattern {
    /// Create a new XSLT Pattern
    ///
    /// Returns the newly allocated xmlPatternPtr or NULL in case of error
    #[doc(alias = "xmlNewPattern")]
    fn new() -> Self {
        Self::default()
    }

    #[doc(alias = "XML_STREAM_XS_IDC")]
    fn is_xs_idc(&self) -> bool {
        self.flags
            & (XmlPatternFlags::XmlPatternXSSel as i32 | XmlPatternFlags::XmlPatternXSField as i32)
            != 0
    }

    #[doc(alias = "XML_STREAM_XS_IDC_SEL")]
    fn is_xs_idc_sel(&self) -> bool {
        self.flags & XmlPatternFlags::XmlPatternXSSel as i32 != 0
    }

    /// Tries to stream compile a pattern
    ///
    /// Returns -1 in case of failure and 0 in case of success.
    #[doc(alias = "xmlStreamCompile")]
    fn stream_compile(&mut self) -> i32 {
        let mut s: i32 = 0;
        let mut root: i32 = 0;
        let mut flags: i32 = 0;
        let mut prevs: i32 = -1;

        if self.steps.is_empty() {
            return -1;
        }
        // special case for .
        if self.steps.len() == 1
            && matches!(self.steps[0].op, XmlPatOp::XmlOpElem)
            && self.steps[0].value.is_none()
            && self.steps[0].value2.is_none()
        {
            let mut stream = XmlStreamComp::new(0);
            // Note that the stream will have no steps in this case.
            stream.flags |= XML_STREAM_FINAL_IS_ANY_NODE as i32;
            self.stream = Some(Rc::new(stream));
            return 0;
        }

        let mut stream = XmlStreamComp::new((self.steps.len() / 2) + 1);

        if self.flags & PAT_FROM_ROOT as i32 != 0 {
            stream.flags |= XML_STREAM_FROM_ROOT as i32;
        }

        'main: for i in 0..self.steps.len() {
            let step = &self.steps[i];
            match step.op {
                XmlPatOp::XmlOpEnd => {}
                XmlPatOp::XmlOpRoot => {
                    if i != 0 {
                        return 0;
                    }
                    root = 1;
                }
                XmlPatOp::XmlOpNs => {
                    s = stream.add_step(
                        None,
                        step.value.clone(),
                        XmlElementType::XmlElementNode as i32,
                        flags,
                    );
                    if s < 0 {
                        return 0;
                    }
                    prevs = s;
                    flags = 0;
                }
                XmlPatOp::XmlOpAttr => {
                    flags |= XML_STREAM_STEP_ATTR as i32;
                    prevs = -1;
                    s = stream.add_step(
                        step.value.clone(),
                        step.value2.clone(),
                        XmlElementType::XmlAttributeNode as i32,
                        flags,
                    );
                    flags = 0;
                    if s < 0 {
                        return 0;
                    }
                }
                XmlPatOp::XmlOpElem => 'to_break: {
                    if step.value.is_none() && step.value2.is_none() {
                        // We have a "." or "self::node()" here.
                        // Eliminate redundant self::node() tests like in "/./."
                        // or "//./"
                        // The only case we won't eliminate is "//.", i.e. if
                        // self::node() is the last node test and we had
                        // continuation somewhere beforehand.
                        if self.steps.len() == i + 1 && flags & XML_STREAM_STEP_DESC as i32 != 0 {
                            // Mark the special case where the expression resolves
                            // to any type of node.
                            if self.steps.len() == i + 1 {
                                stream.flags |= XML_STREAM_FINAL_IS_ANY_NODE as i32;
                            }
                            flags |= XML_STREAM_STEP_NODE as i32;
                            s = stream.add_step(None, None, XML_STREAM_ANY_NODE as i32, flags);
                            if s < 0 {
                                return 0;
                            }
                            flags = 0;
                            // If there was a previous step, mark it to be added to
                            // the result node-set; this is needed since only
                            // the last step will be marked as "is_final" and only
                            // "is_final" nodes are added to the resulting set.
                            if prevs != -1 {
                                stream.steps[prevs as usize].flags |= XML_STREAM_STEP_IN_SET as i32;
                                prevs = -1;
                            }
                            break 'to_break;
                        } else {
                            // Just skip this one.
                            continue 'main;
                        }
                    }
                    // An element node.
                    s = stream.add_step(
                        step.value.clone(),
                        step.value2.clone(),
                        XmlElementType::XmlElementNode as i32,
                        flags,
                    );
                    if s < 0 {
                        return 0;
                    }
                    prevs = s;
                    flags = 0;
                }
                XmlPatOp::XmlOpChild => {
                    // An element node child.
                    s = stream.add_step(
                        step.value.clone(),
                        step.value2.clone(),
                        XmlElementType::XmlElementNode as i32,
                        flags,
                    );
                    if s < 0 {
                        return 0;
                    }
                    prevs = s;
                    flags = 0;
                }
                XmlPatOp::XmlOpAll => {
                    s = stream.add_step(None, None, XmlElementType::XmlElementNode as i32, flags);
                    if s < 0 {
                        return 0;
                    }
                    prevs = s;
                    flags = 0;
                }
                XmlPatOp::XmlOpParent => {}
                XmlPatOp::XmlOpAncestor => {
                    // Skip redundant continuations.
                    if flags & XML_STREAM_STEP_DESC as i32 != 0 {
                        // break;
                    } else {
                        flags |= XML_STREAM_STEP_DESC as i32;
                        // Mark the expression as having "//".
                        if stream.flags & XML_STREAM_DESC as i32 == 0 {
                            stream.flags |= XML_STREAM_DESC as i32;
                        }
                    }
                }
            }
        }
        if root == 0 && self.flags & XML_PATTERN_NOTPATTERN == 0 {
            // If this should behave like a real pattern, we will mark
            // the first step as having "//", to be reentrant on every
            // tree level.
            if stream.flags & XML_STREAM_DESC as i32 == 0 {
                stream.flags |= XML_STREAM_DESC as i32;
            }

            if !stream.steps.is_empty() && stream.steps[0].flags & XML_STREAM_STEP_DESC as i32 == 0
            {
                stream.steps[0].flags |= XML_STREAM_STEP_DESC as i32;
            }
        }
        if stream.steps.len() as i32 <= s {
            return 0;
        }
        stream.steps[s as usize].flags |= XML_STREAM_STEP_FINAL as i32;
        if root != 0 {
            stream.steps[0].flags |= XML_STREAM_STEP_ROOT as i32;
        }
        self.stream = Some(Rc::new(stream));
        0
    }

    /// Reverse all the stack of expressions
    ///
    /// Returns 0 in case of success and -1 in case of error.
    #[doc(alias = "xmlReversePattern")]
    fn reverse_pattern(&mut self) -> i32 {
        // remove the leading // for //a or .//a
        if !self.steps.is_empty() && matches!(self.steps[0].op, XmlPatOp::XmlOpAncestor) {
            self.steps.remove(0);
        }
        self.steps.reverse();
        self.steps.push(XmlStepOp {
            op: XmlPatOp::XmlOpEnd,
            value: None,
            value2: None,
        });
        0
    }

    /// Test whether the node matches the pattern
    ///
    /// Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
    #[doc(alias = "xmlPatMatch")]
    fn pattern_match_internal(&self, mut node: XmlGenericNodePtr) -> i32 {
        let mut states: XmlStepStates = XmlStepStates { states: vec![] };
        // // may require backtrack

        let mut i = 0;
        // restart:
        loop {
            'rollback: {
                while i < self.steps.len() {
                    let mut step = &self.steps[i];
                    match step.op {
                        XmlPatOp::XmlOpEnd => {
                            return 1;
                        }
                        XmlPatOp::XmlOpRoot => {
                            if matches!(node.element_type(), XmlElementType::XmlNamespaceDecl) {
                                break 'rollback;
                            }
                            node = node.parent().unwrap();
                            if matches!(
                                node.element_type(),
                                XmlElementType::XmlDocumentNode
                                    | XmlElementType::XmlHTMLDocumentNode
                            ) {
                                i += 1;
                                continue;
                            }
                            break 'rollback;
                        }
                        XmlPatOp::XmlOpElem => {
                            if !matches!(node.element_type(), XmlElementType::XmlElementNode) {
                                break 'rollback;
                            }
                            let node = XmlNodePtr::try_from(node).unwrap();
                            let Some(value) = step.value.as_deref() else {
                                i += 1;
                                continue;
                            };
                            if Some(value) != node.name().as_deref() {
                                break 'rollback;
                            }

                            // Namespace test
                            if let Some(ns) = node.ns {
                                if ns.href.is_some()
                                    && step.value2.as_deref() != ns.href().as_deref()
                                {
                                    break 'rollback;
                                }
                            } else if step.value2.is_some() {
                                break 'rollback;
                            }
                            i += 1;
                            continue;
                        }
                        XmlPatOp::XmlOpChild => {
                            if !matches!(
                                node.element_type(),
                                XmlElementType::XmlElementNode
                                    | XmlElementType::XmlDocumentNode
                                    | XmlElementType::XmlHTMLDocumentNode
                            ) {
                                break 'rollback;
                            }

                            let mut lst = node.children();

                            if let Some(value) = step.value.as_deref() {
                                while let Some(now) = lst {
                                    if matches!(now.element_type(), XmlElementType::XmlElementNode)
                                    {
                                        let now = XmlNodePtr::try_from(now).unwrap();
                                        if Some(value) == now.name().as_deref() {
                                            break;
                                        }
                                    }
                                    lst = now.next();
                                }
                                if lst.is_some() {
                                    i += 1;
                                    continue;
                                }
                            }
                            break 'rollback;
                        }
                        XmlPatOp::XmlOpAttr => {
                            if !matches!(node.element_type(), XmlElementType::XmlAttributeNode) {
                                break 'rollback;
                            }
                            let node = XmlAttrPtr::try_from(node).unwrap();
                            if let Some(value) = step.value.as_deref() {
                                if Some(value) != node.name().as_deref() {
                                    break 'rollback;
                                }
                            }
                            // Namespace test
                            if let Some(ns) = node.ns {
                                if step.value2.is_some()
                                    && step.value2.as_deref() != ns.href().as_deref()
                                {
                                    break 'rollback;
                                }
                            } else if step.value2.is_some() {
                                break 'rollback;
                            }
                            i += 1;
                            continue;
                        }
                        XmlPatOp::XmlOpParent => {
                            if matches!(
                                node.element_type(),
                                XmlElementType::XmlDocumentNode
                                    | XmlElementType::XmlHTMLDocumentNode
                                    | XmlElementType::XmlNamespaceDecl
                            ) {
                                break 'rollback;
                            }
                            let Some(parent) = node.parent() else {
                                break 'rollback;
                            };
                            node = parent;
                            let Some(value) = step.value.as_deref() else {
                                i += 1;
                                continue;
                            };
                            // Is is correct ??????
                            let node = XmlNodePtr::try_from(node).unwrap();
                            if Some(value) != node.name().as_deref() {
                                break 'rollback;
                            }
                            // Namespace test
                            if let Some(ns) = node.ns {
                                if ns.href.is_some()
                                    && step.value2.as_deref() != ns.href().as_deref()
                                {
                                    break 'rollback;
                                }
                            } else if step.value2.is_some() {
                                break 'rollback;
                            }
                            i += 1;
                            continue;
                        }
                        XmlPatOp::XmlOpAncestor => {
                            // TODO: implement coalescing of ANCESTOR/NODE ops
                            if step.value.is_none() {
                                i += 1;
                                step = &self.steps[i];
                                if matches!(step.op, XmlPatOp::XmlOpRoot) {
                                    return 1;
                                }
                                if !matches!(step.op, XmlPatOp::XmlOpElem) {
                                    break 'rollback;
                                }
                                if step.value.is_none() {
                                    return -1;
                                }
                            }
                            if matches!(
                                node.element_type(),
                                XmlElementType::XmlDocumentNode
                                    | XmlElementType::XmlHTMLDocumentNode
                                    | XmlElementType::XmlNamespaceDecl
                            ) {
                                break 'rollback;
                            }
                            let mut cur = node.parent();
                            while let Some(cur_node) = cur {
                                if matches!(cur_node.element_type(), XmlElementType::XmlElementNode)
                                {
                                    let node = XmlNodePtr::try_from(cur_node).unwrap();

                                    if step.value.as_deref() == node.name().as_deref() {
                                        // Namespace test
                                        if let Some(ns) = node.ns {
                                            if ns.href.is_some()
                                                && step.value2.as_deref() == ns.href().as_deref()
                                            {
                                                break;
                                            }
                                        } else if step.value2.is_none() {
                                            break;
                                        }
                                    }
                                }
                                cur = cur_node.parent();
                            }
                            let Some(cur) = cur else {
                                break 'rollback;
                            };
                            node = cur;
                            // prepare a potential rollback from here
                            // for ancestors of that node.
                            if matches!(step.op, XmlPatOp::XmlOpAncestor) {
                                states.push_state(i as i32, node);
                            } else {
                                states.push_state(i as i32 - 1, node);
                            }
                            i += 1;
                            continue;
                        }
                        XmlPatOp::XmlOpNs => {
                            if !matches!(node.element_type(), XmlElementType::XmlElementNode) {
                                break 'rollback;
                            }
                            let node = XmlNodePtr::try_from(node).unwrap();
                            if let Some(ns) = node.ns {
                                if ns.href.is_some()
                                    && step.value.as_deref() != ns.href().as_deref()
                                {
                                    break 'rollback;
                                }
                            } else if step.value.is_some() {
                                break 'rollback;
                            }
                        }
                        XmlPatOp::XmlOpAll => {
                            if !matches!(node.element_type(), XmlElementType::XmlElementNode) {
                                break 'rollback;
                            }
                        }
                    }
                    i += 1;
                }
            }
            // rollback:
            // got an error try to rollback
            let Some(state) = states.states.pop() else {
                return 0;
            };
            i = state.step as usize;
            node = state.node;
            // goto restart;
        }
    }

    /// Test whether the node matches the pattern
    ///
    /// Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
    #[doc(alias = "xmlPatternMatch")]
    pub fn pattern_match(&self, node: XmlGenericNodePtr) -> i32 {
        let mut ret: i32 = 0;

        let mut now = Some(self);
        while let Some(comp) = now {
            ret = comp.pattern_match_internal(node);
            if ret != 0 {
                return ret;
            }
            now = comp.next.as_deref();
        }
        ret
    }

    /// Check if the pattern is streamable i.e. xmlPatternGetStreamCtxt() should work.
    ///
    /// Returns 1 if streamable, 0 if not and -1 in case of error.
    #[doc(alias = "xmlPatternStreamable")]
    pub fn is_streamable(&self) -> i32 {
        let mut now = Some(self);
        while let Some(comp) = now {
            if comp.stream.is_none() {
                return 0;
            }
            now = comp.next.as_deref();
        }
        1
    }

    /// Check the maximum depth reachable by a pattern
    ///
    /// Returns -2 if no limit (using //), otherwise the depth, and -1 in case of error
    #[doc(alias = "xmlPatternMaxDepth")]
    pub fn max_depth(&self) -> i32 {
        let mut ret: i32 = 0;

        let mut now = Some(self);
        while let Some(comp) = now {
            let Some(stream) = comp.stream.as_deref() else {
                return -1;
            };

            for i in 0..stream.steps.len() {
                if stream.steps[i].flags & XML_STREAM_STEP_DESC as i32 != 0 {
                    return -2;
                }
            }
            if stream.steps.len() as i32 > ret {
                ret = stream.steps.len() as i32;
            }
            now = comp.next.as_deref();
        }
        ret
    }

    /// Check the minimum depth reachable by a pattern, 0 mean the / or . are part of the set.
    ///
    /// Returns -1 in case of error otherwise the depth,
    #[doc(alias = "xmlPatternMinDepth")]
    pub fn min_depth(&self) -> i32 {
        let mut ret: i32 = 12345678;
        let mut now = Some(self);
        while let Some(comp) = now {
            let Some(stream) = comp.stream.as_deref() else {
                return -1;
            };
            if stream.steps.len() < ret as usize {
                ret = stream.steps.len() as i32;
            }
            if ret == 0 {
                return 0;
            }
            now = comp.next.as_deref();
        }
        ret
    }

    /// Check if the pattern must be looked at from the root.
    ///
    /// Returns 1 if true, 0 if false and -1 in case of error
    #[doc(alias = "xmlPatternFromRoot")]
    pub fn is_from_root(&self) -> i32 {
        let mut now = Some(self);
        while let Some(comp) = now {
            if comp.stream.is_none() {
                return -1;
            }
            if comp.flags & PAT_FROM_ROOT as i32 != 0 {
                return 1;
            }
            now = comp.next.as_deref();
        }
        0
    }

    /// Get a streaming context for that pattern
    /// Use xmlFreeStreamCtxt to free the context.
    ///
    /// Returns a pointer to the context or NULL in case of failure
    #[doc(alias = "xmlPatternGetStreamCtxt")]
    pub fn get_stream_context(&self) -> Option<Box<XmlStreamCtxt>> {
        self.stream.as_ref()?;

        let mut ret: Option<Box<XmlStreamCtxt>> = None;
        let mut now = Some(self);
        while let Some(comp) = now {
            let mut cur = XmlStreamCtxt::new(comp.stream.clone()?);
            cur.flags = comp.flags;
            if let Some(ret) = ret.as_deref_mut() {
                cur.next = ret.next.take();
                ret.next = Some(Box::new(cur));
            } else {
                ret = Some(Box::new(cur))
            }
            now = comp.next.as_deref();
        }
        ret
    }
}

impl Default for XmlPattern {
    fn default() -> Self {
        Self {
            data: null_mut(),
            next: None,
            pattern: None,
            flags: 0,
            steps: vec![],
            stream: None,
        }
    }
}

unsafe impl Send for XmlPattern {}
unsafe impl Sync for XmlPattern {}

// XML_STREAM_ANY_NODE is used for comparison against
// xmlElementType enums, to indicate a node of any type.
const XML_STREAM_ANY_NODE: usize = 100;

/// This is the set of options affecting the behaviour of pattern matching with this module
#[doc(alias = "xmlPatternFlags")]
#[repr(C)]
pub enum XmlPatternFlags {
    XmlPatternDefault = 0,      /* simple pattern match */
    XmlPatternXPath = 1 << 0,   /* standard XPath pattern */
    XmlPatternXSSel = 1 << 1,   /* XPath subset for schema selector */
    XmlPatternXSField = 1 << 2, /* XPath subset for schema field */
}

#[doc(alias = "xmlPatParserContext")]
#[repr(C)]
pub struct XmlPatParserContext {
    cur: usize,                                        /* the current char being parsed */
    base: Box<str>,                                    /* the full expression */
    error: i32,                                        /* error code */
    comp: Option<Box<XmlPattern>>,                     /* the result */
    elem: Option<XmlNodePtr>,                          /* the current node if any */
    namespaces: Option<Vec<(String, Option<String>)>>, /* the namespaces definitions */
}

impl XmlPatParserContext {
    /// Create a new XML pattern parser context
    ///
    /// Returns the newly allocated xmlPatParserContextPtr or NULL in case of error
    #[doc(alias = "xmlNewPatParserContext")]
    fn new(pattern: &str, namespaces: Option<Vec<(String, Option<String>)>>) -> Self {
        XmlPatParserContext {
            cur: 0,
            base: pattern.to_owned().into_boxed_str(),
            namespaces,
            ..Default::default()
        }
    }

    fn current_str(&self) -> &str {
        &self.base[self.cur..]
    }

    fn current_byte(&self) -> Option<u8> {
        self.nth_byte(0)
    }

    fn nth_byte(&self, index: usize) -> Option<u8> {
        self.current_str().as_bytes().get(index).copied()
    }

    fn next(&mut self) -> Option<char> {
        let c = self.current_str().chars().next()?;
        self.cur += c.len_utf8();
        Some(c)
    }

    fn skip_blanks(&mut self) {
        let pat = self.current_str();
        let trimmed = pat.trim_start_matches(|c: char| c.is_xml_blank_char());
        let diff = pat.len() - trimmed.len();
        self.cur += diff;
    }

    fn peek_prev(&self, diff: usize) -> Option<u8> {
        let prev = self.cur.checked_sub(diff)?;
        Some(self.current_str().as_bytes()[prev])
    }

    /// Compile the Path Pattern and generates a precompiled
    /// form suitable for fast matching.
    ///
    /// ```text
    /// [5]    Path    ::=    ('.//')? ( Step '/' )* ( Step | '@' NameTest )
    /// ```
    #[doc(alias = "xmlCompileIDCXPathPath")]
    fn compile_idc_xpath_path(&mut self) {
        self.skip_blanks();
        if self.current_byte() == Some(b'/') {
            self.error = 1;
            return;
        }
        self.comp.as_mut().unwrap().flags |= PAT_FROM_CUR as i32;

        if self.current_byte() == Some(b'.') {
            // "." - "self::node()"
            self.next();
            self.skip_blanks();
            if self.current_byte().is_none() {
                // Selection of the context node.
                if self.pattern_add(XmlPatOp::XmlOpElem, None, None) != 0 {
                    self.error = 1;
                };
                return;
            }
            if self.current_byte() != Some(b'/') {
                // TODO: A more meaningful error message.
                self.error = 1;
                return;
            }
            // "./" - "self::node()/"
            self.next();
            self.skip_blanks();
            if self.current_byte() == Some(b'/') {
                if self.peek_prev(1).is_some_and(|c| c.is_xml_blank_char()) {
                    // Disallow "./ /"
                    self.error = 1;
                    return;
                }
                // ".//" - "self:node()/descendant-or-self::node()/"
                if self.pattern_add(XmlPatOp::XmlOpAncestor, None, None) != 0 {
                    self.error = 1;
                    return;
                };
                self.next();
                self.skip_blanks();
            }
            if self.current_byte().is_none() {
                self.error = 1;
                return;
            }
        }
        // Process steps.
        'b: while {
            self.compile_step_pattern();
            if self.error != 0 {
                self.error = 1;
                return;
            }
            self.skip_blanks();
            if self.current_byte() != Some(b'/') {
                break 'b;
            }
            if self.pattern_add(XmlPatOp::XmlOpParent, None, None) != 0 {
                self.error = 1;
                return;
            };
            self.next();
            self.skip_blanks();
            if self.current_byte() == Some(b'/') {
                // Disallow subsequent '//'.
                self.error = 1;
                return;
            }
            if self.current_byte().is_none() {
                self.error = 1;
                return;
            }

            self.current_byte().is_some()
        } {}

        if self.current_byte().is_some() {
            self.error = 1;
        }
    }

    /// Compile the Path Pattern and generates a precompiled
    /// form suitable for fast matching.
    ///
    /// ```text
    /// [5]    Path    ::=    ('.//')? ( Step '/' )* ( Step | '@' NameTest )
    /// ```
    #[doc(alias = "xmlCompilePathPattern")]
    fn compile_path_pattern(&mut self) {
        self.skip_blanks();
        if self.current_byte() == Some(b'/') {
            self.comp.as_mut().unwrap().flags |= PAT_FROM_ROOT as i32;
        } else if self.current_byte() == Some(b'.')
            || self.comp.as_mut().unwrap().flags & XML_PATTERN_NOTPATTERN != 0
        {
            self.comp.as_mut().unwrap().flags |= PAT_FROM_CUR as i32;
        }

        if self.current_byte() == Some(b'/') && self.nth_byte(1) == Some(b'/') {
            if self.pattern_add(XmlPatOp::XmlOpAncestor, None, None) != 0 {
                return;
            };
            self.next();
            self.next();
        } else if self.current_byte() == Some(b'.')
            && self.nth_byte(1) == Some(b'/')
            && self.nth_byte(2) == Some(b'/')
        {
            if self.pattern_add(XmlPatOp::XmlOpAncestor, None, None) != 0 {
                return;
            };
            self.next();
            self.next();
            self.next();
            // Check for incompleteness.
            self.skip_blanks();
            if self.current_byte().is_none() {
                self.error = 1;
                return;
            }
        }
        if self.current_byte() == Some(b'@') {
            self.next();
            self.compile_attribute_test();
            self.skip_blanks();
            // TODO: check for incompleteness
            if self.current_byte().is_some() {
                self.compile_step_pattern();
                if self.error != 0 {
                    return;
                }
            }
        } else {
            if self.current_byte() == Some(b'/') {
                if self.pattern_add(XmlPatOp::XmlOpRoot, None, None) != 0 {
                    return;
                };
                self.next();
                // Check for incompleteness.
                self.skip_blanks();
                if self.current_byte().is_none() {
                    self.error = 1;
                    return;
                }
            }
            self.compile_step_pattern();
            if self.error != 0 {
                return;
            }
            self.skip_blanks();
            while self.current_byte() == Some(b'/') {
                if self.nth_byte(1) == Some(b'/') {
                    if self.pattern_add(XmlPatOp::XmlOpAncestor, None, None) != 0 {
                        return;
                    };
                    self.next();
                    self.next();
                    self.skip_blanks();
                    self.compile_step_pattern();
                    if self.error != 0 {
                        return;
                    }
                } else {
                    if self.pattern_add(XmlPatOp::XmlOpParent, None, None) != 0 {
                        return;
                    };
                    self.next();
                    self.skip_blanks();
                    if self.current_byte().is_none() {
                        self.error = 1;
                        return;
                    }
                    self.compile_step_pattern();
                    if self.error != 0 {
                        return;
                    }
                }
            }
        }
        if self.current_byte().is_some() {
            self.error = 1;
        }
    }

    /// Compile the Step Pattern and generates a precompiled
    /// form suitable for fast matching.
    ///
    /// ```text
    /// [3]    Step    ::=    '.' | NameTest
    /// [4]    NameTest    ::=    QName | '*' | NCName ':' '*'
    /// ```
    #[doc(alias = "xmlCompileStepPattern")]
    fn compile_step_pattern(&mut self) {
        let mut has_blanks: i32 = 0;

        self.skip_blanks();
        if self.current_byte() == Some(b'.') {
            // Context node.
            self.next();
            self.pattern_add(XmlPatOp::XmlOpElem, None, None);
            return;
        }
        if self.current_byte() == Some(b'@') {
            // Attribute test.
            if self.comp.as_ref().unwrap().is_xs_idc_sel() {
                self.error = 1;
                return;
            }
            self.next();
            self.compile_attribute_test();
            return;
        }
        let Some(name) = self.scan_ncname() else {
            if self.current_byte() == Some(b'*') {
                self.next();
                self.pattern_add(XmlPatOp::XmlOpAll, None, None);
            } else {
                self.error = 1;
            }
            return;
        };
        if self.current_byte().is_some_and(|b| b.is_xml_blank_char()) {
            has_blanks = 1;
            self.skip_blanks();
        }
        if self.current_byte() == Some(b':') {
            self.next();
            if self.current_byte() != Some(b':') {
                let prefix = name;

                if has_blanks != 0 || self.current_byte().is_some_and(|b| b.is_xml_blank_char()) {
                    self.error = 1;
                    return;
                }
                // This is a namespace is_match
                let token = self.scan_name();
                let mut url = None;
                if prefix == "xml" {
                    url = Some(XML_XML_NAMESPACE.to_owned());
                } else if let Some(namespaces) = self.namespaces.as_deref() {
                    if let Some((href, _)) = namespaces
                        .iter()
                        .find(|(_, pref)| pref.as_deref() == Some(&prefix))
                    {
                        url = Some(href.to_owned());
                    } else {
                        self.error = 1;
                        return;
                    }
                }
                if let Some(token) = token {
                    self.pattern_add(XmlPatOp::XmlOpElem, Some(&token), url.as_deref());
                } else if self.current_byte() == Some(b'*') {
                    self.next();
                    if self.pattern_add(XmlPatOp::XmlOpNs, url.as_deref(), None) != 0 {
                        return;
                    };
                } else {
                    self.error = 1;
                    return;
                }
            } else {
                self.next();
                if name == "child" {
                    let Some(name) = self.scan_name() else {
                        if self.current_byte() == Some(b'*') {
                            self.next();
                            self.pattern_add(XmlPatOp::XmlOpAll, None, None);
                        } else {
                            self.error = 1;
                        }
                        return;
                    };
                    if self.current_byte() == Some(b':') {
                        let prefix = name;

                        self.next();
                        if self.current_byte().is_some_and(|b| b.is_xml_blank_char()) {
                            self.error = 1;
                            return;
                        }
                        // This is a namespace is_match
                        let token = self.scan_name();
                        let mut url = None;
                        if prefix == "xml" {
                            url = Some(XML_XML_NAMESPACE.to_owned());
                        } else if let Some(namespaces) = self.namespaces.as_deref() {
                            if let Some((href, _)) = namespaces
                                .iter()
                                .find(|(_, pref)| pref.as_deref() == Some(&prefix))
                            {
                                url = Some(href.to_owned());
                            } else {
                                self.error = 1;
                                return;
                            }
                        }
                        if let Some(token) = token {
                            self.pattern_add(XmlPatOp::XmlOpChild, Some(&token), url.as_deref());
                        } else if self.current_byte() == Some(b'*') {
                            self.next();
                            self.pattern_add(XmlPatOp::XmlOpNs, url.as_deref(), None);
                        } else {
                            self.error = 1;
                        }
                    } else {
                        self.pattern_add(XmlPatOp::XmlOpChild, Some(&name), None);
                    }
                } else if name == "attribute" {
                    if self.comp.as_ref().unwrap().is_xs_idc_sel() {
                        self.error = 1;
                        return;
                    }
                    self.compile_attribute_test();
                    return;
                } else {
                    self.error = 1;
                    return;
                }
            }
        } else if self.current_byte() == Some(b'*') {
            self.error = 1;
        } else {
            self.pattern_add(XmlPatOp::XmlOpElem, Some(&name), None);
        }
    }

    /// Compile an attribute test.
    #[doc(alias = "xmlCompileAttributeTest")]
    fn compile_attribute_test(&mut self) {
        self.skip_blanks();
        let name = self.scan_ncname();
        let Some(name) = name else {
            if self.current_byte() == Some(b'*') {
                if self.pattern_add(XmlPatOp::XmlOpAttr, None, None) != 0 {
                    return;
                };
                self.next();
            } else {
                self.error = 1;
            }
            return;
        };
        if self.current_byte() == Some(b':') {
            let prefix = name;

            self.next();

            if self.current_byte().is_some_and(|b| b.is_xml_blank_char()) {
                self.error = 1;
                return;
            }
            // This is a namespace is_match
            let token = self.scan_name();
            let mut url = None;
            if prefix == "xml" {
                url = Some(XML_XML_NAMESPACE.to_owned());
            } else if let Some(namespaces) = self.namespaces.as_deref() {
                if let Some((href, _)) = namespaces
                    .iter()
                    .find(|(_, pref)| pref.as_deref() == Some(&prefix))
                {
                    url = Some(href.to_owned());
                } else {
                    self.error = 1;
                    return;
                }
            }
            if let Some(token) = token {
                self.pattern_add(XmlPatOp::XmlOpAttr, Some(&token), url.as_deref());
            } else if self.current_byte() == Some(b'*') {
                self.next();
                if self.pattern_add(XmlPatOp::XmlOpAttr, None, url.as_deref()) != 0 {
                    return;
                };
            } else {
                self.error = 1;
                return;
            }
        } else if self.pattern_add(XmlPatOp::XmlOpAttr, Some(&name), None) != 0 {
            return;
        }
    }

    /// ```text
    /// [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender
    ///
    /// [5] Name ::= (Letter | '_' | ':') (NameChar)*
    ///
    /// [6] Names ::= Name (S Name)*
    /// ```
    ///
    /// Returns the Name parsed or NULL
    #[doc(alias = "xmlPatScanName")]
    fn scan_name(&mut self) -> Option<String> {
        self.skip_blanks();

        let cur = self.current_str();
        if !cur.starts_with(|c: char| xml_is_letter(c as u32) || c == '_' || c == ':') {
            return None;
        }
        let trimmed = cur.trim_start_matches(|c: char| {
            xml_is_letter(c as u32)
                || c.is_xml_digit()
                || c == '.'
                || c == '-'
                || c == '_'
                || c.is_xml_combining()
                || c.is_xml_extender()
        });
        let diff = cur.len() - trimmed.len();
        let ret = cur[..diff].to_owned();
        self.cur += diff;
        Some(ret)
    }

    /// Parses a non qualified name
    ///
    /// Returns the Name parsed or NULL
    #[doc(alias = "xmlPatScanNCName")]
    fn scan_ncname(&mut self) -> Option<String> {
        self.skip_blanks();

        let cur = self.current_str();
        if !cur.starts_with(|c: char| xml_is_letter(c as u32) || c == '_') {
            return None;
        }
        let trimmed = cur.trim_start_matches(|c: char| {
            xml_is_letter(c as u32)
                || c.is_xml_digit()
                || c == '.'
                || c == '-'
                || c == '_'
                || c.is_xml_combining()
                || c.is_xml_extender()
        });
        let diff = cur.len() - trimmed.len();
        let ret = cur[..diff].to_owned();
        self.cur += diff;
        Some(ret)
    }

    /// Add a step to an XSLT Compiled Match
    ///
    /// Returns -1 in case of failure, 0 otherwise.
    #[doc(alias = "xmlPatternAdd")]
    fn pattern_add(&mut self, op: XmlPatOp, value: Option<&str>, value2: Option<&str>) -> i32 {
        self.comp.as_mut().unwrap().steps.push(XmlStepOp {
            op,
            value: value.map(Rc::from),
            value2: value2.map(Rc::from),
        });
        0
    }
}

impl Default for XmlPatParserContext {
    fn default() -> Self {
        Self {
            cur: 0,
            base: "".to_owned().into_boxed_str(),
            error: 0,
            comp: None,
            elem: None,
            namespaces: None,
        }
    }
}

const PAT_FROM_ROOT: usize = 1 << 8;
const PAT_FROM_CUR: usize = 1 << 9;

const XML_PATTERN_NOTPATTERN: i32 = XmlPatternFlags::XmlPatternXPath as i32
    | XmlPatternFlags::XmlPatternXSSel as i32
    | XmlPatternFlags::XmlPatternXSField as i32;

/// Compile a pattern.
///
/// Returns the compiled form of the pattern or NULL in case of error
#[doc(alias = "xmlPatterncompile")]
pub fn xml_pattern_compile(
    pattern: &str,
    flags: i32,
    namespaces: Option<Vec<(String, Option<String>)>>,
) -> Option<Box<XmlPattern>> {
    let mut ret: Option<Box<XmlPattern>> = None;
    let mut typ: i32 = 0;
    let mut streamable: i32 = 1;

    for pattern in pattern.split('|') {
        let mut ctxt = XmlPatParserContext::new(pattern, namespaces.clone());
        let mut cur = XmlPattern::new();
        cur.flags = flags;
        let is_xml_stream_xs_idc = cur.is_xs_idc();
        ctxt.comp = Some(Box::new(cur));

        if is_xml_stream_xs_idc {
            ctxt.compile_idc_xpath_path();
        } else {
            ctxt.compile_path_pattern();
        }
        if ctxt.error != 0 {
            return None;
        }

        let mut cur = ctxt.comp.unwrap();

        if streamable != 0 {
            if typ == 0 {
                typ = cur.flags & (PAT_FROM_ROOT | PAT_FROM_CUR) as i32;
            } else if typ == PAT_FROM_ROOT as i32 {
                if cur.flags & PAT_FROM_CUR as i32 != 0 {
                    streamable = 0;
                }
            } else if typ == PAT_FROM_CUR as i32 && cur.flags & PAT_FROM_ROOT as i32 != 0 {
                streamable = 0;
            }
        }
        if streamable != 0 {
            cur.stream_compile();
        }
        if cur.reverse_pattern() < 0 {
            return None;
        }
        if let Some(ret) = ret.as_deref_mut() {
            cur.next = ret.next.take();
            ret.next = Some(cur);
        } else {
            ret = Some(cur);
        }
    }
    if streamable == 0 {
        let mut now = ret.as_deref_mut();
        while let Some(cur) = now {
            cur.stream.take();
            now = cur.next.as_deref_mut();
        }
    }

    ret
}

struct XmlStepState {
    step: i32,
    node: XmlGenericNodePtr,
}

struct XmlStepStates {
    states: Vec<XmlStepState>,
}

impl XmlStepStates {
    fn push_state(&mut self, step: i32, node: XmlGenericNodePtr) -> i32 {
        self.states.push(XmlStepState { step, node });
        0
    }
}

// streaming interfaces
#[doc(alias = "xmlStreamCtxt")]
#[repr(C)]
pub struct XmlStreamCtxt {
    // link to next sub pattern if |
    next: Option<Box<XmlStreamCtxt>>,
    // the compiled stream
    comp: Rc<XmlStreamComp>,
    // how deep are we ?
    level: i32,
    // the array of step indexes
    // (index, level)
    states: Vec<(i32, i32)>,
    // validation options
    flags: i32,
    block_level: i32,
}

impl XmlStreamCtxt {
    /// Build a new stream context
    ///
    /// Returns the new structure or NULL in case of error.
    #[doc(alias = "xmlNewStreamCtxt")]
    fn new(stream: Rc<XmlStreamComp>) -> Self {
        Self {
            next: None,
            comp: stream,
            level: 0,
            states: Vec::with_capacity(4),
            flags: 0,
            block_level: -1,
        }
    }

    #[doc(alias = "XML_STREAM_XS_IDC")]
    fn is_xs_idc(&self) -> bool {
        self.flags
            & (XmlPatternFlags::XmlPatternXSSel as i32 | XmlPatternFlags::XmlPatternXSField as i32)
            != 0
    }

    /// Add a new state to the stream context
    ///
    /// Returns -1 in case of error or the state index if successful
    #[doc(alias = "xmlStreamCtxtAddState")]
    fn add_state(&mut self, idx: i32, level: i32) -> i32 {
        for i in 0..self.states.len() {
            if self.states[i].0 < 0 {
                self.states[i] = (idx, level);
                return i as i32;
            }
        }
        self.states.push((idx, level));
        self.states.len() as i32 - 1
    }

    /// Push new data onto the stream. NOTE: if the call xmlPatterncompile()
    /// indicated a dictionary, then strings for name and ns will be expected
    /// to come from the dictionary.
    /// Both @name and @ns being NULL means the / i.e. the root of the document.
    /// This can also act as a reset.
    ///
    /// Returns: -1 in case of error, 1 if the current state in the stream is a is_match and 0 otherwise.
    #[doc(alias = "xmlStreamPushInternal")]
    fn push_internal(&mut self, name: Option<&str>, ns: Option<&str>, node_type: i32) -> i32 {
        let mut ret: i32 = 0;
        let mut err: i32 = 0;
        let mut is_final: i32 = 0;
        let mut step_nr: i32;

        let mut now = Some(self);
        'stream: while let Some(stream) = now {
            'stream_next: {
                let comp = stream.comp.clone();

                if node_type == XmlElementType::XmlElementNode as i32
                    && name.is_none()
                    && ns.is_none()
                {
                    // We have a document node here (or a reset).
                    stream.states.clear();
                    stream.level = 0;
                    stream.block_level = -1;
                    if comp.flags & XML_STREAM_FROM_ROOT as i32 != 0 {
                        if comp.steps.is_empty() {
                            // TODO: We have a "/." here?
                            ret = 1;
                        } else if comp.steps.len() == 1
                            && comp.steps[0].node_type == XML_STREAM_ANY_NODE as i32
                            && comp.steps[0].flags & XML_STREAM_STEP_DESC as i32 != 0
                        {
                            // In the case of "//." the document node will is_match as well.
                            ret = 1;
                        } else if comp.steps[0].flags & XML_STREAM_STEP_ROOT as i32 != 0 {
                            // TODO: Do we need this ?
                            let tmp = stream.add_state(0, 0);
                            if tmp < 0 {
                                err += 1;
                            }
                        }
                    }
                    now = stream.next.as_deref_mut();
                    continue 'stream;
                }

                // Fast check for ".".
                if comp.steps.is_empty() {
                    // / and . are handled at the XPath node set creation
                    // level by checking min depth
                    if stream.flags & XmlPatternFlags::XmlPatternXPath as i32 != 0 {
                        now = stream.next.as_deref_mut();
                        continue 'stream; /* while */
                    }
                    // For non-pattern like evaluation like XML Schema IDCs
                    // or traditional XPath expressions, this will is_match if
                    // we are at the first level only, otherwise on every level.
                    if node_type != XmlElementType::XmlAttributeNode as i32
                        && (stream.flags & XML_PATTERN_NOTPATTERN == 0 || stream.level == 0)
                    {
                        ret = 1;
                    }
                    stream.level += 1;
                    break 'stream_next;
                }
                if stream.block_level != -1 {
                    // Skip blocked expressions.
                    stream.level += 1;
                    break 'stream_next;
                }

                if node_type != XmlElementType::XmlElementNode as i32
                    && node_type != XmlElementType::XmlAttributeNode as i32
                    && comp.flags & XML_STREAM_FINAL_IS_ANY_NODE as i32 == 0
                {
                    // No need to process nodes of other types if we don't
                    // resolve to those types.
                    // TODO: Do we need to block the context here?
                    stream.level += 1;
                    break 'stream_next;
                }

                // Check evolution of existing states
                let mut i = 0;
                let m = stream.states.len();
                while i < m {
                    if comp.flags & XML_STREAM_DESC as i32 == 0 {
                        // If there is no "//", then only the last
                        // added state is of interest.
                        step_nr = stream.states.last().unwrap().0;
                        // TODO: Security check, should not happen, remove it.
                        if stream.states.last().unwrap().1 < stream.level {
                            return -1;
                        }
                        // desc = 0;
                        // loop-stopper
                        i = m;
                    } else {
                        // If there are "//", then we need to process every "//"
                        // occurring in the states, plus any other state for this level.
                        step_nr = stream.states[i].0;

                        // TODO: should not happen anymore: dead states
                        if step_nr < 0 {
                            i += 1;
                            continue;
                        }

                        let tmp = stream.states[i].1;

                        // skip new states just added
                        if tmp > stream.level {
                            i += 1;
                            continue;
                        }

                        // skip states at ancestor levels, except if "//"
                        let desc = comp.steps[step_nr as usize].flags & XML_STREAM_STEP_DESC as i32;
                        if tmp < stream.level && desc == 0 {
                            i += 1;
                            continue;
                        }
                    }
                    // Check for correct node-type.
                    let step = &comp.steps[step_nr as usize];
                    if step.node_type != node_type {
                        if step.node_type == XmlElementType::XmlAttributeNode as i32 {
                            // Block this expression for deeper evaluation.
                            if comp.flags & XML_STREAM_DESC as i32 == 0 {
                                stream.block_level = stream.level + 1;
                            }
                            i += 1;
                            continue;
                        } else if step.node_type != XML_STREAM_ANY_NODE as i32 {
                            i += 1;
                            continue;
                        }
                    }
                    // Compare local/namespace-name.
                    let mut is_match = false;
                    if step.node_type == XML_STREAM_ANY_NODE as i32 {
                        is_match = true;
                    } else if step.name.is_none() {
                        if step.ns.is_none() {
                            // This lets through all elements/attributes.
                            is_match = true;
                        } else if ns.is_some() {
                            is_match = step.ns.as_deref() == ns;
                        }
                    } else if step.ns.is_none() == ns.is_none()
                        && name.is_some()
                        && step.name.as_deref() == name
                        && step.ns.as_deref() == ns
                    {
                        is_match = true;
                    }
                    // #if 0
                    // /*
                    // * TODO: Pointer comparison won't work, since not guaranteed that the given
                    // *  values are in the same dict; especially if it's the namespace name,
                    // *  normally coming from ns->href. We need a namespace dict mechanism !
                    // */
                    //  } else if ((*comp).dict) {
                    //      if (step.name.is_null()) {
                    //          if (step.ns.is_null())
                    //      	is_match = 1;
                    //          else
                    //      	is_match = (step.ns == ns);
                    //      } else {
                    //          is_match = ((step.name == name) && (step.ns == ns));
                    //      }
                    // #endif /* if 0 ------------------------------------------------------- */
                    if is_match {
                        is_final = step.flags & XML_STREAM_STEP_FINAL as i32;
                        if is_final != 0 {
                            ret = 1;
                        } else {
                            stream.add_state(step_nr + 1, stream.level + 1);
                        }
                        if ret != 1 && step.flags & XML_STREAM_STEP_IN_SET as i32 != 0 {
                            // Check if we have a special case like "foo/bar//.", where
                            // "foo" is selected as well.
                            ret = 1;
                        }
                    }
                    if comp.flags & XML_STREAM_DESC as i32 == 0 && (!is_match || is_final != 0) {
                        // Mark this expression as blocked for any evaluation at
                        // deeper levels. Note that this includes "/foo"
                        // expressions if the *pattern* behaviour is used.
                        stream.block_level = stream.level + 1;
                    }
                    i += 1;
                }

                stream.level += 1;

                // Re/enter the expression.
                // Don't reenter if it's an absolute expression like "/foo",
                //   except "//foo".
                let step = &comp.steps[0];
                if step.flags & XML_STREAM_STEP_ROOT as i32 != 0 {
                    break 'stream_next;
                }

                let desc = step.flags & XML_STREAM_STEP_DESC as i32;
                'compare: {
                    if stream.flags & XML_PATTERN_NOTPATTERN != 0 {
                        // Re/enter the expression if it is a "descendant" one,
                        // or if we are at the 1st level of evaluation.
                        if stream.level == 1 {
                            if stream.is_xs_idc() {
                                // XS-IDC: The missing "self::node()" will always
                                // is_match the first given node.
                                break 'stream_next;
                            } else {
                                break 'compare;
                            }
                        }
                        // A "//" is always reentrant.
                        if desc != 0 {
                            break 'compare;
                        }
                        // XS-IDC: Process the 2nd level, since the missing
                        // "self::node()" is responsible for the 2nd level being
                        // the real start level.
                        if stream.level == 2 && stream.is_xs_idc() {
                            break 'compare;
                        }
                        break 'stream_next;
                    }
                }

                // compare:
                // Check expected node-type.
                if step.node_type != node_type
                    && (node_type == XmlElementType::XmlAttributeNode as i32
                        || step.node_type != XML_STREAM_ANY_NODE as i32)
                {
                    break 'stream_next;
                }
                // Compare local/namespace-name.
                let mut is_match = false;
                if step.node_type == XML_STREAM_ANY_NODE as i32 {
                    is_match = true;
                } else if step.name.is_none() {
                    if step.ns.is_none() {
                        // This lets through all elements/attributes.
                        is_match = true;
                    } else if ns.is_some() {
                        is_match = step.ns.as_deref() == ns;
                    }
                } else if step.ns.is_none() == ns.is_none()
                    && name.is_some()
                    && step.name.as_deref() == name
                    && step.ns.as_deref() == ns
                {
                    is_match = true;
                }
                is_final = step.flags & XML_STREAM_STEP_FINAL as i32;
                if is_match {
                    if is_final != 0 {
                        ret = 1;
                    } else {
                        stream.add_state(1, stream.level);
                    }
                    if ret != 1 && step.flags & XML_STREAM_STEP_IN_SET as i32 != 0 {
                        // Check if we have a special case like "foo//.", where
                        // "foo" is selected as well.
                        ret = 1;
                    }
                }
                if comp.flags & XML_STREAM_DESC as i32 == 0 && (!is_match || is_final != 0) {
                    // Mark this expression as blocked for any evaluation at
                    // deeper levels.
                    stream.block_level = stream.level;
                }
            }

            // stream_next:
            now = stream.next.as_deref_mut();
        }

        if err > 0 {
            ret = -1;
        }
        ret
    }

    /// Push new data onto the stream. NOTE: if the call xmlPatterncompile()
    /// indicated a dictionary, then strings for name and ns will be expected
    /// to come from the dictionary.
    /// Both @name and @ns being NULL means the / i.e. the root of the document.
    /// This can also act as a reset.
    /// Different from xmlStreamPush() this function can be fed with nodes of type:
    /// element-, attribute-, text-, cdata-section-, comment- and
    /// processing-instruction-node.
    ///
    /// Returns: -1 in case of error, 1 if the current state in the stream is a is_match and 0 otherwise.
    #[doc(alias = "xmlStreamPushNode")]
    pub fn push_node(&mut self, name: Option<&str>, ns: Option<&str>, node_type: i32) -> i32 {
        self.push_internal(name, ns, node_type)
    }

    /// Push new data onto the stream. NOTE: if the call xmlPatterncompile()
    /// indicated a dictionary, then strings for name and ns will be expected
    /// to come from the dictionary.
    /// Both @name and @ns being NULL means the / i.e. the root of the document.
    /// This can also act as a reset.
    /// Otherwise the function will act as if it has been given an element-node.
    ///
    /// Returns: -1 in case of error, 1 if the current state in the stream is a is_match and 0 otherwise.
    #[doc(alias = "xmlStreamPush")]
    pub fn push(&mut self, name: Option<&str>, ns: Option<&str>) -> i32 {
        self.push_internal(name, ns, XmlElementType::XmlElementNode as i32)
    }

    /// Push new attribute data onto the stream. NOTE: if the call xmlPatterncompile()
    /// indicated a dictionary, then strings for name and ns will be expected
    /// to come from the dictionary.
    /// Both @name and @ns being NULL means the / i.e. the root of the document.
    /// This can also act as a reset.
    /// Otherwise the function will act as if it has been given an attribute-node.
    ///
    /// Returns: -1 in case of error, 1 if the current state in the stream is a is_match and 0 otherwise.
    #[doc(alias = "xmlStreamPushAttr")]
    pub fn push_attr(&mut self, name: Option<&str>, ns: Option<&str>) -> i32 {
        self.push_internal(name, ns, XmlElementType::XmlAttributeNode as i32)
    }

    /// Push one level from the stream.
    ///
    /// Returns: -1 in case of error, 0 otherwise.
    #[doc(alias = "xmlStreamPop")]
    pub fn pop(&mut self) -> i32 {
        let mut now = Some(self);
        while let Some(stream) = now {
            // Reset block-level.
            if stream.block_level == stream.level {
                stream.block_level = -1;
            }

            //  (*stream).level can be zero when XML_FINAL_IS_ANY_NODE is set
            //  (see the thread at
            //  http://mail.gnome.org/archives/xslt/2008-July/msg00027.html)
            if stream.level != 0 {
                stream.level -= 1;
            }
            // Check evolution of existing states
            while stream
                .states
                .last()
                .is_some_and(|state| state.1 > stream.level)
            {
                stream.states.pop();
            }
            now = stream.next.as_deref_mut();
        }
        0
    }

    /// Query if the streaming pattern additionally needs to be fed with
    /// text-, cdata-section-, comment- and processing-instruction-nodes.
    /// If the result is 0 then only element-nodes and attribute-nodes
    /// need to be pushed.
    ///
    /// Returns 1 in case of need of nodes of the above described types,
    /// 0 otherwise. -1 on API errors.
    #[doc(alias = "xmlStreamWantsAnyNode")]
    pub fn wants_any_node(&self) -> i32 {
        let mut now = Some(self);
        while let Some(stream) = now {
            if stream.comp.flags & XML_STREAM_FINAL_IS_ANY_NODE as i32 != 0 {
                return 1;
            }
            now = stream.next.as_deref();
        }
        0
    }
}

unsafe impl Send for XmlStreamCtxt {}
unsafe impl Sync for XmlStreamCtxt {}