tre_regex/
flags.rs

1use std::ffi::c_int;
2
3use crate::tre;
4
5#[allow(clippy::module_name_repetitions)]
6pub type RegFlags = c_int;
7
8/// Flags to pass to [`regcomp`](crate::regcomp).
9#[allow(clippy::module_name_repetitions)]
10#[derive(Clone, Copy, Debug)]
11pub struct RegcompFlags(RegFlags);
12
13impl RegcompFlags {
14    /// Sentinel for empty flags
15    pub const NONE: RegFlags = 0;
16
17    /// Basic (obsolete) regex
18    pub const BASIC: RegFlags = tre::REG_BASIC;
19
20    /// Extended POSIX regex
21    pub const EXTENDED: RegFlags = tre::REG_EXTENDED;
22
23    /// Case-insensitive matches
24    pub const ICASE: RegFlags = tre::REG_ICASE;
25
26    /// Interpret regex literally (all characters are non-special); aka `REG_NOSPEC`
27    pub const LITERAL: RegFlags = tre::REG_LITERAL;
28
29    /// Same meaning as [`RegcompFlags::LITERAL`]
30    pub const NOSPEC: RegFlags = tre::REG_NOSPEC;
31
32    /// Newline-sensitive matching
33    pub const NEWLINE: RegFlags = tre::REG_NEWLINE;
34
35    /// Don't report what was matched; only that it matched.
36    pub const NOSUB: RegFlags = tre::REG_NOSUB;
37
38    /// Concatenation is right-associative
39    pub const RIGHT_ASSOC: RegFlags = tre::REG_RIGHT_ASSOC;
40
41    /// Repetition operators are non-greedy by default
42    pub const UNGREEDY: RegFlags = tre::REG_UNGREEDY;
43
44    /// Use raw bytes
45    pub const USEBYTES: RegFlags = tre::REG_USEBYTES;
46
47    /// Construct a new set of empty flags
48    #[must_use]
49    pub const fn new() -> Self {
50        Self(0)
51    }
52
53    /// Add a flag
54    #[must_use]
55    #[inline]
56    pub const fn add(&self, flag: RegFlags) -> Self {
57        Self(self.0 | flag)
58    }
59
60    /// Remove a flag
61    #[must_use]
62    #[inline]
63    pub const fn remove(&self, flag: RegFlags) -> Self {
64        Self(self.0 & !flag)
65    }
66
67    /// Get set flags as a [`RegFlags`].
68    #[must_use]
69    #[inline]
70    pub const fn get(&self) -> RegFlags {
71        self.0
72    }
73}
74
75/// Flags to pass to [`regexec`](crate::regexec).
76#[allow(clippy::module_name_repetitions)]
77#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
78pub struct RegexecFlags(RegFlags);
79
80impl RegexecFlags {
81    /// Sentinel for empty flags
82    pub const NONE: RegFlags = 0;
83
84    /// Use the approximate matcher
85    pub const APPROX_MATCHER: RegFlags = tre::REG_APPROX_MATCHER;
86
87    /// Use the backtracking matcher
88    pub const BACKTRACKING_MATCHER: RegFlags = tre::REG_BACKTRACKING_MATCHER;
89
90    /// First character of the string is not the beginning of the line
91    pub const NOTBOL: RegFlags = tre::REG_NOTBOL;
92
93    /// Last character of the string is not the end of the line
94    pub const NOTEOL: RegFlags = tre::REG_NOTEOL;
95
96    /// Construct a new set of empty flags
97    #[must_use]
98    #[inline]
99    pub const fn new() -> Self {
100        Self(0)
101    }
102
103    /// Add a flag
104    #[must_use]
105    #[inline]
106    pub const fn add(&self, flag: RegFlags) -> Self {
107        Self(self.0 | flag)
108    }
109
110    /// Remove a flag
111    #[must_use]
112    #[inline]
113    pub const fn remove(&self, flag: RegFlags) -> Self {
114        Self(self.0 & !flag)
115    }
116
117    /// Get set flags as a [`RegFlags`].
118    #[must_use]
119    #[inline]
120    pub const fn get(&self) -> RegFlags {
121        self.0
122    }
123}