glib/auto/regex.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, translate::*, Error, RegexCompileFlags, RegexMatchFlags};
6
7crate::wrapper! {
8 /// A `GRegex` is the "compiled" form of a regular expression pattern.
9 ///
10 /// `GRegex` implements regular expression pattern matching using syntax and
11 /// semantics similar to Perl regular expression. See the
12 /// [PCRE documentation](man:pcrepattern(3)) for the syntax definition.
13 ///
14 /// Some functions accept a @start_position argument, setting it differs
15 /// from just passing over a shortened string and setting [`RegexMatchFlags::NOTBOL`][crate::RegexMatchFlags::NOTBOL]
16 /// in the case of a pattern that begins with any kind of lookbehind assertion.
17 /// For example, consider the pattern "\Biss\B" which finds occurrences of "iss"
18 /// in the middle of words. ("\B" matches only if the current position in the
19 /// subject is not a word boundary.) When applied to the string "Mississipi"
20 /// from the fourth byte, namely "issipi", it does not match, because "\B" is
21 /// always false at the start of the subject, which is deemed to be a word
22 /// boundary. However, if the entire string is passed , but with
23 /// @start_position set to 4, it finds the second occurrence of "iss" because
24 /// it is able to look behind the starting point to discover that it is
25 /// preceded by a letter.
26 ///
27 /// Note that, unless you set the [`RegexCompileFlags::RAW`][crate::RegexCompileFlags::RAW] flag, all the strings passed
28 /// to these functions must be encoded in UTF-8. The lengths and the positions
29 /// inside the strings are in bytes and not in characters, so, for instance,
30 /// "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a
31 /// single character. If you set [`RegexCompileFlags::RAW`][crate::RegexCompileFlags::RAW] the strings can be non-valid
32 /// UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two
33 /// bytes and two characters long.
34 ///
35 /// When matching a pattern, "\n" matches only against a "\n" character in
36 /// the string, and "\r" matches only a "\r" character. To match any newline
37 /// sequence use "\R". This particular group matches either the two-character
38 /// sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed,
39 /// U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"),
40 /// CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line
41 /// separator, U+2028), or PS (paragraph separator, U+2029).
42 ///
43 /// The behaviour of the dot, circumflex, and dollar metacharacters are
44 /// affected by newline characters, the default is to recognize any newline
45 /// character (the same characters recognized by "\R"). This can be changed
46 /// with `G_REGEX_NEWLINE_CR`, `G_REGEX_NEWLINE_LF` and `G_REGEX_NEWLINE_CRLF`
47 /// compile options, and with `G_REGEX_MATCH_NEWLINE_ANY`,
48 /// `G_REGEX_MATCH_NEWLINE_CR`, `G_REGEX_MATCH_NEWLINE_LF` and
49 /// `G_REGEX_MATCH_NEWLINE_CRLF` match options. These settings are also
50 /// relevant when compiling a pattern if `G_REGEX_EXTENDED` is set, and an
51 /// unescaped "#" outside a character class is encountered. This indicates
52 /// a comment that lasts until after the next newline.
53 ///
54 /// Creating and manipulating the same `GRegex` structure from different
55 /// threads is not a problem as `GRegex` does not modify its internal
56 /// state between creation and destruction, on the other hand `GMatchInfo`
57 /// is not threadsafe.
58 ///
59 /// The regular expressions low-level functionalities are obtained through
60 /// the excellent [PCRE](http://www.pcre.org/) library written by Philip Hazel.
61 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
62 pub struct Regex(Shared<ffi::GRegex>);
63
64 match fn {
65 ref => |ptr| ffi::g_regex_ref(ptr),
66 unref => |ptr| ffi::g_regex_unref(ptr),
67 type_ => || ffi::g_regex_get_type(),
68 }
69}
70
71impl Regex {
72 /// Compiles the regular expression to an internal form, and does
73 /// the initial setup of the #GRegex structure.
74 /// ## `pattern`
75 /// the regular expression
76 /// ## `compile_options`
77 /// compile options for the regular expression, or 0
78 /// ## `match_options`
79 /// match options for the regular expression, or 0
80 ///
81 /// # Returns
82 ///
83 /// a #GRegex structure or [`None`] if an error occurred. Call
84 /// g_regex_unref() when you are done with it
85 #[doc(alias = "g_regex_new")]
86 pub fn new(
87 pattern: &str,
88 compile_options: RegexCompileFlags,
89 match_options: RegexMatchFlags,
90 ) -> Result<Option<Regex>, crate::Error> {
91 unsafe {
92 let mut error = std::ptr::null_mut();
93 let ret = ffi::g_regex_new(
94 pattern.to_glib_none().0,
95 compile_options.into_glib(),
96 match_options.into_glib(),
97 &mut error,
98 );
99 if error.is_null() {
100 Ok(from_glib_full(ret))
101 } else {
102 Err(from_glib_full(error))
103 }
104 }
105 }
106
107 /// Returns the number of capturing subpatterns in the pattern.
108 ///
109 /// # Returns
110 ///
111 /// the number of capturing subpatterns
112 #[doc(alias = "g_regex_get_capture_count")]
113 #[doc(alias = "get_capture_count")]
114 pub fn capture_count(&self) -> i32 {
115 unsafe { ffi::g_regex_get_capture_count(self.to_glib_none().0) }
116 }
117
118 /// Returns the compile options that @self was created with.
119 ///
120 /// Depending on the version of PCRE that is used, this may or may not
121 /// include flags set by option expressions such as `(?i)` found at the
122 /// top-level within the compiled pattern.
123 ///
124 /// # Returns
125 ///
126 /// flags from #GRegexCompileFlags
127 #[doc(alias = "g_regex_get_compile_flags")]
128 #[doc(alias = "get_compile_flags")]
129 pub fn compile_flags(&self) -> RegexCompileFlags {
130 unsafe { from_glib(ffi::g_regex_get_compile_flags(self.to_glib_none().0)) }
131 }
132
133 /// Checks whether the pattern contains explicit CR or LF references.
134 ///
135 /// # Returns
136 ///
137 /// [`true`] if the pattern contains explicit CR or LF references
138 #[doc(alias = "g_regex_get_has_cr_or_lf")]
139 #[doc(alias = "get_has_cr_or_lf")]
140 pub fn has_cr_or_lf(&self) -> bool {
141 unsafe { from_glib(ffi::g_regex_get_has_cr_or_lf(self.to_glib_none().0)) }
142 }
143
144 /// Returns the match options that @self was created with.
145 ///
146 /// # Returns
147 ///
148 /// flags from #GRegexMatchFlags
149 #[doc(alias = "g_regex_get_match_flags")]
150 #[doc(alias = "get_match_flags")]
151 pub fn match_flags(&self) -> RegexMatchFlags {
152 unsafe { from_glib(ffi::g_regex_get_match_flags(self.to_glib_none().0)) }
153 }
154
155 /// Returns the number of the highest back reference
156 /// in the pattern, or 0 if the pattern does not contain
157 /// back references.
158 ///
159 /// # Returns
160 ///
161 /// the number of the highest back reference
162 #[doc(alias = "g_regex_get_max_backref")]
163 #[doc(alias = "get_max_backref")]
164 pub fn max_backref(&self) -> i32 {
165 unsafe { ffi::g_regex_get_max_backref(self.to_glib_none().0) }
166 }
167
168 /// Gets the number of characters in the longest lookbehind assertion in the
169 /// pattern. This information is useful when doing multi-segment matching using
170 /// the partial matching facilities.
171 ///
172 /// # Returns
173 ///
174 /// the number of characters in the longest lookbehind assertion.
175 #[doc(alias = "g_regex_get_max_lookbehind")]
176 #[doc(alias = "get_max_lookbehind")]
177 pub fn max_lookbehind(&self) -> i32 {
178 unsafe { ffi::g_regex_get_max_lookbehind(self.to_glib_none().0) }
179 }
180
181 /// Gets the pattern string associated with @self, i.e. a copy of
182 /// the string passed to g_regex_new().
183 ///
184 /// # Returns
185 ///
186 /// the pattern of @self
187 #[doc(alias = "g_regex_get_pattern")]
188 #[doc(alias = "get_pattern")]
189 pub fn pattern(&self) -> crate::GString {
190 unsafe { from_glib_none(ffi::g_regex_get_pattern(self.to_glib_none().0)) }
191 }
192
193 //#[doc(alias = "g_regex_replace_eval")]
194 //pub fn replace_eval(&self, string: &[&str], start_position: i32, match_options: RegexMatchFlags, eval: /*Unimplemented*/FnMut(&MatchInfo, /*Ignored*/String) -> bool, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> Result<crate::GString, crate::Error> {
195 // unsafe { TODO: call ffi:g_regex_replace_eval() }
196 //}
197}