Skip to content

Commit

Permalink
&str -> impl IntoGStr
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmhewitt committed Oct 27, 2023
1 parent 8d1523a commit b8c20b1
Showing 1 changed file with 128 additions and 98 deletions.
226 changes: 128 additions & 98 deletions glib/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ impl Regex {
}

#[doc(alias = "g_regex_escape_nul")]
pub fn escape_nul(string: &str) -> crate::GString {
pub fn escape_nul(string: impl IntoGStr) -> crate::GString {
unsafe {
from_glib_full(ffi::g_regex_escape_nul(
string.as_ptr() as *const _,
string.len() as _,
))
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: &str) -> crate::GString {
pub fn escape_string(string: impl IntoGStr) -> crate::GString {
unsafe {
from_glib_full(ffi::g_regex_escape_string(
string.as_ptr() as *const _,
string.len() as _,
))
string.run_with_gstr(|string| {
from_glib_full(ffi::g_regex_escape_string(
string.to_glib_none().0,
string.len() as _,
))
})
}
}

Expand Down Expand Up @@ -77,153 +81,179 @@ impl Regex {
#[doc(alias = "g_regex_replace")]
pub fn replace(
&self,
string: &str,
string: impl IntoGStr,
start_position: i32,
replacement: &str,
replacement: impl IntoGStr,
match_options: RegexMatchFlags,
) -> Result<crate::GString, crate::Error> {
unsafe {
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))
}
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(&self, string: &str, match_options: RegexMatchFlags) -> Option<MatchInfo> {
pub fn match_all(
&self,
string: impl IntoGStr,
match_options: RegexMatchFlags,
) -> Option<MatchInfo> {
self.match_all_full(string, 0, match_options).ok()
}

#[doc(alias = "g_regex_match_all_full")]
pub fn match_all_full(
&self,
string: &str,
string: impl IntoGStr,
start_position: i32,
match_options: RegexMatchFlags,
) -> Result<MatchInfo, 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.as_ptr() as *const _,
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))
}
string.run_with_gstr(|string| {
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_(&self, string: &str, match_options: RegexMatchFlags) -> Option<MatchInfo> {
pub fn match_(
&self,
string: impl IntoGStr,
match_options: RegexMatchFlags,
) -> Option<MatchInfo> {
self.match_full(string, 0, match_options).ok()
}

#[doc(alias = "g_regex_match_full")]
pub fn match_full(
&self,
string: &str,
string: impl IntoGStr,
start_position: i32,
match_options: RegexMatchFlags,
) -> Result<MatchInfo, 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.as_ptr() as *const _,
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))
}
string.run_with_gstr(|string| {
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: &str,
string: impl IntoGStr,
start_position: i32,
replacement: &str,
replacement: impl IntoGStr,
match_options: RegexMatchFlags,
) -> Result<crate::GString, crate::Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::g_regex_replace_literal(
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))
}
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: &str, match_options: RegexMatchFlags) -> Vec<crate::GString> {
pub fn split(
&self,
string: impl IntoGStr,
match_options: RegexMatchFlags,
) -> Vec<crate::GString> {
self.split_full(string, 0, match_options, 0)
.unwrap_or_default()
}

#[doc(alias = "g_regex_split_full")]
pub fn split_full(
&self,
string: &str,
string: impl IntoGStr,
start_position: i32,
match_options: RegexMatchFlags,
max_tokens: i32,
) -> Result<Vec<crate::GString>, crate::Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::g_regex_split_full(
self.to_glib_none().0,
string.as_ptr() as *const _,
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))
}
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))
}
})
}
}

Expand Down

0 comments on commit b8c20b1

Please sign in to comment.