1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! This module is inefficient and should not be used by Rust programs except for
//! compatibility with GLib.Regex based APIs.

use crate::{
    translate::*, GStr, GStringPtr, MatchInfo, PtrSlice, Regex, RegexCompileFlags, RegexMatchFlags,
};
use std::{mem, ptr};

impl Regex {
    #[doc(alias = "g_regex_get_string_number")]
    #[doc(alias = "get_string_number")]
    pub fn string_number(&self, name: impl IntoGStr) -> i32 {
        name.run_with_gstr(|name| unsafe {
            ffi::g_regex_get_string_number(self.to_glib_none().0, name.to_glib_none().0)
        })
    }

    #[doc(alias = "g_regex_escape_nul")]
    pub fn escape_nul(string: impl IntoGStr) -> crate::GString {
        unsafe {
            string.run_with_gstr(|string| {
                from_glib_full(ffi::g_regex_escape_nul(
                    string.to_glib_none().0,
                    string.len() as _,
                ))
            })
        }
    }

    #[doc(alias = "g_regex_escape_string")]
    pub fn escape_string(string: impl IntoGStr) -> crate::GString {
        unsafe {
            string.run_with_gstr(|string| {
                from_glib_full(ffi::g_regex_escape_string(
                    string.to_glib_none().0,
                    string.len() as _,
                ))
            })
        }
    }

    #[doc(alias = "g_regex_check_replacement")]
    pub fn check_replacement(replacement: impl IntoGStr) -> Result<bool, crate::Error> {
        replacement.run_with_gstr(|replacement| unsafe {
            let mut has_references = mem::MaybeUninit::uninit();
            let mut error = ptr::null_mut();
            let is_ok = ffi::g_regex_check_replacement(
                replacement.to_glib_none().0,
                has_references.as_mut_ptr(),
                &mut error,
            );
            debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib(has_references.assume_init()))
            } else {
                Err(from_glib_full(error))
            }
        })
    }

    #[doc(alias = "g_regex_match_simple")]
    pub fn match_simple(
        pattern: impl IntoGStr,
        string: impl IntoGStr,
        compile_options: RegexCompileFlags,
        match_options: RegexMatchFlags,
    ) -> bool {
        pattern.run_with_gstr(|pattern| {
            string.run_with_gstr(|string| unsafe {
                from_glib(ffi::g_regex_match_simple(
                    pattern.to_glib_none().0,
                    string.to_glib_none().0,
                    compile_options.into_glib(),
                    match_options.into_glib(),
                ))
            })
        })
    }

    #[doc(alias = "g_regex_replace")]
    pub fn replace(
        &self,
        string: impl IntoGStr,
        start_position: i32,
        replacement: impl IntoGStr,
        match_options: RegexMatchFlags,
    ) -> Result<crate::GString, crate::Error> {
        unsafe {
            string.run_with_gstr(|string| {
                replacement.run_with_gstr(|replacement| {
                    let mut error = ptr::null_mut();
                    let ret = ffi::g_regex_replace(
                        self.to_glib_none().0,
                        string.as_ptr() as *const _,
                        string.len() as _,
                        start_position,
                        replacement.to_glib_none().0,
                        match_options.into_glib(),
                        &mut error,
                    );
                    if error.is_null() {
                        Ok(from_glib_full(ret))
                    } else {
                        Err(from_glib_full(error))
                    }
                })
            })
        }
    }

    #[doc(alias = "g_regex_match_all")]
    pub fn match_all<'input>(
        &self,
        string: &'input GStr,
        match_options: RegexMatchFlags,
    ) -> Option<MatchInfo<'input>> {
        self.match_all_full(string, 0, match_options).ok()
    }

    #[doc(alias = "g_regex_match_all_full")]
    pub fn match_all_full<'input>(
        &self,
        string: &'input GStr,
        start_position: i32,
        match_options: RegexMatchFlags,
    ) -> Result<MatchInfo<'input>, crate::Error> {
        unsafe {
            let mut match_info = ptr::null_mut();
            let mut error = ptr::null_mut();
            let is_ok = ffi::g_regex_match_all_full(
                self.to_glib_none().0,
                string.to_glib_none().0,
                string.len() as _,
                start_position,
                match_options.into_glib(),
                &mut match_info,
                &mut error,
            );
            debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib_full(match_info))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "g_regex_match")]
    pub fn match_<'input>(
        &self,
        string: &'input GStr,
        match_options: RegexMatchFlags,
    ) -> Option<MatchInfo<'input>> {
        self.match_full(string, 0, match_options).ok()
    }

    #[doc(alias = "g_regex_match_full")]
    pub fn match_full<'input>(
        &self,
        string: &'input GStr,
        start_position: i32,
        match_options: RegexMatchFlags,
    ) -> Result<MatchInfo<'input>, crate::Error> {
        unsafe {
            let mut match_info = ptr::null_mut();
            let mut error = ptr::null_mut();
            let is_ok = ffi::g_regex_match_full(
                self.to_glib_none().0,
                string.to_glib_none().0,
                string.len() as _,
                start_position,
                match_options.into_glib(),
                &mut match_info,
                &mut error,
            );
            debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib_full(match_info))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "g_regex_replace_literal")]
    pub fn replace_literal(
        &self,
        string: impl IntoGStr,
        start_position: i32,
        replacement: impl IntoGStr,
        match_options: RegexMatchFlags,
    ) -> Result<crate::GString, crate::Error> {
        unsafe {
            string.run_with_gstr(|string| {
                replacement.run_with_gstr(|replacement| {
                    let mut error = ptr::null_mut();
                    let ret = ffi::g_regex_replace_literal(
                        self.to_glib_none().0,
                        string.to_glib_none().0,
                        string.len() as _,
                        start_position,
                        replacement.to_glib_none().0,
                        match_options.into_glib(),
                        &mut error,
                    );
                    if error.is_null() {
                        Ok(from_glib_full(ret))
                    } else {
                        Err(from_glib_full(error))
                    }
                })
            })
        }
    }

    #[doc(alias = "g_regex_split")]
    pub fn split(
        &self,
        string: impl IntoGStr,
        match_options: RegexMatchFlags,
    ) -> PtrSlice<GStringPtr> {
        self.split_full(string, 0, match_options, 0)
            .unwrap_or_default()
    }

    #[doc(alias = "g_regex_split_full")]
    pub fn split_full(
        &self,
        string: impl IntoGStr,
        start_position: i32,
        match_options: RegexMatchFlags,
        max_tokens: i32,
    ) -> Result<PtrSlice<GStringPtr>, crate::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            string.run_with_gstr(|string| {
                let ret = ffi::g_regex_split_full(
                    self.to_glib_none().0,
                    string.to_glib_none().0,
                    string.len() as _,
                    start_position,
                    match_options.into_glib(),
                    max_tokens,
                    &mut error,
                );
                if error.is_null() {
                    Ok(FromGlibPtrContainer::from_glib_full(ret))
                } else {
                    Err(from_glib_full(error))
                }
            })
        }
    }

    #[doc(alias = "g_regex_split_simple")]
    pub fn split_simple(
        pattern: impl IntoGStr,
        string: impl IntoGStr,
        compile_options: RegexCompileFlags,
        match_options: RegexMatchFlags,
    ) -> PtrSlice<GStringPtr> {
        pattern.run_with_gstr(|pattern| {
            string.run_with_gstr(|string| unsafe {
                FromGlibPtrContainer::from_glib_full(ffi::g_regex_split_simple(
                    pattern.to_glib_none().0,
                    string.to_glib_none().0,
                    compile_options.into_glib(),
                    match_options.into_glib(),
                ))
            })
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::RegexCompileFlags;

    #[test]
    fn test_replace_literal() {
        let regex = Regex::new(
            "s[ai]mple",
            RegexCompileFlags::OPTIMIZE,
            RegexMatchFlags::DEFAULT,
        )
        .expect("Regex new")
        .expect("Null regex");

        let quote = "This is a simple sample.";
        let result = regex
            .replace_literal(quote, 0, "XXX", RegexMatchFlags::DEFAULT)
            .expect("regex replace");

        assert_eq!(result, "This is a XXX XXX.");
    }

    #[test]
    fn test_split() {
        let regex = Regex::new(
            "s[ai]mple",
            RegexCompileFlags::OPTIMIZE,
            RegexMatchFlags::DEFAULT,
        )
        .expect("Regex new")
        .expect("Null regex");

        let quote = "This is a simple sample.";
        let result = regex.split(quote, RegexMatchFlags::DEFAULT);

        assert_eq!(result.len(), 3);
        assert_eq!(result[0], "This is a ");
        assert_eq!(result[1], " ");
        assert_eq!(result[2], ".");
    }
}