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