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
/* Copyright 2022-2023 Danny McClanahan */
/* SPDX-License-Identifier: BSD-3-Clause */

//! FFI wrappers for different types of pattern strings.
//!
//! Vectorscan supports 3 distinct types of pattern strings which can be formed
//! to produce a database:
//! - [`Expression`]: Vectorscan PCRE-like regex syntax (null-terminated
//!   [`CString`]).
//! - [`Literal`]: Literal byte string (`Vec<u8>`) which may contain nulls.
//! - [`chimera::ChimeraExpression`]: PCRE regex syntax.
//!
//! Each vectorscan database only supports matching against *exactly one* type
//! of these patterns, but each pattern string variant also has a `*Set` form,
//! and all of these forms support the same interface to vectorscan's most
//! powerful feature: multi-pattern matching, where patterns registered with
//! [`ExprId`] in a set can be associated to
//! [`ExpressionIndex`](crate::matchers::ExpressionIndex) instances when matched
//! against.
//!
//! Creating instances of these structs performs no pattern compilation itself,
//! which is instead performed in a subsequent step by e.g.
//! [`Database::compile()`]. References to these structs can be reused multiple
//! times to create multiple databases without re-allocating the underlying
//! pattern string data:
//!
//!```
//! # #[allow(unused_variables)]
//! # fn main() -> Result<(), vectorscan::error::VectorscanError> {
//! use vectorscan::{expression::*, flags::*};
//!
//! let a: Expression = "a+".parse()?;
//! let b: Expression = "b+".parse()?;
//! let c: Expression = "c+".parse()?;
//!
//! let ab_db = ExpressionSet::from_exprs([&a, &b]).compile(Mode::BLOCK)?;
//! let bc_db = ExpressionSet::from_exprs([&b, &c]).compile(Mode::BLOCK)?;
//! let ca_db = ExpressionSet::from_exprs([&c, &a]).compile(Mode::BLOCK)?;
//! # Ok(())
//! # }
//! ```

use crate::{
  database::Database,
  error::{VectorscanCompileError, VectorscanRuntimeError},
  flags::{ExtFlags, Flags, Mode},
  hs,
};

use std::{
  ffi::{CStr, CString},
  fmt,
  marker::PhantomData,
  mem, ops,
  os::raw::{c_char, c_uint, c_ulonglong},
  ptr, slice, str,
};

/// Vectorscan regex pattern string.
///
/// Vectorscan itself supports a subset of PCRE syntax in the pattern string;
/// see [Pattern Support] for reference. The use of unsupported constructs will
/// result in compilation errors.
///
/// Note that as the underlying vectorscan library interprets pattern strings as
/// null-terminated [`CStr`]s, null bytes are *not* supported within
/// `Expression` strings. Use a [`Literal`] or [`LiteralSet`] database if you
/// need to match against pattern strings containing explicit null bytes.
///
/// Instances can be created equivalently with [`Self::new()`] or
/// [`str::parse()`] via the [`str::FromStr`] impl:
///
///```
/// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
/// use vectorscan::expression::Expression;
///
/// let e1: Expression = "asdf+".parse()?;
/// let e2 = Expression::new("asdf+")?;
/// assert_eq!(e1, e2);
/// # Ok(())
/// # }
/// ```
///
/// [Pattern Support]: https://intel.github.io/vectorscan/dev-reference/compilation.html#pattern-support
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Expression(CString);

impl fmt::Display for Expression {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let b = self.as_bytes();
    match str::from_utf8(b) {
      Ok(s) => write!(f, "{}", s),
      Err(_) => write!(f, "(non-utf8: {:?})", b),
    }
  }
}

impl Expression {
  /// Reference the underlying bytes, *without* the trailing null terminator.
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// let e = vectorscan::expression::Expression::new("asdf")?;
  /// assert_eq!(e.as_bytes(), b"asdf");
  /// # Ok(())
  /// # }
  /// ```
  pub fn as_bytes(&self) -> &[u8] { self.0.as_bytes() }

  pub(crate) fn as_ptr(&self) -> *const c_char { self.0.as_c_str().as_ptr() }

  /// Produce a `NULL`-terminated C-style wrapper for the given pattern string.
  ///
  /// This will fail if the string contains any internal `NULL` bytes, as those
  /// are not supported by the vectorscan regex compiler:
  ///```
  /// use vectorscan::{expression::*, error::*};
  ///
  /// let pat = "as\0df";
  /// let e = match Expression::new(pat) {
  ///    Err(VectorscanCompileError::NullByte(e)) => e,
  ///    _ => unreachable!(),
  /// };
  /// assert_eq!(e.nul_position(), 2);
  /// ```
  pub fn new(x: impl Into<Vec<u8>>) -> Result<Self, VectorscanCompileError> {
    Ok(Self(CString::new(x)?))
  }

  /// Utility function providing information about a regular expression. The
  /// information provided in [`info::ExprInfo`] includes the minimum and
  /// maximum width of a pattern match.
  ///
  /// Note: successful analysis of an expression with this function does not
  /// imply that compilation of the same expression (via
  /// [`Database::compile()`] or [`Database::compile_multi()`]) would succeed.
  /// This function may return [`Ok`] for regular expressions that
  /// Vectorscan cannot compile.
  ///
  /// Note: some per-pattern flags (such as [`Flags::ALLOWEMPTY`] and
  /// [`Flags::SOM_LEFTMOST`]) are accepted by this call, but as they do not
  /// affect the properties returned in the [`info::ExprInfo`] structure,
  /// they will not affect the outcome of this function.
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::{*, info::*}, flags::Flags};
  ///
  /// let expr: Expression = "(he)llo".parse()?;
  ///
  /// let info = expr.info(Flags::default())?;
  ///
  /// assert_eq!(info, ExprInfo {
  ///   min_width: ExprWidth(5),
  ///   max_width: Some(ExprWidth(5)),
  ///   unordered_matches: UnorderedMatchBehavior::OnlyOrdered,
  ///   matches_at_eod: MatchAtEndBehavior::WillNeverMatchAtEOD,
  /// });
  /// # Ok(())
  /// # }
  /// ```
  pub fn info(&self, flags: Flags) -> Result<info::ExprInfo, VectorscanCompileError> {
    let mut info = ptr::null_mut();
    let mut compile_err = ptr::null_mut();
    VectorscanRuntimeError::copy_from_native_compile_error(
      unsafe {
        hs::hs_expression_info(
          self.as_ptr(),
          flags.into_native(),
          &mut info,
          &mut compile_err,
        )
      },
      compile_err,
    )?;

    let ret = info::ExprInfo::from_native(unsafe { *info });

    unsafe {
      crate::free_misc(info as *mut u8);
    }

    Ok(ret)
  }

  /// Utility function providing information about a regular expression, with
  /// extended parameter support. The information provided in [`info::ExprInfo`]
  /// includes the minimum and maximum width of a pattern match.
  ///
  /// Note: successful analysis of an expression with this function does not
  /// imply that compilation of the same expression (via
  /// [`Database::compile()`] or [`Database::compile_multi()`]) would succeed.
  /// This function may return [`Ok`] for regular expressions that
  /// Vectorscan cannot compile.
  ///
  /// Note: some per-pattern flags (such as [`Flags::ALLOWEMPTY`] and
  /// [`Flags::SOM_LEFTMOST`]) are accepted by this call, but as they do not
  /// affect the properties returned in the [`info::ExprInfo`] structure,
  /// they will not affect the outcome of this function.
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::{*, info::*}, flags::Flags};
  ///
  /// let expr: Expression = ".*lo".parse()?;
  ///
  /// let ext = ExprExt::from_min_length(4);
  ///
  /// let info = expr.ext_info(Flags::default(), &ext)?;
  ///
  /// assert_eq!(info, ExprInfo {
  ///   min_width: ExprWidth(4),
  ///   max_width: None,
  ///   unordered_matches: UnorderedMatchBehavior::OnlyOrdered,
  ///   matches_at_eod: MatchAtEndBehavior::WillNeverMatchAtEOD,
  /// });
  /// # Ok(())
  /// # }
  /// ```
  pub fn ext_info(
    &self,
    flags: Flags,
    ext_flags: &ExprExt,
  ) -> Result<info::ExprInfo, VectorscanCompileError> {
    let mut info = ptr::null_mut();
    let mut compile_err = ptr::null_mut();
    VectorscanRuntimeError::copy_from_native_compile_error(
      unsafe {
        hs::hs_expression_ext_info(
          self.as_ptr(),
          flags.into_native(),
          ext_flags.as_ref_native(),
          &mut info,
          &mut compile_err,
        )
      },
      compile_err,
    )?;

    let ret = info::ExprInfo::from_native(unsafe { *info });

    unsafe {
      crate::free_misc(info as *mut u8);
    }

    Ok(ret)
  }

  /// Call [`Database::compile()`] with [`None`] for the platform.
  pub fn compile(&self, flags: Flags, mode: Mode) -> Result<Database, VectorscanCompileError> {
    Database::compile(self, flags, mode, None)
  }
}

impl str::FromStr for Expression {
  type Err = VectorscanCompileError;

  fn from_str(s: &str) -> Result<Self, Self::Err> { Self::new(s) }
}

/// A literal byte string.
///
/// Unlike for [`Expression`], [`Database::compile_literal()`] will parse the
/// string content in a literal sense without any regular grammars. For example,
/// the expression `abc?` simply means a char sequence of `a`, `b`, `c`,
/// and `?`. The `?` here doesn't mean 0 or 1 quantifier under regular
/// semantics.
///
/// Also unlike [`Expression`], the underlying vectorscan library interprets
/// literal patterns with a pointer and a length instead of a `NULL`-terminated
/// string. **Importantly, this allows it to contain `\0` or `NULL` bytes
/// itself!**
///
/// Finally note that literal expressions do not support an "info" interface
/// like [`Expression::info()`] and [`Expression::ext_info()`], since most of
/// those properties can be inferred from the literal string itself.
///
/// Instances can be created equivalently with [`Self::new()`] or
/// [`str::parse()`] via the [`str::FromStr`] impl:
///```
/// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
/// use vectorscan::expression::Literal;
///
/// let e1: Literal = "as\0df".parse()?;
/// let e2 = Literal::new("as\0df")?;
/// assert_eq!(e1, e2);
/// # Ok(())
/// # }
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Literal(Vec<u8>);

impl fmt::Debug for Literal {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let b = self.as_bytes();
    match str::from_utf8(b) {
      Ok(s) => write!(f, "Literal({:?})", s),
      Err(_) => write!(f, "Literal({:?})", b),
    }
  }
}

impl fmt::Display for Literal {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let b = self.as_bytes();
    match str::from_utf8(b) {
      Ok(s) => write!(f, "{}", s),
      Err(_) => write!(f, "(non-utf8 literal: {:?})", b),
    }
  }
}

impl Literal {
  /// Reference the underlying bytes. This wrapper does *not* allocate any null
  /// terminator.
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// let e = vectorscan::expression::Literal::new("as\0df")?;
  /// assert_eq!(e.as_bytes(), b"as\0df");
  /// # Ok(())
  /// # }
  /// ```
  pub fn as_bytes(&self) -> &[u8] { &self.0 }

  pub(crate) fn as_ptr(&self) -> *const c_char {
    unsafe { mem::transmute(self.as_bytes().as_ptr()) }
  }

  /// Wrap a byte slice to be interpreted literally. This does *not* allocate
  /// any null terminator.
  pub fn new(x: impl Into<Vec<u8>>) -> Result<Self, VectorscanCompileError> { Ok(Self(x.into())) }

  /// Call [`Database::compile_literal()`] with [`None`] for the platform.
  pub fn compile(&self, flags: Flags, mode: Mode) -> Result<Database, VectorscanCompileError> {
    Database::compile_literal(self, flags, mode, None)
  }
}

impl str::FromStr for Literal {
  type Err = VectorscanCompileError;

  fn from_str(s: &str) -> Result<Self, Self::Err> { Self::new(s) }
}

/// The ID number to associate with a pattern match in an expression set.
///
/// When provided to an expression set, this value is converted into an
/// [`ExpressionIndex`](crate::matchers::ExpressionIndex) in a
/// [`Match`](crate::matchers::Match),
/// [`VectoredMatch`](crate::matchers::VectoredMatch), or
/// [`ChimeraMatch`](crate::matchers::chimera::ChimeraMatch) upon matching the
/// given pattern.
///
/// This ID is used in [`ExpressionSet::with_ids()`],
/// [`LiteralSet::with_ids()`], and
/// [`ChimeraExpressionSet::with_ids()`](chimera::ChimeraExpressionSet::with_ids).
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ExprId(pub c_uint);

/// Collection of regular expressions.
///
/// This is the main entry point to vectorscan's primary functionality: matching
/// against sets of patterns at once, which is typically poorly supported or
/// less featureful than single-pattern matching in many other regex engines.
///
/// This struct provides an immutable (returning `Self`) builder interface
/// to attach additional configuration to the initial set of patterns
/// constructed with [`Self::from_exprs()`].
#[derive(Clone)]
pub struct ExpressionSet<'a> {
  ptrs: Vec<*const c_char>,
  flags: Option<Vec<Flags>>,
  ids: Option<Vec<ExprId>>,
  exts: Option<Vec<*const hs::hs_expr_ext>>,
  _ph: PhantomData<&'a u8>,
}

impl<'a> fmt::Debug for ExpressionSet<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let exprs: Vec<&'a CStr> = self
      .ptrs
      .iter()
      .map(|p| unsafe { CStr::from_ptr(*p) })
      .collect();
    let exts: Option<&[Option<&ExprExt>]> = self
      .exts
      .as_ref()
      .map(|exts| unsafe { slice::from_raw_parts(mem::transmute(exts.as_ptr()), exprs.len()) });
    write!(
      f,
      "ExpressionSet(exprs={:?}, flags={:?}, ids={:?}, exts={:?})",
      exprs, &self.flags, &self.ids, exts,
    )
  }
}

impl<'a> ExpressionSet<'a> {
  /// Construct a pattern set from references to parsed expressions.
  ///
  /// The length of this initial `exprs` argument is returned by
  /// [`Self::len()`], and all subsequent configuration methods are checked to
  /// provide iterators of the same length:
  ///
  ///```should_panic
  /// use vectorscan::expression::*;
  ///
  /// let a: Expression = "a+".parse().unwrap();
  /// // Fails due to argument length mismatch:
  /// ExpressionSet::from_exprs([&a])
  ///   .with_flags([]);
  /// ```
  pub fn from_exprs(exprs: impl IntoIterator<Item=&'a Expression>) -> Self {
    Self {
      ptrs: exprs.into_iter().map(|e| e.as_ptr()).collect(),
      flags: None,
      ids: None,
      exts: None,
      _ph: PhantomData,
    }
  }

  /// Provide flags which modify the behavior of each expression.
  ///
  /// The length of `flags` is checked to be the same as [`Self::len()`].
  ///
  /// If this builder method is not used, [`Flags::default()`] will be assigned
  /// to all patterns.
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::*, flags::*, matchers::*};
  ///
  /// // Create two expressions to demonstrate separate flags for each pattern:
  /// let a: Expression = "a+[^a]".parse()?;
  /// let b: Expression = "b+[^b]".parse()?;
  ///
  /// // Get the start of match for one pattern, but not the other:
  /// let db = ExpressionSet::from_exprs([&a, &b])
  ///   .with_flags([Flags::default(), Flags::SOM_LEFTMOST])
  ///   .compile(Mode::BLOCK)?;
  ///
  /// let mut scratch = db.allocate_scratch()?;
  ///
  /// let mut matches: Vec<&str> = Vec::new();
  /// scratch.scan_sync(&db, "aardvark imbibbe".into(), |m| {
  ///   matches.push(unsafe { m.source.as_str() });
  ///   MatchResult::Continue
  /// })?;
  /// // Start of match is preserved for only one pattern:
  /// assert_eq!(&matches, &["aar", "aardvar", "bi", "bbe"]);
  /// # Ok(())
  /// # }
  /// ```
  pub fn with_flags(mut self, flags: impl IntoIterator<Item=Flags>) -> Self {
    let flags: Vec<_> = flags.into_iter().collect();
    assert_eq!(self.len(), flags.len());
    self.flags = Some(flags);
    self
  }

  /// Assign an ID number to each pattern.
  ///
  /// The length of `ids` is checked to be the same as [`Self::len()`]. Multiple
  /// patterns can be assigned the same ID.
  ///
  /// If this builder method is not used, vectorscan will assign them all the ID
  /// number 0:
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::*, flags::*, state::*, matchers::*, sources::*};
  ///
  /// // Create two expressions to demonstrate multiple pattern IDs.
  /// let a: Expression = "a+[^a]".parse()?;
  /// let b: Expression = "b+[^b]".parse()?;
  ///
  /// // Create one db with ID numbers, and one without.
  /// let set1 = ExpressionSet::from_exprs([&a, &b]).compile(Mode::BLOCK)?;
  /// let set2 = ExpressionSet::from_exprs([&a, &b])
  ///   .with_ids([ExprId(300), ExprId(12)])
  ///   .compile(Mode::BLOCK)?;
  ///
  /// let mut scratch = Scratch::blank();
  /// scratch.setup_for_db(&set1)?;
  /// scratch.setup_for_db(&set2)?;
  ///
  /// let msg: ByteSlice = "aardvark imbibbe".into();
  ///
  /// // The first db doesn't differentiate matches by ID number:
  /// let mut matches1: Vec<ExpressionIndex> = Vec::new();
  /// scratch.scan_sync(&set1, msg, |m| {
  ///   matches1.push(m.id);
  ///   MatchResult::Continue
  /// })?;
  /// assert_eq!(
  ///   &matches1,
  ///   &[ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0)],
  /// );
  ///
  /// // The second db returns corresponding ExpressionIndex instances:
  /// let mut matches2: Vec<ExpressionIndex> = Vec::new();
  /// scratch.scan_sync(&set2, msg, |m| {
  ///   matches2.push(m.id);
  ///   MatchResult::Continue
  /// })?;
  /// assert_eq!(
  ///   &matches2,
  ///   &[ExpressionIndex(300), ExpressionIndex(300), ExpressionIndex(12), ExpressionIndex(12)],
  /// );
  /// # Ok(())
  /// # }
  /// ```
  pub fn with_ids(mut self, ids: impl IntoIterator<Item=ExprId>) -> Self {
    let ids: Vec<_> = ids.into_iter().collect();
    assert_eq!(self.len(), ids.len());
    self.ids = Some(ids);
    self
  }

  /// Optionally assign [`ExprExt`] configuration to each pattern.
  ///
  /// This is the only available entry point to compiling a database with
  /// [`ExprExt`] configuration for a given pattern (i.e. the single
  /// expression compiler does not support extended configuration).
  ///
  /// If [`Expression::ext_info()`] succeeds with a given
  /// [`Expression`]/[`ExprExt`] pair, then compiling the same pattern and
  /// configuration into a vectorscan database via an expression set with this
  /// method is likely but not guaranteed to succeed.
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::*, flags::*, matchers::*};
  ///
  /// // Apply extended configuration to one version of the pattern, but not the other:
  /// let a: Expression = "a.*b".parse()?;
  /// let a_ext = ExprExt::from_min_length(4);
  /// let set = ExpressionSet::from_exprs([&a, &a])
  ///   .with_exts([Some(&a_ext), None])
  ///   .with_ids([ExprId(1), ExprId(2)])
  ///   .compile(Mode::BLOCK)?;
  /// let mut scratch = set.allocate_scratch()?;
  ///
  /// // The configured pattern does not match because of its min length attribute:
  /// let mut matches: Vec<ExpressionIndex> = Vec::new();
  /// scratch.scan_sync(&set, "ab".into(), |m| {
  ///   matches.push(m.id);
  ///   MatchResult::Continue
  /// })?;
  /// assert_eq!(&matches, &[ExpressionIndex(2)]);
  ///
  /// // Howver, both patterns match a longer input:
  /// matches.clear();
  /// scratch.scan_sync(&set, "asssssb".into(), |m| {
  ///   matches.push(m.id);
  ///   MatchResult::Continue
  /// })?;
  /// assert_eq!(&matches, &[ExpressionIndex(1), ExpressionIndex(2)]);
  /// # Ok(())
  /// # }
  /// ```
  pub fn with_exts(mut self, exts: impl IntoIterator<Item=Option<&'a ExprExt>>) -> Self {
    let exts: Vec<*const hs::hs_expr_ext> = exts
      .into_iter()
      .map(|e| {
        e.map(|e| e.as_ref_native() as *const hs::hs_expr_ext)
          .unwrap_or(ptr::null())
      })
      .collect();
    assert_eq!(self.len(), exts.len());
    self.exts = Some(exts);
    self
  }

  /// Call [`Database::compile_multi()`] with [`None`] for the platform.
  pub fn compile(self, mode: Mode) -> Result<Database, VectorscanCompileError> {
    Database::compile_multi(&self, mode, None)
  }

  /// The number of patterns in this set.
  pub fn len(&self) -> usize { self.ptrs.len() }

  /// Whether this set contains any patterns.
  pub fn is_empty(&self) -> bool { self.len() == 0 }

  pub(crate) fn num_elements(&self) -> c_uint { self.len() as c_uint }

  pub(crate) fn exts_ptr(&self) -> Option<*const *const hs::hs_expr_ext> {
    self.exts.as_ref().map(|e| e.as_ptr())
  }

  pub(crate) fn expressions_ptr(&self) -> *const *const c_char { self.ptrs.as_ptr() }

  pub(crate) fn flags_ptr(&self) -> *const c_uint {
    self
      .flags
      .as_ref()
      .map(|f| unsafe { mem::transmute(f.as_ptr()) })
      .unwrap_or(ptr::null())
  }

  pub(crate) fn ids_ptr(&self) -> *const c_uint {
    self
      .ids
      .as_ref()
      .map(|i| unsafe { mem::transmute(i.as_ptr()) })
      .unwrap_or(ptr::null())
  }
}

/// Data produced by vectorscan to analyze a particular expression.
///
/// These structs cover the output of [`Expression::info()`] and
/// [`Expression::ext_info()`].
pub mod info {
  use crate::hs;

  use displaydoc::Display;

  use std::os::raw::{c_char, c_uint};

  /// The upper or lower bound for the length of any matches returned by a
  /// pattern.
  #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
  #[repr(transparent)]
  pub struct ExprWidth(pub usize);

  impl ExprWidth {
    pub(crate) const fn parse_min_width(x: c_uint) -> Self { Self(x as usize) }

    pub(crate) const fn parse_max_width(x: c_uint) -> Option<Self> {
      if x == c_uint::MAX {
        None
      } else {
        Some(Self(x as usize))
      }
    }
  }

  /// Whether the expression can produce matches that are not returned in order,
  /// such as those produced by assertions.
  #[derive(
    Debug,
    Display,
    Copy,
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    num_enum::IntoPrimitive,
    num_enum::FromPrimitive,
  )]
  #[repr(i8)]
  pub enum UnorderedMatchBehavior {
    /// Disallows matches that are not returned in order.
    #[num_enum(default)]
    OnlyOrdered = 0,
    /// Allows matches that are not returned in order.
    AllowsUnordered = 1,
  }

  impl UnorderedMatchBehavior {
    pub(crate) const fn from_native(x: c_char) -> Self {
      if x == 0 {
        Self::OnlyOrdered
      } else {
        Self::AllowsUnordered
      }
    }
  }

  /// Whether this expression can produce matches at end of data (EOD).
  ///
  /// In streaming mode, EOD matches are raised during
  /// [`Scratch::flush_eod_sync()`](crate::state::Scratch::flush_eod_sync) or
  /// [`Scratch::flush_eod_sync()`](crate::state::Scratch::flush_eod_sync),
  /// since it is only when `flush_eod()` is called that the EOD location is
  /// known.
  ///
  /// Note: trailing `\b` word boundary assertions may also result in EOD
  /// matches as end-of-data can act as a word boundary.
  #[derive(Debug, Display, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
  #[repr(i8)]
  pub enum MatchAtEndBehavior {
    /// Pattern will never match at EOD.
    WillNeverMatchAtEOD,
    /// Pattern *may* match at EOD.
    MayMatchAtEOD,
    /// Pattern will *only* match at EOD.
    WillOnlyMatchAtEOD,
  }

  impl MatchAtEndBehavior {
    pub(crate) fn from_native(matches_at_eod: c_char, matches_only_at_eod: c_char) -> Self {
      match (matches_at_eod, matches_only_at_eod) {
        (0, 0) => Self::WillNeverMatchAtEOD,
        (x, 0) if x != 0 => Self::MayMatchAtEOD,
        (_, x) if x != 0 => Self::WillOnlyMatchAtEOD,
        x => unreachable!("unreachable pattern: {:?}", x),
      }
    }
  }

  /// Data produced by vectorscan to analyze a particular expression.
  ///
  /// This struct is produced by [`super::Expression::info()`]:
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::{*, info::*}, flags::Flags};
  ///
  /// let expr: Expression = "(he)llo$".parse()?;
  /// let info = expr.info(Flags::default())?;
  /// assert_eq!(info, ExprInfo {
  ///   min_width: ExprWidth(5),
  ///   max_width: Some(ExprWidth(5)),
  ///   unordered_matches: UnorderedMatchBehavior::AllowsUnordered,
  ///   matches_at_eod: MatchAtEndBehavior::WillOnlyMatchAtEOD,
  /// });
  /// # Ok(())
  /// # }
  /// ```
  ///
  /// as well as [`super::Expression::ext_info()`]:
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::{*, info::*}, flags::Flags};
  ///
  /// let expr: Expression = ".*lo($)?".parse()?;
  /// let ext = ExprExt::from_min_length(4);
  /// let info = expr.ext_info(Flags::default(), &ext)?;
  /// assert_eq!(info, ExprInfo {
  ///   min_width: ExprWidth(4),
  ///   max_width: None,
  ///   unordered_matches: UnorderedMatchBehavior::AllowsUnordered,
  ///   matches_at_eod: MatchAtEndBehavior::MayMatchAtEOD,
  /// });
  /// # Ok(())
  /// # }
  /// ```
  #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
  pub struct ExprInfo {
    /// The minimum length in bytes of a match for the pattern. If the pattern
    /// has an unbounded minimum length, this will be 0.
    ///
    /// Note: in some cases when using advanced features to suppress matches
    /// (such as extended parameters or
    /// [`Flags::SINGLEMATCH`](crate::flags::Flags::SINGLEMATCH)) this
    /// may represent a conservative lower bound for the true minimum length of
    /// a match.
    pub min_width: ExprWidth,
    /// The maximum length in bytes of a match for the pattern. If the pattern
    /// has an unbounded maximum length, this will be [`None`].
    ///
    /// Note: in some cases when using advanced features to suppress matches
    /// (such as extended parameters or
    /// [`Flags::SINGLEMATCH`](crate::flags::Flags::SINGLEMATCH)) this
    /// may represent a conservative upper bound for the true maximum length of
    /// a match.
    pub max_width: Option<ExprWidth>,
    /// Whether this expression can produce matches that are not returned in
    /// order, such as those produced by assertions.
    pub unordered_matches: UnorderedMatchBehavior,
    /// Whether this expression can produce matches at end of data (EOD).
    ///
    /// In streaming mode, EOD matches are raised during
    /// [`Scratch::flush_eod_sync()`](crate::state::Scratch::flush_eod_sync) or
    /// [`Scratch::flush_eod_sync()`](crate::state::Scratch::flush_eod_sync),
    /// since it is only when `flush_eod()` is called that the EOD location
    /// is known.
    ///
    /// Note: trailing `\b` word boundary assertions may also result in EOD
    /// matches as end-of-data can act as a word boundary.
    pub matches_at_eod: MatchAtEndBehavior,
  }

  impl ExprInfo {
    pub(crate) fn from_native(x: hs::hs_expr_info) -> Self {
      let hs::hs_expr_info {
        min_width,
        max_width,
        unordered_matches,
        matches_at_eod,
        matches_only_at_eod,
      } = x;
      let min_width = ExprWidth::parse_min_width(min_width);
      let max_width = ExprWidth::parse_max_width(max_width);
      let unordered_matches = UnorderedMatchBehavior::from_native(unordered_matches);
      let matches_at_eod = MatchAtEndBehavior::from_native(matches_at_eod, matches_only_at_eod);
      Self {
        min_width,
        max_width,
        unordered_matches,
        matches_at_eod,
      }
    }
  }
}

/// Configuration for extended vectorscan parameters.
///
/// These parameters cover various types of fuzzy search as well as input
/// subsetting features. See [Extended Parameters] for a further reference.
///
/// [Extended Parameters]: https://intel.github.io/vectorscan/dev-reference/compilation.html#extparam
///
/// This structure may be passed in when building a database with
/// [`ExpressionSet::with_exts()`], or used to interrogate a single expression
/// with [`Expression::ext_info()`].
///
/// Like many other flags arguments, this struct also supports [`ops::BitOr`]
/// and the `|` operator for composition:
///
///```
/// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
/// use vectorscan::{expression::*, flags::*, matchers::*, sources::*};
///
/// // Apply extended configuration to one version of the pattern, but not the other:
/// let a: Expression = "ab".parse()?;
/// let ext = ExprExt::from_min_offset(3) | ExprExt::from_max_offset(15);
/// let set = ExpressionSet::from_exprs([&a, &a])
///   .with_exts([Some(&ext), None])
///   .with_ids([ExprId(1), ExprId(2)])
///   .compile(Mode::BLOCK)?;
/// let mut scratch = set.allocate_scratch()?;
///
/// let msg: ByteSlice = "ab   ab                ab".into();
///
/// let mut matches: Vec<ExpressionIndex> = Vec::new();
/// scratch.scan_sync(&set, msg, |m| {
///   matches.push(m.id);
///   MatchResult::Continue
/// })?;
///
/// // The configured pattern misses out on the first and last match of "ab":
/// assert_eq!(&matches, &[
///   ExpressionIndex(2), ExpressionIndex(1), ExpressionIndex(2), ExpressionIndex(2),
/// ]);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct ExprExt(hs::hs_expr_ext);

impl Default for ExprExt {
  fn default() -> Self { Self::zeroed() }
}

impl ExprExt {
  /// Generate an empty instance with all features disabled.
  /* FIXME: make this const when const zeroed() is stabilized! */
  pub fn zeroed() -> Self { unsafe { mem::MaybeUninit::zeroed().assume_init() } }

  /// The minimum end offset in the data stream at which this expression should
  /// match successfully.
  pub fn from_min_offset(x: usize) -> Self {
    let ext_flags = ExtFlags::MIN_OFFSET;
    let mut s = Self::zeroed();
    s.0.flags = ext_flags.into_native();
    s.0.min_offset = x as c_ulonglong;
    s
  }

  /// The maximum end offset in the data stream at which this expression should
  /// match successfully.
  pub fn from_max_offset(x: usize) -> Self {
    let ext_flags = ExtFlags::MAX_OFFSET;
    let mut s = Self::zeroed();
    s.0.flags = ext_flags.into_native();
    s.0.max_offset = x as c_ulonglong;
    s
  }

  /// The minimum match length (from start to end) required to successfully
  /// match this expression.
  ///
  /// This is one alternative to the use of [`Flags::ALLOWEMPTY`].
  ///
  /// This does not require [`Flags::SOM_LEFTMOST`]:
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::*, flags::*, matchers::*, sources::*};
  ///
  /// let a: Expression = "a.*b".parse()?;
  /// let ext = ExprExt::from_min_length(4);
  /// let set = ExpressionSet::from_exprs([&a, &a])
  ///   // #1 has no min_length, #2 does:
  ///   .with_exts([None, Some(&ext)])
  ///   .with_ids([ExprId(1), ExprId(2)])
  ///   .compile(Mode::BLOCK)?;
  /// let mut scratch = set.allocate_scratch()?;
  ///
  /// let msg: ByteSlice = "   ab   ab   ".into();
  ///
  /// let mut matches: Vec<(u32, &str)> = Vec::new();
  /// scratch.scan_sync(&set, msg, |m| {
  ///   matches.push((m.id.0, unsafe { m.source.as_str() }));
  ///   MatchResult::Continue
  /// })?;
  ///
  /// assert_eq!(&matches, &[
  ///   // Without min_length, both matches show up:
  ///   (1, "   ab"),
  ///   (1, "   ab   ab"),
  ///   // SOM_LEFTMOST is disabled, so we don't know the match start,
  ///   // but the min_length property is correctly applied regardless:
  ///   (2, "   ab   ab"),
  /// ]);
  /// # Ok(())
  /// # }
  /// ```
  pub fn from_min_length(x: usize) -> Self {
    let ext_flags = ExtFlags::MIN_LENGTH;
    let mut s = Self::zeroed();
    s.0.flags = ext_flags.into_native();
    s.0.min_length = x as c_ulonglong;
    s
  }

  /// Allow patterns to approximately match within this [edit distance](https://en.wikipedia.org/wiki/Edit_distance).
  pub fn from_edit_distance(x: usize) -> Self {
    let ext_flags = ExtFlags::EDIT_DISTANCE;
    let mut s = Self::zeroed();
    s.0.flags = ext_flags.into_native();
    assert!(x < c_uint::MAX as usize);
    s.0.edit_distance = x as c_uint;
    s
  }

  /// Allow patterns to approximately match within this [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance).
  pub fn from_hamming_distance(x: usize) -> Self {
    let ext_flags = ExtFlags::HAMMING_DISTANCE;
    let mut s = Self::zeroed();
    s.0.flags = ext_flags.into_native();
    assert!(x < c_uint::MAX as usize);
    s.0.hamming_distance = x as c_uint;
    s
  }

  const fn ext_flags(&self) -> ExtFlags { ExtFlags::from_native(self.0.flags) }

  fn min_offset(&self) -> Option<c_ulonglong> {
    if self.ext_flags().has_min_offset() {
      Some(self.0.min_offset)
    } else {
      None
    }
  }

  fn max_offset(&self) -> Option<c_ulonglong> {
    if self.ext_flags().has_max_offset() {
      Some(self.0.max_offset)
    } else {
      None
    }
  }

  fn min_length(&self) -> Option<c_ulonglong> {
    if self.ext_flags().has_min_length() {
      Some(self.0.min_length)
    } else {
      None
    }
  }

  fn edit_distance(&self) -> Option<c_uint> {
    if self.ext_flags().has_edit_distance() {
      Some(self.0.edit_distance)
    } else {
      None
    }
  }

  fn hamming_distance(&self) -> Option<c_uint> {
    if self.ext_flags().has_hamming_distance() {
      Some(self.0.hamming_distance)
    } else {
      None
    }
  }

  fn compose(mut self, rhs: Self) -> Self {
    self.0.flags = (self.ext_flags() | rhs.ext_flags()).into_native();
    if let Some(min_offset) = rhs.min_offset() {
      self.0.min_offset = min_offset;
    }
    if let Some(max_offset) = rhs.max_offset() {
      self.0.max_offset = max_offset;
    }
    if let Some(min_length) = rhs.min_length() {
      self.0.min_length = min_length;
    }
    if let Some(edit_distance) = rhs.edit_distance() {
      self.0.edit_distance = edit_distance;
    }
    if let Some(hamming_distance) = rhs.hamming_distance() {
      self.0.hamming_distance = hamming_distance;
    }
    self
  }

  pub(crate) fn as_ref_native(&self) -> &hs::hs_expr_ext { &self.0 }
}

impl ops::BitOr for ExprExt {
  type Output = Self;

  fn bitor(self, other: Self) -> Self { self.compose(other) }
}

impl ops::BitOrAssign for ExprExt {
  fn bitor_assign(&mut self, rhs: Self) {
    use ops::BitOr;
    *self = self.bitor(rhs);
  }
}

/// Collection of literals.
///
/// This is the analogue to [`ExpressionSet`] for [`Literal`] expressions, which
/// cannot be combined with [`Expression`] patterns in the same database.
///
/// This struct provides an immutable (returning `Self`) builder interface
/// to attach additional configuration to the initial set of patterns
/// constructed with [`Self::from_lits()`].
#[derive(Clone)]
pub struct LiteralSet<'a> {
  ptrs: Vec<*const c_char>,
  lens: Vec<usize>,
  flags: Option<Vec<Flags>>,
  ids: Option<Vec<ExprId>>,
  _ph: PhantomData<&'a u8>,
}

impl<'a> fmt::Debug for LiteralSet<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let exprs: Vec<&'a [u8]> = self
      .ptrs
      .iter()
      .zip(self.lens.iter())
      .map(|(p, n)| unsafe { slice::from_raw_parts(*p as *const u8, *n) })
      .collect();
    let joined_exprs: String = exprs
      .into_iter()
      .map(|s| {
        str::from_utf8(s)
          .map(|s| format!("{:?}", s))
          .unwrap_or_else(|_| format!("(non-utf8: {:?})", s))
      })
      .collect::<Vec<_>>()
      .join(", ");
    write!(
      f,
      "LiteralSet(exprs=[{}], flags={:?}, ids={:?})",
      joined_exprs, &self.flags, &self.ids
    )
  }
}

impl<'a> LiteralSet<'a> {
  /// Construct a pattern set from references to parsed literals.
  ///
  /// The length of this initial `exprs` argument is returned by
  /// [`Self::len()`], and all subsequent configuration methods are checked to
  /// provide iterators of the same length:
  ///
  ///```should_panic
  /// use vectorscan::expression::*;
  ///
  /// let a: Literal = "a\0b".parse().unwrap();
  /// // Fails due to argument length mismatch:
  /// LiteralSet::from_lits([&a])
  ///   .with_flags([]);
  /// ```
  pub fn from_lits(lits: impl IntoIterator<Item=&'a Literal>) -> Self {
    let mut ptrs: Vec<_> = Vec::new();
    let mut lens: Vec<_> = Vec::new();

    for l in lits.into_iter() {
      ptrs.push(l.as_ptr());
      lens.push(l.as_bytes().len());
    }

    Self {
      ptrs,
      lens,
      flags: None,
      ids: None,
      _ph: PhantomData,
    }
  }

  /// Provide flags which modify the behavior of each expression.
  ///
  /// The length of `flags` is checked to be the same as [`Self::len()`].
  ///
  /// If this builder method is not used, [`Flags::default()`] will be assigned
  /// to all patterns.
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::*, flags::*, matchers::*};
  ///
  /// // Create two expressions to demonstrate separate flags for each pattern:
  /// let a: Literal = "a".parse()?;
  /// let b: Literal = "b".parse()?;
  ///
  /// // Get the start of match for one pattern, but not the other:
  /// let db = LiteralSet::from_lits([&a, &b])
  ///   .with_flags([Flags::default(), Flags::SOM_LEFTMOST])
  ///   .compile(Mode::BLOCK)?;
  ///
  /// let mut scratch = db.allocate_scratch()?;
  ///
  /// let mut matches: Vec<&str> = Vec::new();
  /// scratch.scan_sync(&db, "aardvark imbibbe".into(), |m| {
  ///   matches.push(unsafe { m.source.as_str() });
  ///   MatchResult::Continue
  /// })?;
  /// // Start of match is preserved for only one pattern:
  /// assert_eq!(&matches, &["a", "aa", "aardva", "b", "b", "b"]);
  /// # Ok(())
  /// # }
  /// ```
  pub fn with_flags(mut self, flags: impl IntoIterator<Item=Flags>) -> Self {
    let flags: Vec<_> = flags.into_iter().collect();
    assert_eq!(self.len(), flags.len());
    self.flags = Some(flags.to_vec());
    self
  }

  /// Assign an ID number to each pattern.
  ///
  /// The length of `ids` is checked to be the same as [`Self::len()`]. Multiple
  /// patterns can be assigned the same ID.
  ///
  /// If this builder method is not used, vectorscan will assign them all the ID
  /// number 0:
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::VectorscanError> {
  /// use vectorscan::{expression::*, flags::*, state::*, matchers::*, sources::*};
  ///
  /// // Create two expressions to demonstrate multiple pattern IDs.
  /// let a: Literal = "a".parse()?;
  /// let b: Literal = "b".parse()?;
  ///
  /// // Create one db with ID numbers, and one without.
  /// let set1 = LiteralSet::from_lits([&a, &b]).compile(Mode::BLOCK)?;
  /// let set2 = LiteralSet::from_lits([&a, &b])
  ///   .with_ids([ExprId(300), ExprId(12)])
  ///   .compile(Mode::BLOCK)?;
  ///
  /// let mut scratch = Scratch::blank();
  /// scratch.setup_for_db(&set1)?;
  /// scratch.setup_for_db(&set2)?;
  ///
  /// let msg: ByteSlice = "aardvark imbibbe".into();
  ///
  /// // The first db doesn't differentiate matches by ID number:
  /// let mut matches1: Vec<ExpressionIndex> = Vec::new();
  /// scratch.scan_sync(&set1, msg, |m| {
  ///   matches1.push(m.id);
  ///   MatchResult::Continue
  /// })?;
  /// assert_eq!(
  ///   &matches1,
  ///   &[
  ///      ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0),
  ///      ExpressionIndex(0), ExpressionIndex(0),
  ///    ],
  /// );
  ///
  /// // The second db returns corresponding ExpressionIndex instances:
  /// let mut matches2: Vec<ExpressionIndex> = Vec::new();
  /// scratch.scan_sync(&set2, msg, |m| {
  ///   matches2.push(m.id);
  ///   MatchResult::Continue
  /// })?;
  /// assert_eq!(
  ///   &matches2,
  ///   &[
  ///      ExpressionIndex(300), ExpressionIndex(300), ExpressionIndex(300),
  ///      ExpressionIndex(12), ExpressionIndex(12), ExpressionIndex(12),
  ///    ],
  /// );
  /// # Ok(())
  /// # }
  /// ```
  pub fn with_ids(mut self, ids: impl IntoIterator<Item=ExprId>) -> Self {
    let ids: Vec<_> = ids.into_iter().collect();
    assert_eq!(self.len(), ids.len());
    self.ids = Some(ids.to_vec());
    self
  }

  /// Call [`Database::compile_multi_literal()`] with [`None`] for the platform.
  pub fn compile(self, mode: Mode) -> Result<Database, VectorscanCompileError> {
    Database::compile_multi_literal(&self, mode, None)
  }

  /// The number of literals in this set.
  pub fn len(&self) -> usize { self.ptrs.len() }

  /// Whether this set contains any literals.
  pub fn is_empty(&self) -> bool { self.len() == 0 }

  pub(crate) fn num_elements(&self) -> c_uint { self.len() as c_uint }

  pub(crate) fn literals_ptr(&self) -> *const *const c_char { self.ptrs.as_ptr() }

  pub(crate) fn lengths_ptr(&self) -> *const usize { self.lens.as_ptr() }

  pub(crate) fn flags_ptr(&self) -> *const c_uint {
    self
      .flags
      .as_ref()
      .map(|f| unsafe { mem::transmute(f.as_ptr()) })
      .unwrap_or(ptr::null())
  }

  pub(crate) fn ids_ptr(&self) -> *const c_uint {
    self
      .ids
      .as_ref()
      .map(|i| unsafe { mem::transmute(i.as_ptr()) })
      .unwrap_or(ptr::null())
  }
}

/// Pattern strings for the chimera library.
///
/// As per [Pattern Support], chimera has full support for PCRE.
///
/// [Pattern Support]: https://intel.github.io/vectorscan/dev-reference/chimera.html#pattern-support
///
/// As chimera focuses mainly on supporting PCRE compatibility and group
/// matching support, this interface is less full-featured than the standard
/// vectorscan library [`super::expression`]. However, the same idioms apply:
/// creating expression instances performs no pattern compilation itself, and
/// references to these structs can be reused without re-allocating the
/// underlying pattern string data:
///
///```
/// # #[allow(unused_variables)]
/// # fn main() -> Result<(), vectorscan::error::chimera::ChimeraError> {
/// use vectorscan::{expression::chimera::*, flags::chimera::*};
///
/// let a: ChimeraExpression = "a+".parse()?;
/// let b: ChimeraExpression = "b+".parse()?;
/// let c: ChimeraExpression = "c+".parse()?;
///
/// let ab_db = ChimeraExpressionSet::from_exprs([&a, &b]).compile(ChimeraMode::NOGROUPS)?;
/// let bc_db = ChimeraExpressionSet::from_exprs([&b, &c]).compile(ChimeraMode::NOGROUPS)?;
/// let ca_db = ChimeraExpressionSet::from_exprs([&c, &a]).compile(ChimeraMode::NOGROUPS)?;
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "chimera")]
#[cfg_attr(docsrs, doc(cfg(feature = "chimera")))]
pub mod chimera {
  use super::ExprId;
  use crate::{
    database::chimera::ChimeraDb,
    error::chimera::ChimeraCompileError,
    flags::chimera::{ChimeraFlags, ChimeraMode},
  };

  use std::{
    ffi::{CStr, CString},
    fmt,
    marker::PhantomData,
    mem,
    os::raw::{c_char, c_uint, c_ulong},
    ptr, str,
  };

  /// Chimera (PCRE) pattern string.
  ///
  /// Note that as the underlying chimera library interprets pattern strings as
  /// null-terminated [`CStr`]s, null bytes are *not* supported within
  /// `ChimeraExpression` strings. If matching against patterns containing
  /// explicit null bytes is necessary, consider [`super::Literal`] or
  /// [`super::LiteralSet`] from the base vectorscan library.
  ///
  /// Note also that the chimera library does not support an "info" interface
  /// such as [`super::Expression::info()`] and
  /// [`super::Expression::ext_info()`] from the base vectorscan library.
  ///
  /// Instances can be created equivalently with [`Self::new()`] or
  /// [`str::parse()`] via the [`str::FromStr`] impl:
  ///
  ///```
  /// # fn main() -> Result<(), vectorscan::error::chimera::ChimeraError> {
  /// use vectorscan::expression::chimera::ChimeraExpression;
  ///
  /// let e1: ChimeraExpression = "asd(f+)".parse()?;
  /// let e2 = ChimeraExpression::new("asd(f+)")?;
  /// assert_eq!(e1, e2);
  /// # Ok(())
  /// # }
  /// ```
  #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
  pub struct ChimeraExpression(CString);

  impl fmt::Debug for ChimeraExpression {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
      let b = self.as_bytes();
      match str::from_utf8(b) {
        Ok(s) => write!(f, "ChimeraExpression({:?})", s),
        Err(_) => write!(f, "ChimeraExpression({:?})", b),
      }
    }
  }

  impl fmt::Display for ChimeraExpression {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
      let b = self.as_bytes();
      match str::from_utf8(b) {
        Ok(s) => write!(f, "{}", s),
        Err(_) => write!(f, "(non-utf8: {:?})", b),
      }
    }
  }

  impl ChimeraExpression {
    /// Reference the underlying bytes, *without* the trailing null terminator.
    ///
    ///```
    /// # fn main() -> Result<(), vectorscan::error::chimera::ChimeraError> {
    /// let e = vectorscan::expression::chimera::ChimeraExpression::new("asd(f+)")?;
    /// assert_eq!(e.as_bytes(), b"asd(f+)");
    /// # Ok(())
    /// # }
    /// ```
    pub fn as_bytes(&self) -> &[u8] { self.0.as_bytes() }

    pub(crate) fn as_ptr(&self) -> *const c_char { self.0.as_c_str().as_ptr() }

    /// Produce a `NULL`-terminated C-style wrapper for the given pattern
    /// string.
    ///
    /// This will fail if the string contains any internal `NULL` bytes, as
    /// those are not supported by the chimera library:
    ///```
    /// use vectorscan::{expression::chimera::*, error::chimera::*};
    ///
    /// let pat = "as\0df";
    /// let e = match ChimeraExpression::new(pat) {
    ///    Err(ChimeraCompileError::NullByte(e)) => e,
    ///    _ => unreachable!(),
    /// };
    /// assert_eq!(e.nul_position(), 2);
    /// ```
    pub fn new(x: impl Into<Vec<u8>>) -> Result<Self, ChimeraCompileError> {
      Ok(Self(CString::new(x)?))
    }

    /// Call [`ChimeraDb::compile()`] with [`None`] for the platform.
    pub fn compile(
      &self,
      flags: ChimeraFlags,
      mode: ChimeraMode,
    ) -> Result<ChimeraDb, ChimeraCompileError> {
      ChimeraDb::compile(self, flags, mode, None)
    }
  }

  impl str::FromStr for ChimeraExpression {
    type Err = ChimeraCompileError;

    fn from_str(s: &str) -> Result<Self, Self::Err> { Self::new(s) }
  }

  /// Extended configuration for the PCRE matching phase of chimera.
  ///
  /// The only entry point to configuring this is
  /// [`ChimeraExpressionSet::with_limits()`].
  #[derive(Debug, Copy, Clone)]
  pub struct ChimeraMatchLimits {
    /// A limit from pcre_extra on the amount of match function called in PCRE
    /// to limit backtracking that can take place.
    pub match_limit: c_ulong,
    /// A limit from pcre_extra on the recursion depth of match function in
    /// PCRE.
    pub match_limit_recursion: c_ulong,
  }

  /// Collection of regular expressions.
  ///
  /// This is the analogue to [`super::ExpressionSet`] for [`ChimeraExpression`]
  /// instances.
  ///
  /// This struct provides an immutable (returning `Self`) builder interface
  /// to attach additional configuration to the initial set of patterns
  /// constructed with [`Self::from_exprs()`].
  #[derive(Clone)]
  pub struct ChimeraExpressionSet<'a> {
    ptrs: Vec<*const c_char>,
    flags: Option<Vec<ChimeraFlags>>,
    ids: Option<Vec<ExprId>>,
    limits: Option<ChimeraMatchLimits>,
    _ph: PhantomData<&'a u8>,
  }

  impl<'a> fmt::Debug for ChimeraExpressionSet<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
      let exprs: Vec<&'a CStr> = self
        .ptrs
        .iter()
        .map(|p| unsafe { CStr::from_ptr(*p) })
        .collect();
      write!(
        f,
        "ChimeraExpressionSet(exprs={:?}, flags={:?}, ids={:?}, limits={:?})",
        exprs, &self.flags, &self.ids, &self.limits
      )
    }
  }

  impl<'a> ChimeraExpressionSet<'a> {
    /// Construct a pattern set from references to parsed expressions.
    ///
    /// The length of this initial `exprs` argument is returned by
    /// [`Self::len()`], and all subsequent configuration methods are checked to
    /// provide iterators of the same length:
    ///
    ///```should_panic
    /// use vectorscan::expression::chimera::*;
    ///
    /// let a: ChimeraExpression = "a+".parse().unwrap();
    /// // Fails due to argument length mismatch:
    /// ChimeraExpressionSet::from_exprs([&a])
    ///   .with_flags([]);
    /// ```
    pub fn from_exprs(exprs: impl IntoIterator<Item=&'a ChimeraExpression>) -> Self {
      Self {
        ptrs: exprs.into_iter().map(|e| e.as_ptr()).collect(),
        flags: None,
        ids: None,
        limits: None,
        _ph: PhantomData,
      }
    }

    /// Provide flags which modify the behavior of each expression.
    ///
    /// The length of `flags` is checked to be the same as [`Self::len()`].
    ///
    /// If this builder method is not used, [`ChimeraFlags::default()`] will be
    /// assigned to all patterns.
    ///
    ///```
    /// # fn main() -> Result<(), vectorscan::error::chimera::ChimeraError> {
    /// use vectorscan::{expression::chimera::*, flags::chimera::*, matchers::chimera::*};
    ///
    /// // Create two expressions to demonstrate separate flags for each pattern:
    /// let a: ChimeraExpression = "a+[^a]".parse()?;
    /// let b: ChimeraExpression = "b+[^b]".parse()?;
    ///
    /// // Get the start of match for one pattern, but not the other:
    /// let db = ChimeraExpressionSet::from_exprs([&a, &b])
    ///   .with_flags([ChimeraFlags::default(), ChimeraFlags::SINGLEMATCH])
    ///   .compile(ChimeraMode::NOGROUPS)?;
    ///
    /// let mut scratch = db.allocate_scratch()?;
    ///
    /// let mut matches: Vec<&str> = Vec::new();
    /// scratch.scan_sync(&db, "aardvark imbibbe".into(), |m| {
    ///   matches.push(unsafe { m.source.as_str() });
    ///   ChimeraMatchResult::Continue
    /// }, |_| ChimeraMatchResult::Continue)?;
    /// // SINGLEMATCH is preserved for only one pattern:
    /// assert_eq!(&matches, &["aar", "ar", "bi"]);
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_flags(mut self, flags: impl IntoIterator<Item=ChimeraFlags>) -> Self {
      let flags: Vec<_> = flags.into_iter().collect();
      assert_eq!(self.len(), flags.len());
      self.flags = Some(flags);
      self
    }

    /// Assign an ID number to each pattern.
    ///
    /// The length of `ids` is checked to be the same as [`Self::len()`].
    /// Multiple patterns can be assigned the same ID.
    ///
    /// If this builder method is not used, vectorscan will assign them all the
    /// ID number 0:
    ///
    ///```
    /// # fn main() -> Result<(), vectorscan::error::chimera::ChimeraError> {
    /// use vectorscan::{sources::*, expression::{*, chimera::*}, flags::chimera::*, state::chimera::*, matchers::{*, chimera::*}};
    ///
    /// // Create two expressions to demonstrate multiple pattern IDs.
    /// let a: ChimeraExpression = "a+[^a]".parse()?;
    /// let b: ChimeraExpression = "b+[^b]".parse()?;
    ///
    /// // Create one db with ID numbers, and one without.
    /// let set1 = ChimeraExpressionSet::from_exprs([&a, &b]).compile(ChimeraMode::NOGROUPS)?;
    /// let set2 = ChimeraExpressionSet::from_exprs([&a, &b])
    ///   .with_ids([ExprId(300), ExprId(12)])
    ///   .compile(ChimeraMode::NOGROUPS)?;
    ///
    /// let mut scratch = ChimeraScratch::blank();
    /// scratch.setup_for_db(&set1)?;
    /// scratch.setup_for_db(&set2)?;
    ///
    /// let msg: ByteSlice = "aardvark imbibbe".into();
    ///
    /// // The first db doesn't differentiate matches by ID number:
    /// let mut matches1: Vec<ExpressionIndex> = Vec::new();
    /// scratch.scan_sync(&set1, msg, |m| {
    ///   matches1.push(m.id);
    ///   ChimeraMatchResult::Continue
    /// }, |_| ChimeraMatchResult::Continue)?;
    /// assert_eq!(
    ///   &matches1,
    ///   &[ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0)],
    /// );
    ///
    /// // The second db returns corresponding ExpressionIndex instances:
    /// let mut matches2: Vec<ExpressionIndex> = Vec::new();
    /// scratch.scan_sync(&set2, msg, |m| {
    ///   matches2.push(m.id);
    ///   ChimeraMatchResult::Continue
    /// }, |_| ChimeraMatchResult::Continue)?;
    /// assert_eq!(
    ///   &matches2,
    ///   &[ExpressionIndex(300), ExpressionIndex(300), ExpressionIndex(12), ExpressionIndex(12)],
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_ids(mut self, ids: impl IntoIterator<Item=ExprId>) -> Self {
      let ids: Vec<_> = ids.into_iter().collect();
      assert_eq!(self.len(), ids.len());
      self.ids = Some(ids);
      self
    }

    /// Assign extended PCRE configuration to the entire pattern set.
    ///
    /// This is the only entry point to configuring PCRE match limits (i.e. the
    /// single-pattern compiler does not support match limits).
    ///
    ///```
    /// # fn main() -> Result<(), vectorscan::error::chimera::ChimeraError> {
    /// use vectorscan::{sources::*, expression::chimera::*, flags::chimera::*, state::chimera::*, matchers::chimera::*, error::chimera::*};
    ///
    /// // Create one db with backtracking match limits, and one without.
    /// let a: ChimeraExpression = r"(asdf?)hey\1".parse()?;
    /// let set1 = ChimeraExpressionSet::from_exprs([&a]).compile(ChimeraMode::GROUPS)?;
    /// let set2 = ChimeraExpressionSet::from_exprs([&a])
    ///   .with_limits(ChimeraMatchLimits { match_limit: 1, match_limit_recursion: 1 })
    ///   .compile(ChimeraMode::GROUPS)?;
    ///
    /// let mut scratch = ChimeraScratch::blank();
    /// scratch.setup_for_db(&set1)?;
    /// scratch.setup_for_db(&set2)?;
    ///
    /// let msg: ByteSlice = "asdfheyasdf".into();
    ///
    /// // The first db doesn't stop the matching engine:
    /// let mut matches1: Vec<&str> = Vec::new();
    /// scratch.scan_sync(&set1, msg, |m| {
    ///   matches1.push(unsafe { m.captures.unwrap()[1].unwrap().as_str() });
    ///   ChimeraMatchResult::Continue
    /// }, |_| ChimeraMatchResult::Terminate)?;
    /// assert_eq!(&matches1, &["asdf"]);
    ///
    /// // The second db imposes a match limit, which triggers the second callback to return
    /// // `ChimeraMatchResult::Terminate`.
    /// let mut matches2: Vec<ChimeraMatchError> = Vec::new();
    /// let result = scratch.scan_sync(
    ///   &set2,
    ///   msg,
    ///   |_| unreachable!(),
    ///   |e| {
    ///     matches2.push(e);
    ///     ChimeraMatchResult::Terminate
    ///   },
    /// );
    /// assert!(matches![result, Err(ChimeraRuntimeError::ScanTerminated)]);
    /// assert_eq!(matches2.len(), 1);
    /// assert_eq!(matches2[0].error_type, ChimeraMatchErrorType::MatchLimit);
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_limits(mut self, limits: ChimeraMatchLimits) -> Self {
      self.limits = Some(limits);
      self
    }

    /// Call [`ChimeraDb::compile_multi()`] with [`None`] for the platform.
    pub fn compile(self, mode: ChimeraMode) -> Result<ChimeraDb, ChimeraCompileError> {
      ChimeraDb::compile_multi(&self, mode, None)
    }

    /// The number of patterns in this set.
    pub fn len(&self) -> usize { self.ptrs.len() }

    /// Whether this set contains any patterns.
    pub fn is_empty(&self) -> bool { self.len() == 0 }

    pub(crate) fn limits(&self) -> Option<ChimeraMatchLimits> { self.limits }

    pub(crate) fn num_elements(&self) -> c_uint { self.len() as c_uint }

    pub(crate) fn expressions_ptr(&self) -> *const *const c_char { self.ptrs.as_ptr() }

    pub(crate) fn flags_ptr(&self) -> *const c_uint {
      self
        .flags
        .as_ref()
        .map(|f| unsafe { mem::transmute(f.as_ptr()) })
        .unwrap_or(ptr::null())
    }

    pub(crate) fn ids_ptr(&self) -> *const c_uint {
      self
        .ids
        .as_ref()
        .map(|i| unsafe { mem::transmute(i.as_ptr()) })
        .unwrap_or(ptr::null())
    }
  }
}