Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions glib/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -117,8 +118,8 @@ impl Regex {
&self,
string: &'input GStr,
match_options: RegexMatchFlags,
) -> Option<MatchInfo<'input>> {
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")]
Expand All @@ -127,11 +128,11 @@ impl Regex {
string: &'input GStr,
start_position: i32,
match_options: RegexMatchFlags,
) -> Result<MatchInfo<'input>, crate::Error> {
) -> Result<(bool, MatchInfo<'input>), crate::Error> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking API change but the current API makes little sense.

If we want to preserve the API we could omit the bool return value and instead require the caller to check match_info.matches() which provides the very same information. It's a bit redundant right now but closer to the C API.

@bilelmoussaoui Any opinions?

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 _,
Expand All @@ -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))
}
}
Expand All @@ -154,8 +155,8 @@ impl Regex {
&self,
string: &'input GStr,
match_options: RegexMatchFlags,
) -> Option<MatchInfo<'input>> {
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")]
Expand All @@ -164,11 +165,11 @@ impl Regex {
string: &'input GStr,
start_position: i32,
match_options: RegexMatchFlags,
) -> Result<MatchInfo<'input>, 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 _,
Expand All @@ -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))
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
}
Loading