Skip to content

Commit

Permalink
Return PtrSlice instead of Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmhewitt committed Oct 27, 2023
1 parent b8c20b1 commit 41e5991
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions glib/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
//! This module is inefficient and should not be used by Rust programs except for
//! compatibility with GLib.Regex based APIs.

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

impl Regex {
Expand Down Expand Up @@ -223,7 +226,7 @@ impl Regex {
&self,
string: impl IntoGStr,
match_options: RegexMatchFlags,
) -> Vec<crate::GString> {
) -> PtrSlice<GStringPtr> {
self.split_full(string, 0, match_options, 0)
.unwrap_or_default()
}
Expand All @@ -235,7 +238,7 @@ impl Regex {
start_position: i32,
match_options: RegexMatchFlags,
max_tokens: i32,
) -> Result<Vec<crate::GString>, crate::Error> {
) -> Result<PtrSlice<GStringPtr>, crate::Error> {
unsafe {
let mut error = ptr::null_mut();
string.run_with_gstr(|string| {
Expand Down Expand Up @@ -263,7 +266,7 @@ impl Regex {
string: impl IntoGStr,
compile_options: RegexCompileFlags,
match_options: RegexMatchFlags,
) -> Vec<crate::GString> {
) -> PtrSlice<GStringPtr> {
pattern.run_with_gstr(|pattern| {
string.run_with_gstr(|string| unsafe {
FromGlibPtrContainer::from_glib_full(ffi::g_regex_split_simple(
Expand Down Expand Up @@ -299,4 +302,23 @@ mod tests {

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], ".");
}
}

0 comments on commit 41e5991

Please sign in to comment.