diff --git a/glib/src/regex.rs b/glib/src/regex.rs index 45fbad437ba2..565fad018ab5 100644 --- a/glib/src/regex.rs +++ b/glib/src/regex.rs @@ -102,6 +102,7 @@ impl Regex { match_options.into_glib(), &mut error, ); + debug_assert_eq!(ret.is_null(), !error.is_null()); if error.is_null() { Ok(from_glib_full(ret)) } else { @@ -117,8 +118,8 @@ impl Regex { &self, string: &'input GStr, match_options: RegexMatchFlags, - ) -> Option> { - self.match_all_full(string, 0, match_options).ok() + ) -> Result<(bool, MatchInfo<'input>), crate::Error> { + self.match_all_full(string, 0, match_options) } #[doc(alias = "g_regex_match_all_full")] @@ -127,11 +128,11 @@ impl Regex { string: &'input GStr, start_position: i32, match_options: RegexMatchFlags, - ) -> Result, crate::Error> { + ) -> Result<(bool, 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( + let res = ffi::g_regex_match_all_full( self.to_glib_none().0, string.to_glib_none().0, string.len() as _, @@ -140,10 +141,10 @@ impl Regex { &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)) + Ok((from_glib(res), from_glib_full(match_info))) } else { + debug_assert!(match_info.is_null()); Err(from_glib_full(error)) } } @@ -154,8 +155,8 @@ impl Regex { &self, string: &'input GStr, match_options: RegexMatchFlags, - ) -> Option> { - self.match_full(string, 0, match_options).ok() + ) -> Result<(bool, MatchInfo<'input>), crate::Error> { + self.match_full(string, 0, match_options) } #[doc(alias = "g_regex_match_full")] @@ -164,11 +165,11 @@ impl Regex { string: &'input GStr, start_position: i32, match_options: RegexMatchFlags, - ) -> Result, crate::Error> { + ) -> Result<(bool, 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( + let res = ffi::g_regex_match_full( self.to_glib_none().0, string.to_glib_none().0, string.len() as _, @@ -177,10 +178,10 @@ impl Regex { &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)) + Ok((from_glib(res), from_glib_full(match_info))) } else { + debug_assert!(match_info.is_null()); Err(from_glib_full(error)) } } @@ -207,6 +208,7 @@ impl Regex { match_options.into_glib(), &mut error, ); + debug_assert_eq!(ret.is_null(), !error.is_null()); if error.is_null() { Ok(from_glib_full(ret)) } else { @@ -247,6 +249,7 @@ impl Regex { max_tokens, &mut error, ); + debug_assert_eq!(ret.is_null(), !error.is_null()); if error.is_null() { Ok(FromGlibPtrContainer::from_glib_full(ret)) } else { @@ -317,4 +320,37 @@ mod tests { assert_eq!(result[1], " "); assert_eq!(result[2], "."); } + + #[test] + fn test_match() { + let regex = glib::Regex::new( + r"\d", + glib::RegexCompileFlags::DEFAULT, + glib::RegexMatchFlags::DEFAULT, + ) + .expect("Regex new") + .expect("Null regex"); + + // This works (matches) + let input = glib::GString::from("87"); + let m = regex.match_(input.as_gstr(), glib::RegexMatchFlags::DEFAULT); + let (res, m) = m.unwrap(); + assert!(res); + assert!(m.matches()); + assert_eq!(m.match_count(), 1); + assert_eq!(m.fetch(0).as_deref(), Some("8")); + assert!(m.next().unwrap()); + assert_eq!(m.fetch(0).as_deref(), Some("7")); + assert!(!m.next().unwrap()); + assert!(m.fetch(0).is_none()); + + // This panics (no match) + let input = glib::GString::from("a"); + let m = regex.match_(input.as_gstr(), glib::RegexMatchFlags::DEFAULT); + let (res, m) = m.unwrap(); + assert!(!res); + assert!(!m.matches()); + assert_eq!(m.match_count(), 0); + assert!(m.fetch(0).is_none()); + } }