Skip to main content

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::{Error, RegexCompileFlags, RegexMatchFlags, ffi, translate::*};
6
7crate::wrapper! {
8    /// match_info,
9    ///   NULL);
10    /// ```text
11    ///
12    /// The method [`match_full()`][Self::match_full()] (and other methods implementing
13    /// `start_pos`) allow for lookback before the start position to determine if
14    /// the previous character satisfies an assertion.
15    ///
16    /// Unless you set the [flags@GLib.RegexCompileFlags.RAW] as one of
17    /// the `GRegexCompileFlags`, all the strings passed to `GRegex` methods must
18    /// be encoded in UTF-8. The lengths and the positions inside the strings are
19    /// in bytes and not in characters, so, for instance, `\xc3\xa0` (i.e., `à`)
20    /// is two bytes long but it is treated as a single character. If you set
21    /// `G_REGEX_RAW`, the strings can be non-valid UTF-8 strings and a byte is
22    /// treated as a character, so `\xc3\xa0` is two bytes and two characters long.
23    ///
24    /// Regarding line endings, `\n` matches a `\n` character, and `\r` matches
25    /// a `\r` character. More generally, `\R` matches all typical line endings:
26    /// CR + LF (`\r\n`), LF (linefeed, U+000A, `\n`), VT (vertical tab, U+000B,
27    /// `\v`), FF (formfeed, U+000C, `\f`), CR (carriage return, U+000D, `\r`),
28    /// NEL (next line, U+0085), LS (line separator, U+2028), and PS (paragraph
29    /// separator, U+2029).
30    ///
31    /// The behaviour of the dot, circumflex, and dollar metacharacters are
32    /// affected by newline characters. By default, `GRegex` matches any newline
33    /// character matched by `\R`. You can limit the matched newline characters by
34    /// specifying the [flags@GLib.RegexMatchFlags.NEWLINE_CR],
35    /// [flags@GLib.RegexMatchFlags.NEWLINE_LF], and
36    /// [flags@GLib.RegexMatchFlags.NEWLINE_CRLF] compile options, and
37    /// with [flags@GLib.RegexMatchFlags.NEWLINE_ANY],
38    /// [flags@GLib.RegexMatchFlags.NEWLINE_CR],
39    /// [flags@GLib.RegexMatchFlags.NEWLINE_LF] and
40    /// [flags@GLib.RegexMatchFlags.NEWLINE_CRLF] match options.
41    /// These settings are also relevant when compiling a pattern if
42    /// [flags@GLib.RegexCompileFlags.EXTENDED] is set and an unescaped
43    /// `#` outside a character class is encountered. This indicates a comment
44    /// that lasts until after the next newline.
45    ///
46    /// Because `GRegex` does not modify its internal state between creation and
47    /// destruction, you can create and modify the same `GRegex` instance from
48    /// different threads. In contrast, [`MatchInfo`][crate::MatchInfo] is not thread safe.
49    ///
50    /// The regular expression low-level functionalities are obtained through
51    /// the excellent [PCRE](http://www.pcre.org/) library written by Philip Hazel.
52    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
53    pub struct Regex(Shared<ffi::GRegex>);
54
55    match fn {
56        ref => |ptr| ffi::g_regex_ref(ptr),
57        unref => |ptr| ffi::g_regex_unref(ptr),
58        type_ => || ffi::g_regex_get_type(),
59    }
60}
61
62impl Regex {
63    /// Compiles the regular expression to an internal form, and does
64    /// the initial setup of the #GRegex structure.
65    /// ## `pattern`
66    /// the regular expression
67    /// ## `compile_options`
68    /// compile options for the regular expression, or 0
69    /// ## `match_options`
70    /// match options for the regular expression, or 0
71    ///
72    /// # Returns
73    ///
74    /// a #GRegex structure or [`None`] if an error occurred. Call
75    ///   g_regex_unref() when you are done with it
76    #[doc(alias = "g_regex_new")]
77    pub fn new(
78        pattern: &str,
79        compile_options: RegexCompileFlags,
80        match_options: RegexMatchFlags,
81    ) -> Result<Option<Regex>, crate::Error> {
82        unsafe {
83            let mut error = std::ptr::null_mut();
84            let ret = ffi::g_regex_new(
85                pattern.to_glib_none().0,
86                compile_options.into_glib(),
87                match_options.into_glib(),
88                &mut error,
89            );
90            if error.is_null() {
91                Ok(from_glib_full(ret))
92            } else {
93                Err(from_glib_full(error))
94            }
95        }
96    }
97
98    /// Returns the number of capturing subpatterns in the pattern.
99    ///
100    /// # Returns
101    ///
102    /// the number of capturing subpatterns
103    #[doc(alias = "g_regex_get_capture_count")]
104    #[doc(alias = "get_capture_count")]
105    pub fn capture_count(&self) -> i32 {
106        unsafe { ffi::g_regex_get_capture_count(self.to_glib_none().0) }
107    }
108
109    /// Returns the compile options that @self was created with.
110    ///
111    /// Depending on the version of PCRE that is used, this may or may not
112    /// include flags set by option expressions such as `(?i)` found at the
113    /// top-level within the compiled pattern.
114    ///
115    /// # Returns
116    ///
117    /// flags from #GRegexCompileFlags
118    #[doc(alias = "g_regex_get_compile_flags")]
119    #[doc(alias = "get_compile_flags")]
120    pub fn compile_flags(&self) -> RegexCompileFlags {
121        unsafe { from_glib(ffi::g_regex_get_compile_flags(self.to_glib_none().0)) }
122    }
123
124    /// Checks whether the pattern contains explicit CR or LF references.
125    ///
126    /// # Returns
127    ///
128    /// [`true`] if the pattern contains explicit CR or LF references
129    #[doc(alias = "g_regex_get_has_cr_or_lf")]
130    #[doc(alias = "get_has_cr_or_lf")]
131    pub fn has_cr_or_lf(&self) -> bool {
132        unsafe { from_glib(ffi::g_regex_get_has_cr_or_lf(self.to_glib_none().0)) }
133    }
134
135    /// Returns the match options that @self was created with.
136    ///
137    /// # Returns
138    ///
139    /// flags from #GRegexMatchFlags
140    #[doc(alias = "g_regex_get_match_flags")]
141    #[doc(alias = "get_match_flags")]
142    pub fn match_flags(&self) -> RegexMatchFlags {
143        unsafe { from_glib(ffi::g_regex_get_match_flags(self.to_glib_none().0)) }
144    }
145
146    /// Returns the number of the highest back reference
147    /// in the pattern, or 0 if the pattern does not contain
148    /// back references.
149    ///
150    /// # Returns
151    ///
152    /// the number of the highest back reference
153    #[doc(alias = "g_regex_get_max_backref")]
154    #[doc(alias = "get_max_backref")]
155    pub fn max_backref(&self) -> i32 {
156        unsafe { ffi::g_regex_get_max_backref(self.to_glib_none().0) }
157    }
158
159    /// Gets the number of characters in the longest lookbehind assertion in the
160    /// pattern. This information is useful when doing multi-segment matching using
161    /// the partial matching facilities.
162    ///
163    /// # Returns
164    ///
165    /// the number of characters in the longest lookbehind assertion.
166    #[doc(alias = "g_regex_get_max_lookbehind")]
167    #[doc(alias = "get_max_lookbehind")]
168    pub fn max_lookbehind(&self) -> i32 {
169        unsafe { ffi::g_regex_get_max_lookbehind(self.to_glib_none().0) }
170    }
171
172    /// Gets the pattern string associated with @self, i.e. a copy of
173    /// the string passed to g_regex_new().
174    ///
175    /// # Returns
176    ///
177    /// the pattern of @self
178    #[doc(alias = "g_regex_get_pattern")]
179    #[doc(alias = "get_pattern")]
180    pub fn pattern(&self) -> crate::GString {
181        unsafe { from_glib_none(ffi::g_regex_get_pattern(self.to_glib_none().0)) }
182    }
183
184    //#[doc(alias = "g_regex_replace_eval")]
185    //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> {
186    //    unsafe { TODO: call ffi:g_regex_replace_eval() }
187    //}
188}