re2 0.0.11

Wrapper for the re2 C++ regex library.
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
/* Copyright 2022-2024 Danny McClanahan */
/* SPDX-License-Identifier: BSD-3-Clause */

//! Rust interface to the re2 regular-expression library. RE2 supports
//! Perl-style regular expressions (with extensions like `\d`, `\w`, `\s`, ...).
//!
//! # Regexp Syntax
//!
//! This module uses the `re2` library and hence supports
//! its syntax for regular expressions, which is similar to Perl's with
//! some of the more complicated things thrown away.  In particular,
//! backreferences and generalized assertions are not available, nor is `\Z`.
//!
//! See [Syntax] for the syntax supported by RE2, and a comparison with PCRE and
//! PERL regexps.
//!
//! [Syntax]: https://github.com/google/re2/wiki/Syntax
//!
//! For those not familiar with Perl's regular expressions,
//! here are some examples of the most commonly used extensions:
//!
//! - `"hello (\\w+) world"`  -- `\w` matches a "word" character
//! - `"version (\\d+)"`      -- `\d` matches a digit
//! - `"hello\\s+world"`      -- `\s` matches any whitespace character
//! - `"\\b(\\w+)\\b"`        -- `\b` matches non-empty string at word boundary
//! - `"(?i)hello"`           -- `(?i)` turns on case-insensitive matching
//! - `"/\\*(.*?)\\*/"`       -- `.*?` matches `.` minimum number of times
//!   possible
//!
//! The double backslashes are needed when writing string literals.
//! However, they should NOT be used when writing raw string literals:
//!
//! - `r"(hello (\w+) world)"`  -- `\w` matches a "word" character
//! - `r"(version (\d+))"`      -- `\d` matches a digit
//! - `r"(hello\s+world)"`      -- `\s` matches any whitespace character
//! - `r"(\b(\w+)\b)"`          -- `\b` matches non-empty string at word
//!   boundary
//! - `r"((?i)hello)"`          -- `(?i)` turns on case-insensitive matching
//! - `r"(/\*(.*?)\*/)"`        -- `.*?` matches `.` minimum number of times
//!   possible
//!
//! When using UTF-8 encoding, case-insensitive matching will perform
//! simple case folding, not full case folding.

// Warn for missing docs in general, and hard require crate-level docs.
#![warn(missing_docs)]
#![deny(rustdoc::missing_crate_level_docs)]
/* Make all doctests fail if they produce any warnings. */
#![doc(test(attr(deny(warnings))))]

pub(crate) use re2_sys::{re2, re2_c};

pub mod error;
pub use error::RE2Error;
use error::{CompileError, RE2ErrorCode, RewriteError};

pub mod options;
pub use options::{Anchor, CannedOptions, Options};

pub mod string;
use string::{StringView, StringWrapper};

pub mod set;

pub mod filtered;

use std::{
  cmp, fmt, hash,
  marker::PhantomData,
  mem::{self, MaybeUninit},
  ops, ptr, str,
};

/* TODO: use MaybeUninit::uninit_array()! */
fn uninit_array<T, const N: usize>() -> [MaybeUninit<T>; N] {
  unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() }
}

/* TODO: use MaybeUninit::array_assume_init()! */
unsafe fn array_assume_init<T: Sized, const N: usize>(x: [MaybeUninit<T>; N]) -> [T; N] {
  let x: *const [MaybeUninit<T>; N] = &x;
  let y: *const [T; N] = mem::transmute(x);
  ptr::read(y)
}

fn map_array<T, U, const N: usize, F: Fn(T) -> U>(argv: [T; N], f: F) -> [U; N] {
  let mut ret: [MaybeUninit<U>; N] = uninit_array();
  for (output, input) in ret.iter_mut().zip(argv.into_iter()) {
    output.write(f(input));
  }
  unsafe { array_assume_init(ret) }
}

/// High-level string search and replacement with a single pattern.
///
///```
/// # fn main() -> Result<(), re2::RE2Error> {
/// use re2::RE2;
///
/// let r: RE2 = "a(.+)f".parse()?;
/// let [m] = r.full_match_capturing("asdf").unwrap();
/// assert_eq!(m, "sd");
/// # Ok(())
/// # }
/// ```
#[repr(transparent)]
pub struct RE2(re2_c::RE2Wrapper);

/// # RE2 = (pattern, options)
/// Basic components of `RE2` objects. [`Self::quote_meta()`] is a common
/// utility method to construct pattern strings.
impl RE2 {
  /// Compile an `RE2` pattern.
  ///
  /// A [`FromStr`](str::FromStr) implementation is also provided which calls
  /// this method with [`Options::default()`], enabling the use of
  /// [`str::parse()`]:
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::{RE2, options::Options};
  ///
  /// let r = RE2::compile("asdf".into(), Options::default())?;
  /// assert!(r.full_match("asdf"));
  ///
  /// let r2: RE2 = "asdf".parse()?;
  /// assert_eq!(r, r2);
  /// # Ok(())
  /// # }
  /// ```
  ///
  /// The error contains the original pattern string as well as a description of
  /// the compile failure:
  ///```
  /// use re2::{RE2, error::*};
  ///
  /// assert_eq!(
  ///   "a(sdf".parse::<RE2>().err().unwrap(),
  ///   CompileError {
  ///     message: "missing ): a(sdf".to_string(),
  ///     arg: "a(sdf".to_string(),
  ///     code: RE2ErrorCode::MissingParen,
  ///   },
  /// );
  /// ```
  pub fn compile(pattern: &str, options: Options) -> Result<Self, CompileError> {
    let pattern = StringView::from_str(pattern);
    let s = Self(unsafe { re2_c::RE2Wrapper::new(pattern.into_native(), &options.into_native()) });
    s.check_error()?;
    Ok(s)
  }

  fn check_error_code(&self) -> Result<(), RE2ErrorCode> {
    RE2ErrorCode::from_native(unsafe { self.0.error_code() })
  }

  /// Extract the pattern string provided to the compiler.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "asdf".parse()?;
  /// assert_eq!(r.pattern(), "asdf");
  /// # Ok(())
  /// # }
  /// ```
  pub fn pattern(&self) -> &str { unsafe { StringView::from_native(self.0.pattern()).as_str() } }

  /// Extract the options object provided to the compiler.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::{RE2, options::*};
  ///
  /// let o: Options = CannedOptions::POSIX.into();
  /// let r = RE2::compile("asdf".into(), o)?;
  /// assert_eq!(o, r.options());
  /// assert_ne!(o, Options::default());
  /// # Ok(())
  /// # }
  /// ```
  pub fn options(&self) -> Options { unsafe { *self.0.options() }.into() }

  /// Create a new instance of this `RE2` with the same semantics.
  ///
  /// This method is largely intended for documentation purposes; [`Clone`] is
  /// not implemented because of several performance reasons to re-use the
  /// same instance, e.g. with an [`Arc`](std::sync::Arc).
  ///
  /// The matching implementation may use a lazy NFA, which is partially
  /// constructed as it is matched against input strings, so it improves
  /// performance to use a reference to the same instance where possible, to
  /// avoid reconstructing the same NFA components. Upfront compilation of this
  /// NFA is also somewhat expensive in itself (even if less expensive than a
  /// DFA), and best to avoid repeating.
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::{RE2, options::*};
  ///
  /// let o: Options = CannedOptions::POSIX.into();
  /// let r = RE2::compile("asdf".into(), o)?;
  /// assert_eq!(o, r.options());
  /// assert_ne!(o, Options::default());
  ///
  /// let r2 = r.expensive_clone();
  /// assert_eq!(&r, &r2);
  /// # Ok(())
  /// # }
  /// ```
  pub fn expensive_clone(&self) -> Self { Self::compile(self.pattern(), self.options()).unwrap() }

  fn error(&self) -> StringView { unsafe { StringView::from_native(self.0.error()) } }

  fn error_arg(&self) -> StringView { unsafe { StringView::from_native(self.0.error_arg()) } }

  fn check_error(&self) -> Result<(), CompileError> {
    self.check_error_code().map_err(|code| CompileError {
      message: String::from_utf8_lossy(self.error().as_slice()).to_string(),
      arg: String::from_utf8_lossy(self.error_arg().as_slice()).to_string(),
      code,
    })
  }

  /// Escape any metacharacters in `pattern`.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::RE2;
  ///
  /// let q = RE2::quote_meta("1.5-1.8?".into());
  /// let r: RE2 = unsafe { q.as_view().as_str() }.parse()?;
  /// assert_eq!(r"1\.5\-1\.8\?", r.pattern());
  /// assert!(r.full_match("1.5-1.8?"));
  /// # Ok(())
  /// # }
  /// ```
  ///
  /// Note that literal patterns can be used instead in some cases:
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::{RE2, options::Options};
  ///
  /// let o = Options { literal: true, ..Default::default() };
  /// let r = RE2::compile("1.5-1.8?".into(), o)?;
  /// assert_eq!("1.5-1.8?", r.pattern());
  /// assert!(r.full_match("1.5-1.8?"));
  /// # Ok(())
  /// # }
  /// ```
  pub fn quote_meta(pattern: &str) -> StringWrapper {
    let pattern = StringView::from_str(pattern);
    let mut out = StringWrapper::from_view(pattern);
    unsafe { re2_c::RE2Wrapper::quote_meta(pattern.into_native(), out.as_mut_native()) };
    out
  }
}

impl str::FromStr for RE2 {
  type Err = CompileError;

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

/// Deletes the underlying C++ object on drop.
impl ops::Drop for RE2 {
  fn drop(&mut self) {
    unsafe {
      self.0.clear();
    }
  }
}

/// # Matching
/// Matching methods tend to have a few variants:
/// - Methods with a `*_capturing` suffix will return a variable array of
///   strings corresponding to matching capture groups. For these methods,
///   requesting more groups than the result of [`Self::num_captures()`] will
///   immediately return [`None`] without performing the search.
///
/// [`Self::match_routine()`] also returns a variable array of strings, but
/// presents a slightly different interface. It is the most general matching
/// entry point along with [`Self::match_no_captures()`] and is suitable for
/// building higher-level matching interfaces.
impl RE2 {
  fn empty_result<'a, const N: usize>() -> [StringView<'a>; N] {
    assert_eq!(N, 0);
    let ret: [MaybeUninit<StringView<'a>>; N] = uninit_array();
    unsafe { array_assume_init(ret) }
  }

  fn convert_string_views<'a, const N: usize>(argv: [re2_c::StringView; N]) -> [StringView<'a>; N] {
    map_array(argv, StringView::from_native)
  }

  fn convert_strings<const N: usize>(argv: [StringView; N]) -> [&str; N] {
    map_array(argv, |s| unsafe { s.as_str() })
  }

  fn convert_from_strings<const N: usize>(argv: [&str; N]) -> [StringView; N] {
    map_array(argv, StringView::from_str)
  }

  pub(crate) fn full_match_view(&self, text: StringView) -> bool {
    unsafe { self.0.full_match(text.into_native()) }
  }

  /// Match against `text` without capturing. Pattern must match entire string.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a.df".parse()?;
  /// assert!(r.full_match("asdf"));
  /// assert!(!r.full_match("asdfe"));
  /// assert!(!r.full_match("basdf"));
  /// # Ok(())
  /// # }
  /// ```
  pub fn full_match(&self, text: &str) -> bool { self.full_match_view(StringView::from_str(text)) }

  pub(crate) fn full_match_capturing_view<'a, const N: usize>(
    &self,
    text_view: StringView<'a>,
  ) -> Option<[StringView<'a>; N]> {
    if N == 0 {
      return if self.full_match_view(text_view) {
        Some(Self::empty_result())
      } else {
        None
      };
    }
    if N > self.num_captures() {
      return None;
    }

    let mut argv: [MaybeUninit<re2_c::StringView>; N] = uninit_array();

    if !unsafe {
      self.0.full_match_n(
        text_view.into_native(),
        /* TODO: use MaybeUninit::slice_as_mut_ptr! */
        mem::transmute(argv.as_mut_ptr()),
        argv.len(),
      )
    } {
      return None;
    }

    Some(unsafe { Self::convert_string_views(array_assume_init(argv)) })
  }

  /// Match against `text` and return a subset of declared capture groups.
  /// Pattern must match entire string.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a(.)d(f)".parse()?;
  /// assert_eq!(2, r.num_captures());
  ///
  /// let msg = "asdf";
  /// // The 0 case still works, but just calls .full_match():
  /// assert!(r.full_match_capturing::<0>(msg).is_some());
  ///
  /// let [s1, s2] = r.full_match_capturing(msg).unwrap();
  /// assert_eq!(s1, "s");
  /// assert_eq!(s2, "f");
  /// // The result isn't copied, it points to the same memory:
  /// assert_eq!(s1.as_bytes().as_ptr(), msg[1..].as_bytes().as_ptr());
  /// assert_eq!(s2.as_bytes().as_ptr(), msg[3..].as_bytes().as_ptr());
  /// # Ok(())
  /// # }
  /// ```
  pub fn full_match_capturing<'a, const N: usize>(&self, text: &'a str) -> Option<[&'a str; N]> {
    self
      .full_match_capturing_view(StringView::from_str(text))
      .map(Self::convert_strings)
  }

  pub(crate) fn partial_match_view(&self, text: StringView) -> bool {
    unsafe { self.0.partial_match(text.into_native()) }
  }

  /// Like [`Self::full_match()`], except that the pattern may match a substring
  /// of `text`.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a.df".parse()?;
  /// assert!(r.partial_match("asdf"));
  /// assert!(r.partial_match("asdfe"));
  /// assert!(r.partial_match("basdf"));
  /// assert!(!r.partial_match("ascf"));
  /// # Ok(())
  /// # }
  /// ```
  pub fn partial_match(&self, text: &str) -> bool {
    self.partial_match_view(StringView::from_str(text))
  }

  pub(crate) fn partial_match_capturing_view<'a, const N: usize>(
    &self,
    text_view: StringView<'a>,
  ) -> Option<[StringView<'a>; N]> {
    if N == 0 {
      return if self.partial_match_view(text_view) {
        Some(Self::empty_result())
      } else {
        None
      };
    }
    if N > self.num_captures() {
      return None;
    }

    let mut argv: [MaybeUninit<re2_c::StringView>; N] = uninit_array();

    if !unsafe {
      self.0.partial_match_n(
        text_view.into_native(),
        /* TODO: use MaybeUninit::slice_as_mut_ptr! */
        mem::transmute(argv.as_mut_ptr()),
        argv.len(),
      )
    } {
      return None;
    }

    Some(unsafe { Self::convert_string_views(array_assume_init(argv)) })
  }

  /// Match against `text` and return a subset of declared capture groups.
  ///
  /// Like [`Self::full_match_capturing()`], except that the pattern may match a
  /// substring of `text`.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::*;
  ///
  /// let o: Options = CannedOptions::POSIX.into();
  /// let r = RE2::compile("a(.+)d(f)".into(), o)?;
  /// assert_eq!(2, r.num_captures());
  ///
  /// let msg = "the asdf is withdfin me";
  /// // The 0 case still works, but just calls .partial_match():
  /// assert!(r.partial_match_capturing::<0>(msg).is_some());
  ///
  /// let [s1, s2] = r.partial_match_capturing(msg).unwrap();
  /// assert_eq!(s1, "sdf is with");
  /// assert_eq!(s2, "f");
  /// // The result isn't copied, it points to the same memory:
  /// assert_eq!(s1.as_bytes().as_ptr(), msg[5..].as_bytes().as_ptr());
  /// assert_eq!(s2.as_bytes().as_ptr(), msg[17..].as_bytes().as_ptr());
  /// # Ok(())
  /// # }
  /// ```
  pub fn partial_match_capturing<'a, const N: usize>(&self, text: &'a str) -> Option<[&'a str; N]> {
    self
      .partial_match_capturing_view(StringView::from_str(text))
      .map(Self::convert_strings)
  }

  pub(crate) fn consume_view(&self, text_view: &mut StringView) -> bool {
    if !unsafe { self.0.consume(text_view.as_mut_native()) } {
      return false;
    }
    true
  }

  /// If the pattern matches some prefix of `text`, advance `text` past the
  /// match.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a.{2}".parse()?;
  /// let mut s = "asdf";
  /// assert!(r.consume(&mut s));
  /// assert_eq!(s, "f");
  /// # Ok(())
  /// # }
  /// ```
  pub fn consume(&self, text: &mut &str) -> bool {
    let mut text_view = StringView::from_str(text);
    let ret = self.consume_view(&mut text_view);
    if ret {
      *text = unsafe { text_view.as_str() };
    }
    ret
  }

  pub(crate) fn consume_capturing_view<'a, const N: usize>(
    &self,
    text_view: &mut StringView<'a>,
  ) -> Option<[StringView<'a>; N]> {
    if N == 0 {
      return if self.consume_view(text_view) {
        Some(Self::empty_result())
      } else {
        None
      };
    }
    if N > self.num_captures() {
      return None;
    }

    let mut argv: [MaybeUninit<re2_c::StringView>; N] = uninit_array();

    if !unsafe {
      self.0.consume_n(
        text_view.as_mut_native(),
        /* TODO: use MaybeUninit::slice_as_mut_ptr! */
        mem::transmute(argv.as_mut_ptr()),
        argv.len(),
      )
    } {
      return None;
    }

    Some(unsafe { Self::convert_string_views(array_assume_init(argv)) })
  }

  /// If the pattern matches some prefix of `text`, advance `text` past the
  /// match and return some subset of captured sub-patterns.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a(.)d(f)".parse()?;
  /// assert_eq!(2, r.num_captures());
  ///
  /// let mut msg = "asdfasdfe";
  /// // The 0 case still works, but just calls .consume():
  /// assert!(r.consume_capturing::<0>(&mut msg).is_some());
  /// assert_eq!(msg, "asdfe");
  ///
  /// let [s1, s2] = r.consume_capturing(&mut msg).unwrap();
  /// assert_eq!(s1, "s");
  /// assert_eq!(s2, "f");
  /// assert_eq!(msg, "e");
  /// # Ok(())
  /// # }
  /// ```
  pub fn consume_capturing<'a, const N: usize>(&self, text: &mut &'a str) -> Option<[&'a str; N]> {
    let mut text_view = StringView::from_str(text);
    let ret = self.consume_capturing_view(&mut text_view);
    if ret.is_some() {
      *text = unsafe { text_view.as_str() };
    }
    ret.map(Self::convert_strings)
  }

  pub(crate) fn find_and_consume_view(&self, text_view: &mut StringView) -> bool {
    if !unsafe { self.0.find_and_consume(text_view.as_mut_native()) } {
      return false;
    }
    true
  }

  /// If the pattern matches anywhere in `text`, advance `text` past the match.
  ///
  /// Like [`Self::consume()`], but does not anchor the match at the beginning
  /// of the text.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a.{2}".parse()?;
  /// let mut s = "the asdf";
  /// assert!(r.find_and_consume(&mut s));
  /// assert_eq!(s, "f");
  /// # Ok(())
  /// # }
  /// ```
  pub fn find_and_consume(&self, text: &mut &str) -> bool {
    let mut text_view = StringView::from_str(text);
    let ret = self.find_and_consume_view(&mut text_view);
    if ret {
      *text = unsafe { text_view.as_str() };
    }
    ret
  }

  pub(crate) fn find_and_consume_capturing_view<'a, const N: usize>(
    &self,
    text_view: &mut StringView<'a>,
  ) -> Option<[StringView<'a>; N]> {
    if N == 0 {
      return if self.find_and_consume_view(text_view) {
        Some(Self::empty_result())
      } else {
        None
      };
    }
    if N > self.num_captures() {
      return None;
    }

    let mut argv: [MaybeUninit<re2_c::StringView>; N] = uninit_array();

    if !unsafe {
      self.0.find_and_consume_n(
        text_view.as_mut_native(),
        /* TODO: use MaybeUninit::slice_as_mut_ptr! */
        mem::transmute(argv.as_mut_ptr()),
        argv.len(),
      )
    } {
      return None;
    }

    Some(unsafe { Self::convert_string_views(array_assume_init(argv)) })
  }

  /// If the pattern matches anywhere in `text`, advance `text` past the match
  /// and return some subset of captured sub-patterns.
  ///
  /// Like [`Self::consume_capturing()`], but does not anchor the match at the
  /// beginning of the text.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a(.)d(f)".parse()?;
  /// assert_eq!(2, r.num_captures());
  ///
  /// let mut msg = "the asdf and the asdfe";
  /// // The 0 case still works, but just calls .find_and_consume():
  /// assert!(r.find_and_consume_capturing::<0>(&mut msg).is_some());
  /// assert_eq!(msg, " and the asdfe");
  ///
  /// let [s1, s2] = r.find_and_consume_capturing(&mut msg).unwrap();
  /// assert_eq!(s1, "s");
  /// assert_eq!(s2, "f");
  /// assert_eq!(msg, "e");
  /// # Ok(())
  /// # }
  /// ```
  pub fn find_and_consume_capturing<'a, const N: usize>(
    &self,
    text: &mut &'a str,
  ) -> Option<[&'a str; N]> {
    let mut text_view = StringView::from_str(text);
    let ret = self.find_and_consume_capturing_view(&mut text_view);
    if ret.is_some() {
      *text = unsafe { text_view.as_str() };
    }
    ret.map(Self::convert_strings)
  }

  pub(crate) fn match_no_captures_view(
    &self,
    text: StringView,
    range: ops::Range<usize>,
    anchor: Anchor,
  ) -> bool {
    let ops::Range { start, end } = range;

    unsafe {
      self
        .0
        .match_single(text.into_native(), start, end, anchor.into_native())
    }
  }

  /// General matching routine. Suitable for calling from higher-level code.
  ///
  /// Match against `text` within `range`, taking into account `anchor`. Returns
  /// [`true`] if match found, [`false`] if not.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::{RE2, options::Anchor};
  ///
  /// let r: RE2 = "(foo)|(bar)baz".parse()?;
  /// let msg = "barbazbla";
  ///
  /// assert!(r.match_no_captures(msg, 0..msg.len(), Anchor::Unanchored));
  /// assert!(r.match_no_captures(msg, 0..msg.len(), Anchor::AnchorStart));
  /// assert!(!r.match_no_captures(msg, 0..msg.len(), Anchor::AnchorBoth));
  /// assert!(r.match_no_captures(msg, 0..6, Anchor::AnchorBoth));
  /// assert!(!r.match_no_captures(msg, 1..msg.len(), Anchor::Unanchored));
  /// # Ok(())
  /// # }
  /// ```
  pub fn match_no_captures(&self, text: &str, range: ops::Range<usize>, anchor: Anchor) -> bool {
    self.match_no_captures_view(StringView::from_str(text), range, anchor)
  }

  pub(crate) fn match_routine_view<'a, const N: usize>(
    &self,
    text_view: StringView<'a>,
    range: ops::Range<usize>,
    anchor: Anchor,
  ) -> Option<[StringView<'a>; N]> {
    if N == 0 {
      return if self.match_no_captures_view(text_view, range, anchor) {
        Some(Self::empty_result())
      } else {
        None
      };
    }
    let ops::Range { start, end } = range;
    let mut submatches: [MaybeUninit<re2_c::StringView>; N] = uninit_array();

    if !unsafe {
      self.0.match_routine(
        text_view.into_native(),
        start,
        end,
        anchor.into_native(),
        /* TODO: use MaybeUninit::slice_as_mut_ptr! */
        mem::transmute(submatches.as_mut_ptr()),
        submatches.len(),
      )
    } {
      return None;
    }

    Some(Self::convert_string_views(unsafe {
      array_assume_init(submatches)
    }))
  }

  /// General matching routine with capture groups. Suitable for calling from
  /// higher-level code.
  ///
  /// Match against `text` within `range`, taking into account `anchor`. Returns
  /// [`Some`] if match found, [`None`] if not.
  ///
  /// NB: Unlike other matching methods, the 0th element of the result
  /// corresponds to the entire matched text, so the indices of the returned
  /// array correspond to the indices of declared capture groups e.g. from
  /// [`Self::named_groups()`]. Also unlike other matching methods, requesting
  /// more captures than the number of declared capture groups will simply
  /// return empty strings for the excessive captures instead of failing the
  /// match.
  ///
  /// Performance-wise, `N == 0` (capturing nothing, like
  /// [`Self::match_no_captures()`]) is much faster than `N == 1` (only
  /// capturing the entire match text), which is faster than `N > 1` (if any
  /// capture groups are selected).
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::{RE2, options::Anchor};
  ///
  /// let r: RE2 = "(foo)|(bar)baz".parse()?;
  /// let msg = "barbazbla";
  ///
  /// let [s0, s1, s2, s3] = r.match_routine(msg, 0..msg.len(), Anchor::AnchorStart).unwrap();
  /// assert_eq!(s0, "barbaz");
  /// assert_eq!(s1, "");
  /// assert_eq!(s2, "bar");
  /// assert_eq!(s3, "");
  /// # Ok(())
  /// # }
  /// ```
  pub fn match_routine<'a, const N: usize>(
    &self,
    text: &'a str,
    range: ops::Range<usize>,
    anchor: Anchor,
  ) -> Option<[&'a str; N]> {
    self
      .match_routine_view(StringView::from_str(text), range, anchor)
      .map(Self::convert_strings)
  }
}

/// # String Replacement with a Rewrite String
/// These methods use a mutable [`StringWrapper`] instance with dynamically
/// allocated data to record the result of applying a "rewrite string" to the
/// capture groups for a given match using [`Self::vector_rewrite()`]. They may
/// mutate data from the string or append to the string upon a successful match.
///
/// Within a rewrite string, backslash-escaped digits (`\1` to `\9`) can be
/// used to insert text matching corresponding parenthesized group
/// from the pattern. `\0` refers to the entire matched text:
///```
/// # fn main() -> Result<(), re2::RE2Error> {
/// use re2::{RE2, string::StringWrapper};
///
/// let r: RE2 = "b+".parse()?;
/// let mut s = StringWrapper::from_view("yabba dabba doo".into());
///
/// assert!(r.replace(&mut s, "d"));
/// assert_eq!("yada dabba doo", unsafe { s.as_view().as_str() });
///
/// assert!(r.replace(&mut s, r"!\0!"));
/// assert_eq!("yada da!bb!a doo", unsafe { s.as_view().as_str() });
/// # Ok(())
/// # }
/// ```
impl RE2 {
  pub(crate) fn replace_view(&self, text: &mut StringWrapper, rewrite: StringView) -> bool {
    unsafe { self.0.replace(text.as_mut_native(), rewrite.into_native()) }
  }

  /// Replace the first match of this pattern in `text` with `rewrite`.
  ///
  /// Returns [`true`] if the pattern matches and a replacement occurs,
  /// [`false`] otherwise.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = ".he".parse()?;
  /// let mut s = re2::string::StringWrapper::from_view("all the king's men".into());
  /// assert!(r.replace(&mut s, "duh"));
  /// assert_eq!(unsafe { s.as_view().as_str() }, "all duh king's men");
  /// # Ok(())
  /// # }
  /// ```
  pub fn replace(&self, text: &mut StringWrapper, rewrite: &str) -> bool {
    self.replace_view(text, StringView::from_str(rewrite))
  }

  pub(crate) fn replace_n_view(
    &self,
    text: &mut StringWrapper,
    rewrite: StringView,
    limit: usize,
  ) -> usize {
    if limit == 0 {
      self.global_replace_view(text, rewrite)
    } else {
      let mut num_replacements_made: usize = 0;
      while self.replace_view(text, rewrite) {
        num_replacements_made += 1;
      }
      num_replacements_made
    }
  }

  /// Applies [`Self::replace()`] to `text` at most `limit` times. Returns the
  /// number of replacements made.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = ".he".parse()?;
  /// let mut s = re2::string::StringWrapper::from_view(
  ///   "all the king's horses and all the king's men".into());
  /// assert_eq!(2, r.replace_n(&mut s, "duh", 3));
  /// assert_eq!(
  ///   unsafe { s.as_view().as_str() },
  ///   "all duh king's horses and all duh king's men",
  /// );
  /// # Ok(())
  /// # }
  /// ```
  pub fn replace_n(&self, text: &mut StringWrapper, rewrite: &str, limit: usize) -> usize {
    self.replace_n_view(text, StringView::from_str(rewrite), limit)
  }

  pub(crate) fn global_replace_view(&self, text: &mut StringWrapper, rewrite: StringView) -> usize {
    unsafe {
      self
        .0
        .global_replace(text.as_mut_native(), rewrite.into_native())
    }
  }

  /// Applies [`Self::replace()`] to `text` for all non-overlapping occurrences
  /// of the pattern. Returns the number of replacements made.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = ".he".parse()?;
  /// let mut s = re2::string::StringWrapper::from_view(
  ///   "all the king's horses and all the king's men".into());
  /// assert_eq!(2, r.global_replace(&mut s, "duh"));
  /// assert_eq!(
  ///   unsafe { s.as_view().as_str() },
  ///   "all duh king's horses and all duh king's men",
  /// );
  /// # Ok(())
  /// # }
  /// ```
  pub fn global_replace(&self, text: &mut StringWrapper, rewrite: &str) -> usize {
    self.global_replace_view(text, StringView::from_str(rewrite))
  }

  pub(crate) fn extract_view(
    &self,
    text: StringView,
    rewrite: StringView,
    out: &mut StringWrapper,
  ) -> bool {
    unsafe {
      self.0.extract(
        text.into_native(),
        rewrite.into_native(),
        out.as_mut_native(),
      )
    }
  }

  /// Like [`Self::replace()`], except that if the pattern matches, `rewrite` is
  /// copied into `out` with substitutions. The non-matching portions of
  /// `text` are ignored.
  ///
  /// Returns [`true`] iff a match occured and the extraction happened
  /// successfully; if no match occurs, the string is left unaffected.
  ///
  /// **REQUIRES: `text` must not alias any part of `out`!**
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "(.h)e".parse()?;
  /// let mut s = re2::string::StringWrapper::blank();
  /// assert!(r.extract("all the king's men", r"\1a", &mut s));
  /// assert_eq!(unsafe { s.as_view().as_str() }, "tha");
  /// # Ok(())
  /// # }
  /// ```
  pub fn extract(&self, text: &str, rewrite: &str, out: &mut StringWrapper) -> bool {
    self.extract_view(
      StringView::from_str(text),
      StringView::from_str(rewrite),
      out,
    )
  }
}

/// # Iterators
/// These methods make use of lower-level matching methods like
/// [`Self::match_routine()`] to produce lazy streams of results.
impl RE2 {
  pub(crate) fn find_iter_view<'r, 'h: 'r, const N: usize>(
    &'r self,
    hay: StringView<'h>,
  ) -> impl Iterator<Item=[StringView<'h>; N]>+'r {
    assert_ne!(
      N, 0,
      "N must be at least 1 to capture the match text for non-overlapping matches"
    );
    MatchIter {
      remaining_input: hay,
      pattern: self,
    }
  }

  /// Yields all non-overlapping matches in `hay`. Supports capture groups.
  ///
  /// The `N` argument is interpreted the same way as in
  /// [`Self::match_routine()`], where requesting fewer submatches is more
  /// performant, and the 0th submatch corresponds to the entire matched text.
  /// However, note that `N` must be at least 1 when calling this method in
  /// order to record match boundaries to avoid overlaps.
  ///
  /// NB: if no input is consumed upon searching the regex, iteration will
  /// end to avoid looping infinitely.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a+(.)".parse()?;
  ///
  /// let hay = "aardvarks all ailing: awesome";
  /// let whole_matches: Vec<&str> = r.find_iter::<1>(hay).map(|[m]| m).collect();
  /// let submatches: Vec<&str> = r.find_iter::<2>(hay).map(|[_, m]| m).collect();
  /// assert_eq!(&whole_matches, &["aar", "ar", "al", "ai", "aw"]);
  /// assert_eq!(&submatches, &["r", "r", "l", "i", "w"]);
  /// # Ok(())
  /// # }
  /// ```
  pub fn find_iter<'r, 'h: 'r, const N: usize>(
    &'r self,
    hay: &'h str,
  ) -> impl Iterator<Item=[&'h str; N]>+'r {
    self
      .find_iter_view(StringView::from_str(hay))
      .map(Self::convert_strings)
  }

  pub(crate) fn split_view<'r, 'h: 'r>(
    &'r self,
    hay: StringView<'h>,
  ) -> impl Iterator<Item=StringView<'h>>+'r {
    SplitIter {
      remaining_input: Some(hay),
      pattern: self,
    }
  }

  /// Yields all slices between matches of the pattern.
  ///
  /// Split by arbitrary amount of tabs or whitespace:
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = r"[ \t]+".parse()?;
  ///
  /// let hay = "a b \t  c\td    e";
  /// let fields: Vec<&str> = r.split(hay).collect();
  /// assert_eq!(&fields, &["a", "b", "c", "d", "e"]);
  /// # Ok(())
  /// # }
  /// ```
  ///
  /// Multiple contiguous matches yield empty slices:
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "X".parse()?;
  ///
  /// let hay = "XXXXaXXbXc";
  /// let chunks: Vec<&str> = r.split(hay).collect();
  /// assert_eq!(&chunks, &["", "", "", "", "a", "", "b", "c"]);
  /// # Ok(())
  /// # }
  /// ```
  ///
  /// Separators at start or end also yield empty slices:
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "0".parse()?;
  ///
  /// let hay = "010";
  /// let chunks: Vec<&str> = r.split(hay).collect();
  /// assert_eq!(&chunks, &["", "1", ""]);
  /// # Ok(())
  /// # }
  /// ```
  pub fn split<'r, 'h: 'r>(&'r self, hay: &'h str) -> impl Iterator<Item=&'h str>+'r {
    self
      .split_view(StringView::from_str(hay))
      .map(|s| unsafe { s.as_str() })
  }
}

/// # Introspection
/// Introspection methods to investigate match groups and rewrite strings.
impl RE2 {
  /// Returns the maximum submatch needed for the `rewrite` to be done by
  /// [`Self::replace()`]. E.g. if `rewrite == r"foo \2,\1"`, returns 2.
  ///
  /// [`Self::check_rewrite()`] ensures that this number is no larger than the
  /// result of [`Self::num_captures()`].
  ///```
  /// use re2::RE2;
  ///
  /// assert_eq!(0, RE2::max_submatch("asdf".into()));
  /// assert_eq!(0, RE2::max_submatch(r"\0asdf".into()));
  /// assert_eq!(1, RE2::max_submatch(r"\0a\1sdf".into()));
  /// assert_eq!(3, RE2::max_submatch(r"\3a\1sdf".into()));
  /// ```
  pub fn max_submatch(rewrite: &str) -> usize {
    let rewrite = StringView::from_str(rewrite);
    unsafe { re2_c::RE2Wrapper::max_submatch(rewrite.into_native()) }
  }

  /// Return the number of capture groups in the current pattern.
  ///
  /// Alternately, this can be understood as the highest capture group index
  /// which this pattern can support in a rewrite string, with 0 referring to
  /// the entire match. Also see [`Self::max_submatch()`].
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::RE2;
  ///
  /// assert_eq!(0, "a.df".parse::<RE2>()?.num_captures());
  /// assert_eq!(1, "a(.)df".parse::<RE2>()?.num_captures());
  /// assert_eq!(2, "a((.)df)".parse::<RE2>()?.num_captures());
  /// assert_eq!(3, "(?P<foo>a)((.)df)".parse::<RE2>()?.num_captures());
  /// # Ok(())
  /// # }
  /// ```
  pub fn num_captures(&self) -> usize { unsafe { self.0.num_captures() } }

  /// Return an FFI handle to a C++ iterator over the named capture groups.
  ///
  /// This method does not provide information about positional-only capture
  /// groups. Use [`Self::named_and_numbered_groups()`] for an iterator that
  /// covers positional as well as named groups.
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::RE2;
  ///
  /// assert_eq!(0, "a(.)df".parse::<RE2>()?.named_groups().count());
  /// assert_eq!(1, "a(?P<hey>.)df".parse::<RE2>()?.named_groups().count());
  ///
  /// // Not all captures are named:
  /// let r: RE2 = "a(?P<y>(?P<x>.)d(f)(?P<z>e))".parse()?;
  /// assert_eq!(4, r.num_captures());
  ///
  /// // Results are sorted by number:
  /// let groups: Vec<(&str, usize)> = r.named_groups()
  ///   .map(|g| (g.name(), *g.index()))
  ///   .collect();
  /// assert_eq!(vec![("y", 1), ("x", 2), ("z", 4)], groups);
  /// # Ok(())
  /// # }
  /// ```
  pub fn named_groups(&self) -> impl Iterator<Item=NamedGroup<'_>>+'_ { self.make_named_groups() }

  fn make_named_groups(&self) -> NamedCapturingGroups<'_> {
    unsafe { NamedCapturingGroups::from_native(self.0.named_groups()) }
  }

  /// Return an iterator covering both named and positional-only groups.
  ///
  /// Positional-only groups are represented with [`None`] instead of a
  /// [`str`](prim@str).
  ///
  /// The index of each group can be recovered by calling `.enumerate()` on the
  /// result. This is possible because this iterator also generates a
  /// positional-only group at index 0, which can be thought
  /// of as corresponding to the 0th group in a rewrite string (aka the entire
  /// match).
  ///
  /// While most use cases will likely employ either only positional or only
  /// named groups, this method can be useful for introspection over
  /// uncontrolled inputs.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let r: re2::RE2 = "a(?P<y>(?P<x>.)d(f)(?P<z>e)(n))".parse()?;
  /// assert_eq!(5, r.num_captures());
  ///
  /// let indexed: Vec<(usize, Option<&str>)> = r.named_and_numbered_groups()
  ///   .enumerate()
  ///   .collect();
  /// assert_eq!(
  ///   &indexed,
  ///   &[(0, None), (1, Some("y")), (2, Some("x")), (3, None), (4, Some("z")), (5, None)] );
  /// # Ok(())
  /// # }
  pub fn named_and_numbered_groups(&self) -> impl ExactSizeIterator<Item=Option<&str>> {
    NamedAndNumberedGroups::new(self.num_captures(), self.make_named_groups())
  }

  pub(crate) fn check_rewrite_view(&self, rewrite: StringView) -> Result<(), RewriteError> {
    let mut sw = StringWrapper::blank();

    if unsafe {
      self
        .0
        .check_rewrite_string(rewrite.into_native(), sw.as_mut_native())
    } {
      Ok(())
    } else {
      Err(RewriteError {
        message: String::from_utf8_lossy(sw.as_view().as_slice()).to_string(),
      })
    }
  }

  /// Check that the given `rewrite` string is suitable for use with this
  /// regular expression.
  ///
  /// It checks that:
  /// - The regular expression has enough parenthesized subexpressions to
  ///   satisfy all of the `\N` tokens in rewrite
  /// - The rewrite string doesn't have any syntax errors.  E.g., `\` followed
  ///   by anything other than a digit or `\`.
  ///
  /// A [`true`] return value guarantees that [`Self::replace()`] and
  /// [`Self::extract()`] won't fail because of a bad rewrite string.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// use re2::{RE2, error::RewriteError};
  ///
  /// let r: RE2 = "asdf".parse()?;
  /// r.check_rewrite("a").unwrap();
  /// r.check_rewrite(r"a\0b").unwrap();
  /// assert_eq!(
  ///   RewriteError {
  ///     message: "Rewrite schema requests 1 matches, but the regexp only has 0 parenthesized subexpressions.".to_string(),
  ///   },
  ///   r.check_rewrite(r"a\0b\1").err().unwrap(),
  /// );
  /// # Ok(())
  /// # }
  /// ```
  pub fn check_rewrite(&self, rewrite: &str) -> Result<(), RewriteError> {
    self.check_rewrite_view(StringView::from_str(rewrite))
  }

  pub(crate) fn vector_rewrite_view<const N: usize>(
    &self,
    out: &mut StringWrapper,
    rewrite: StringView,
    inputs: [StringView; N],
  ) -> bool {
    let mut input_views: [MaybeUninit<re2_c::StringView>; N] = uninit_array();
    for (sv, s) in input_views.iter_mut().zip(inputs.into_iter()) {
      sv.write(s.into_native());
    }
    let input_views = unsafe { array_assume_init(input_views) };

    unsafe {
      self.0.vector_rewrite(
        out.as_mut_native(),
        rewrite.into_native(),
        input_views.as_ptr(),
        input_views.len(),
      )
    }
  }

  /// Append the `rewrite` string, with backslash substitutions from `inputs`,
  /// to string `out`.
  ///
  /// Returns [`true`] on success. This method can fail because of a malformed
  /// rewrite string. [`Self::check_rewrite()`] guarantees that the rewrite will
  /// be sucessful.
  ///
  ///```
  /// # fn main() -> Result<(), re2::RE2Error> {
  /// let mut sw = re2::string::StringWrapper::blank();
  /// let r: re2::RE2 = "a(s+)d(f+)".parse()?;
  /// assert!(r.vector_rewrite(&mut sw, r"bb\1cc\0dd\2", ["asdff", "s", "ff"]));
  /// assert_eq!(unsafe { sw.as_view().as_str() }, "bbsccasdffddff");
  /// # Ok(())
  /// # }
  /// ```
  pub fn vector_rewrite<const N: usize>(
    &self,
    out: &mut StringWrapper,
    rewrite: &str,
    inputs: [&str; N],
  ) -> bool {
    let rewrite = StringView::from_str(rewrite);
    let inputs = Self::convert_from_strings(inputs);
    self.vector_rewrite_view(out, rewrite, inputs)
  }
}

impl fmt::Debug for RE2 {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(
      f,
      "RE2(pattern={:?}, options={:?})",
      self.pattern(),
      self.options()
    )
  }
}

impl fmt::Display for RE2 {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let o = self.options();
    if o == Options::default() {
      write!(f, "/{}/", self.pattern())
    } else {
      write!(f, "RE2(/{}/, options={:?})", self.pattern(), o)
    }
  }
}

impl cmp::PartialEq for RE2 {
  fn eq(&self, other: &Self) -> bool {
    self.pattern().eq(other.pattern()) && self.options().eq(&other.options())
  }
}

impl cmp::Eq for RE2 {}

impl cmp::PartialOrd for RE2 {
  fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
}

impl cmp::Ord for RE2 {
  fn cmp(&self, other: &Self) -> cmp::Ordering {
    let intermediate = self.pattern().cmp(other.pattern());
    if intermediate != cmp::Ordering::Equal {
      return intermediate;
    }
    self.options().cmp(&other.options())
  }
}

impl hash::Hash for RE2 {
  fn hash<H>(&self, state: &mut H)
  where H: hash::Hasher {
    self.pattern().hash(state);
    self.options().hash(state);
  }
}

/// An FFI handle to a string-index pair representing a named parenthetical
/// capture group.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct NamedGroup<'a> {
  inner: re2_c::NamedGroup,
  _ph: PhantomData<&'a u8>,
}

impl<'a> NamedGroup<'a> {
  pub(crate) const unsafe fn from_native(inner: re2_c::NamedGroup) -> Self {
    Self {
      inner,
      _ph: PhantomData,
    }
  }

  /// Get a handle to the group's name, which can be decoded to
  /// [`str`](prim@str) if the original pattern was also UTF-8.
  pub const fn name(&self) -> &'a str {
    unsafe { mem::transmute(StringView::from_native(self.inner.name_).as_str()) }
  }

  /// Get a handle to the index, which does not change after creation.
  pub const fn index(&self) -> &'a usize { unsafe { mem::transmute(&self.inner.index_) } }
}

impl<'a> fmt::Debug for NamedGroup<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "NamedGroup(i={}, name={:?})", self.index(), self.name())
  }
}

#[repr(transparent)]
struct NamedCapturingGroups<'a> {
  inner: re2_c::NamedCapturingGroups,
  _ph: PhantomData<&'a u8>,
}

impl<'a> NamedCapturingGroups<'a> {
  pub(crate) const unsafe fn from_native(inner: re2_c::NamedCapturingGroups) -> Self {
    Self {
      inner,
      _ph: PhantomData,
    }
  }

  fn deref(&self) -> NamedGroup<'a> {
    let mut out: MaybeUninit<re2_c::NamedGroup> = MaybeUninit::uninit();
    unsafe {
      self.inner.deref(out.as_mut_ptr());
      NamedGroup::from_native(out.assume_init())
    }
  }

  fn advance(&mut self) {
    unsafe {
      self.inner.advance();
    }
  }

  fn completed(&self) -> bool { unsafe { self.inner.completed() } }
}

impl<'a> Iterator for NamedCapturingGroups<'a> {
  type Item = NamedGroup<'a>;

  fn next(&mut self) -> Option<Self::Item> {
    if self.completed() {
      return None;
    }

    let ret = self.deref();
    self.advance();
    Some(ret)
  }
}

struct NamedAndNumberedGroups<'a> {
  at_start: bool,
  total_num_captures: usize,
  groups_iter: Option<NamedCapturingGroups<'a>>,
  next_named_group: Option<NamedGroup<'a>>,
  cur_index: usize,
}

impl<'a> NamedAndNumberedGroups<'a> {
  pub fn new(total_num_captures: usize, groups_iter: NamedCapturingGroups<'a>) -> Self {
    Self {
      at_start: true,
      total_num_captures,
      groups_iter: Some(groups_iter),
      next_named_group: None,
      cur_index: 0,
    }
  }

  const fn remaining(&self) -> usize {
    if self.at_start {
      self.total_num_captures + 1
    } else {
      self.total_num_captures - self.cur_index + 1
    }
  }
}

impl<'a> Iterator for NamedAndNumberedGroups<'a> {
  type Item = Option<&'a str>;

  fn next(&mut self) -> Option<Self::Item> {
    let Self {
      ref mut at_start,
      ref total_num_captures,
      ref mut groups_iter,
      ref mut next_named_group,
      ref mut cur_index,
    } = self;

    /* Return 0th submatch (for whole pattern). */
    if *at_start {
      *at_start = false;
      *cur_index = 1;
      return Some(None);
    }

    if *cur_index > *total_num_captures {
      return None;
    }

    if next_named_group.is_none() {
      let reset_groups_iter = if let Some(ref mut g) = groups_iter {
        if g.completed() {
          true
        } else {
          *next_named_group = Some(g.deref());
          g.advance();
          false
        }
      } else {
        false
      };
      if reset_groups_iter {
        *groups_iter = None;
      }
    }
    let ret = if let Some(named_group) = next_named_group {
      if *cur_index < *named_group.index() {
        None
      } else {
        debug_assert_eq!(cur_index, named_group.index());
        Some(named_group.name())
      }
    } else {
      None
    };
    if ret.is_some() {
      *next_named_group = None;
    }

    *cur_index += 1;

    Some(ret)
  }

  fn size_hint(&self) -> (usize, Option<usize>) { (self.remaining(), Some(self.remaining())) }
}

impl<'a> ExactSizeIterator for NamedAndNumberedGroups<'a> {}

struct MatchIter<'r, 'h, const N: usize> {
  remaining_input: StringView<'h>,
  pattern: &'r RE2,
}

impl<'r, 'h, const N: usize> Iterator for MatchIter<'r, 'h, N> {
  type Item = [StringView<'h>; N];

  fn next(&mut self) -> Option<Self::Item> {
    let matches = self.pattern.match_routine_view(
      self.remaining_input,
      0..self.remaining_input.len(),
      Anchor::Unanchored,
    )?;
    let consumed = unsafe {
      /* Group 0 has the entire match, and since we checked N > 0 elsewhere we know
       * it exists. */
      let full_match = matches.get_unchecked(0).as_slice();
      /* The remaining input should point past the end of the match. */
      let new_start = full_match.as_ptr().add(full_match.len());
      /* Calculate the distance from the previous start. */
      let consumed = new_start.offset_from(self.remaining_input.as_slice().as_ptr());
      debug_assert!(consumed >= 0);
      consumed as usize
    };
    /* Matched empty string; to avoid looping forever, return None. */
    if consumed == 0 {
      return None;
    }
    self.remaining_input = self.remaining_input.index_range(consumed..).unwrap();
    Some(matches)
  }
}

struct SplitIter<'r, 'h> {
  remaining_input: Option<StringView<'h>>,
  pattern: &'r RE2,
}

impl<'r, 'h> Iterator for SplitIter<'r, 'h> {
  type Item = StringView<'h>;

  fn next(&mut self) -> Option<Self::Item> {
    let remaining = self.remaining_input?;
    if let Some([m]) =
      self
        .pattern
        .match_routine_view(remaining, 0..remaining.len(), Anchor::Unanchored)
    {
      let m = m.as_slice();
      let prev_start = remaining.as_slice().as_ptr();
      let consumed = unsafe { m.as_ptr().offset_from(prev_start) };
      debug_assert!(consumed >= 0);
      let consumed = consumed as usize;
      let ret = remaining.index_range(..consumed).unwrap();
      let consumed_with_match = consumed + m.len();
      /* Matched empty string; to avoid looping forever, return None. */
      self.remaining_input = if consumed_with_match == 0 {
        None
      } else {
        Some(remaining.index_range(consumed_with_match..).unwrap())
      };
      Some(ret)
    } else {
      mem::take(&mut self.remaining_input)
    }
  }
}