hyperscan_tokio/
pattern.rs1use crate::Flags;
4
5#[derive(Debug, Clone)]
7pub struct PatternInfo {
8 pub id: u32,
10 pub original: String,
12 pub simplified: String,
14 pub capture_groups: Vec<CaptureGroupInfo>,
16 pub features: PatternFeatures,
18 pub flags: Flags,
20 pub match_limit: Option<u32>,
22 pub match_limit_recursion: Option<u32>,
24}
25
26#[derive(Debug, Clone)]
28pub struct CaptureGroupInfo {
29 pub name: Option<String>,
31 pub index: usize,
33 pub offset_adjustment: i32,
35}
36
37#[derive(Debug, Clone, Default)]
39pub struct PatternFeatures {
40 pub has_captures: bool,
42 pub has_named_captures: bool,
44 pub has_backrefs: bool,
46 pub has_lookarounds: bool,
48 pub has_anchors: bool,
50 pub has_word_boundaries: bool,
52}
53
54impl PatternFeatures {
55 pub fn needs_simplification(&self) -> bool {
57 self.has_captures || self.has_backrefs || self.has_lookarounds
58 }
59
60 pub fn needs_extraction(&self) -> bool {
62 self.has_captures
63 }
64}
65
66#[derive(Debug, Clone, PartialEq)]
68pub struct CaptureGroup {
69 pub start: usize,
71 pub end: usize,
73 pub name: Option<String>,
75}
76
77impl CaptureGroup {
78 pub fn as_bytes<'a>(&self, data: &'a [u8]) -> &'a [u8] {
80 &data[self.start..self.end]
81 }
82
83 pub fn as_str<'a>(&self, data: &'a [u8]) -> Option<&'a str> {
85 std::str::from_utf8(self.as_bytes(data)).ok()
86 }
87
88 pub fn is_empty(&self) -> bool {
90 self.start == self.end
91 }
92
93 pub fn len(&self) -> usize {
95 self.end - self.start
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn test_pattern_features() {
105 let mut features = PatternFeatures::default();
106 assert!(!features.needs_simplification());
107 assert!(!features.needs_extraction());
108
109 features.has_captures = true;
110 assert!(features.needs_simplification());
111 assert!(features.needs_extraction());
112 }
113
114 #[test]
115 fn test_capture_group() {
116 let data = b"Hello, World!";
117 let capture = CaptureGroup {
118 start: 7,
119 end: 12,
120 name: None,
121 };
122
123 assert_eq!(capture.as_bytes(data), b"World");
124 assert_eq!(capture.as_str(data), Some("World"));
125 }
126}