Skip to content

Commit

Permalink
glib: Implement Regex
Browse files Browse the repository at this point in the history
These modules are inefficient and should not be used by Rust programs except for
compatibility with GLib.Regex based APIs.

All methods are implemented except for g_regex_replace_eval
  • Loading branch information
davidmhewitt authored and bilelmoussaoui committed Oct 27, 2023
1 parent 203c957 commit 03bce03
Show file tree
Hide file tree
Showing 8 changed files with 767 additions and 0 deletions.
80 changes: 80 additions & 0 deletions glib/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ generate = [
"GLib.NormalizeMode",
"GLib.OptionArg",
"GLib.OptionFlags",
"GLib.RegexCompileFlags",
"GLib.RegexMatchFlags",
"GLib.SeekType",
"GLib.SpawnFlags",
"GLib.Time",
Expand Down Expand Up @@ -714,6 +716,84 @@ status = "generate"
name = "get_user_data"
ignore = true # unsafe pointer

[[object]]
name = "GLib.MatchInfo"
status = "generate"
[[object.function]]
name = "expand_references"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "fetch_named"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "fetch_named_pos"
# impl IntoGStr for parameters instead of &str
manual = true

[[object]]
name = "GLib.Regex"
status = "generate"
[[object.function]]
name = "check_replacement"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "escape_nul"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "escape_string"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "match"
# implement in terms of match_full
manual = true
[[object.function]]
name = "match_all"
# implement in terms of match_all_full
manual = true
[[object.function]]
name = "match_all_full"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "match_simple"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "match_full"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "replace"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "replace_literal"
# impl IntoGStr for parameters instead of &str
manual = true
[[object.function]]
name = "split"
# implement in terms of split_full
manual = true
[[object.function]]
name = "split_full"
# impl IntoGStr for parameters instead of &str
# return slice of str pointers
manual = true
[[object.function]]
name = "split_simple"
# impl IntoGStr for parameters instead of &str
# return slice of str pointers
manual = true
[[object.function]]
name = "get_string_number"
# impl IntoGStr for parameters instead of &str
manual = true

[[object]]
name = "GLib.Source"
status = "generate"
Expand Down
120 changes: 120 additions & 0 deletions glib/src/auto/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,126 @@ impl FromGlib<ffi::GOptionFlags> for OptionFlags {
}
}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[doc(alias = "GRegexCompileFlags")]
pub struct RegexCompileFlags: u32 {
#[doc(alias = "G_REGEX_DEFAULT")]
const DEFAULT = ffi::G_REGEX_DEFAULT as _;
#[doc(alias = "G_REGEX_CASELESS")]
const CASELESS = ffi::G_REGEX_CASELESS as _;
#[doc(alias = "G_REGEX_MULTILINE")]
const MULTILINE = ffi::G_REGEX_MULTILINE as _;
#[doc(alias = "G_REGEX_DOTALL")]
const DOTALL = ffi::G_REGEX_DOTALL as _;
#[doc(alias = "G_REGEX_EXTENDED")]
const EXTENDED = ffi::G_REGEX_EXTENDED as _;
#[doc(alias = "G_REGEX_ANCHORED")]
const ANCHORED = ffi::G_REGEX_ANCHORED as _;
#[doc(alias = "G_REGEX_DOLLAR_ENDONLY")]
const DOLLAR_ENDONLY = ffi::G_REGEX_DOLLAR_ENDONLY as _;
#[doc(alias = "G_REGEX_UNGREEDY")]
const UNGREEDY = ffi::G_REGEX_UNGREEDY as _;
#[doc(alias = "G_REGEX_RAW")]
const RAW = ffi::G_REGEX_RAW as _;
#[doc(alias = "G_REGEX_NO_AUTO_CAPTURE")]
const NO_AUTO_CAPTURE = ffi::G_REGEX_NO_AUTO_CAPTURE as _;
#[doc(alias = "G_REGEX_OPTIMIZE")]
const OPTIMIZE = ffi::G_REGEX_OPTIMIZE as _;
#[doc(alias = "G_REGEX_FIRSTLINE")]
const FIRSTLINE = ffi::G_REGEX_FIRSTLINE as _;
#[doc(alias = "G_REGEX_DUPNAMES")]
const DUPNAMES = ffi::G_REGEX_DUPNAMES as _;
#[doc(alias = "G_REGEX_NEWLINE_CR")]
const NEWLINE_CR = ffi::G_REGEX_NEWLINE_CR as _;
#[doc(alias = "G_REGEX_NEWLINE_LF")]
const NEWLINE_LF = ffi::G_REGEX_NEWLINE_LF as _;
#[doc(alias = "G_REGEX_NEWLINE_CRLF")]
const NEWLINE_CRLF = ffi::G_REGEX_NEWLINE_CRLF as _;
#[doc(alias = "G_REGEX_NEWLINE_ANYCRLF")]
const NEWLINE_ANYCRLF = ffi::G_REGEX_NEWLINE_ANYCRLF as _;
#[doc(alias = "G_REGEX_BSR_ANYCRLF")]
const BSR_ANYCRLF = ffi::G_REGEX_BSR_ANYCRLF as _;
#[doc(alias = "G_REGEX_JAVASCRIPT_COMPAT")]
const JAVASCRIPT_COMPAT = ffi::G_REGEX_JAVASCRIPT_COMPAT as _;
}
}

#[doc(hidden)]
impl IntoGlib for RegexCompileFlags {
type GlibType = ffi::GRegexCompileFlags;

#[inline]
fn into_glib(self) -> ffi::GRegexCompileFlags {
self.bits()
}
}

#[doc(hidden)]
impl FromGlib<ffi::GRegexCompileFlags> for RegexCompileFlags {
#[inline]
unsafe fn from_glib(value: ffi::GRegexCompileFlags) -> Self {
Self::from_bits_truncate(value)
}
}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[doc(alias = "GRegexMatchFlags")]
pub struct RegexMatchFlags: u32 {
#[doc(alias = "G_REGEX_MATCH_DEFAULT")]
const DEFAULT = ffi::G_REGEX_MATCH_DEFAULT as _;
#[doc(alias = "G_REGEX_MATCH_ANCHORED")]
const ANCHORED = ffi::G_REGEX_MATCH_ANCHORED as _;
#[doc(alias = "G_REGEX_MATCH_NOTBOL")]
const NOTBOL = ffi::G_REGEX_MATCH_NOTBOL as _;
#[doc(alias = "G_REGEX_MATCH_NOTEOL")]
const NOTEOL = ffi::G_REGEX_MATCH_NOTEOL as _;
#[doc(alias = "G_REGEX_MATCH_NOTEMPTY")]
const NOTEMPTY = ffi::G_REGEX_MATCH_NOTEMPTY as _;
#[doc(alias = "G_REGEX_MATCH_PARTIAL")]
const PARTIAL = ffi::G_REGEX_MATCH_PARTIAL as _;
#[doc(alias = "G_REGEX_MATCH_NEWLINE_CR")]
const NEWLINE_CR = ffi::G_REGEX_MATCH_NEWLINE_CR as _;
#[doc(alias = "G_REGEX_MATCH_NEWLINE_LF")]
const NEWLINE_LF = ffi::G_REGEX_MATCH_NEWLINE_LF as _;
#[doc(alias = "G_REGEX_MATCH_NEWLINE_CRLF")]
const NEWLINE_CRLF = ffi::G_REGEX_MATCH_NEWLINE_CRLF as _;
#[doc(alias = "G_REGEX_MATCH_NEWLINE_ANY")]
const NEWLINE_ANY = ffi::G_REGEX_MATCH_NEWLINE_ANY as _;
#[doc(alias = "G_REGEX_MATCH_NEWLINE_ANYCRLF")]
const NEWLINE_ANYCRLF = ffi::G_REGEX_MATCH_NEWLINE_ANYCRLF as _;
#[doc(alias = "G_REGEX_MATCH_BSR_ANYCRLF")]
const BSR_ANYCRLF = ffi::G_REGEX_MATCH_BSR_ANYCRLF as _;
#[doc(alias = "G_REGEX_MATCH_BSR_ANY")]
const BSR_ANY = ffi::G_REGEX_MATCH_BSR_ANY as _;
#[doc(alias = "G_REGEX_MATCH_PARTIAL_SOFT")]
const PARTIAL_SOFT = ffi::G_REGEX_MATCH_PARTIAL_SOFT as _;
#[doc(alias = "G_REGEX_MATCH_PARTIAL_HARD")]
const PARTIAL_HARD = ffi::G_REGEX_MATCH_PARTIAL_HARD as _;
#[doc(alias = "G_REGEX_MATCH_NOTEMPTY_ATSTART")]
const NOTEMPTY_ATSTART = ffi::G_REGEX_MATCH_NOTEMPTY_ATSTART as _;
}
}

#[doc(hidden)]
impl IntoGlib for RegexMatchFlags {
type GlibType = ffi::GRegexMatchFlags;

#[inline]
fn into_glib(self) -> ffi::GRegexMatchFlags {
self.bits()
}
}

#[doc(hidden)]
impl FromGlib<ffi::GRegexMatchFlags> for RegexMatchFlags {
#[inline]
unsafe fn from_glib(value: ffi::GRegexMatchFlags) -> Self {
Self::from_bits_truncate(value)
}
}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[doc(alias = "GSpawnFlags")]
Expand Down
91 changes: 91 additions & 0 deletions glib/src/auto/match_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{translate::*, Error, Regex};

crate::wrapper! {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MatchInfo(Shared<ffi::GMatchInfo>);

match fn {
ref => |ptr| ffi::g_match_info_ref(ptr),
unref => |ptr| ffi::g_match_info_unref(ptr),
type_ => || ffi::g_match_info_get_type(),
}
}

impl MatchInfo {
#[doc(alias = "g_match_info_fetch")]
pub fn fetch(&self, match_num: i32) -> Option<crate::GString> {
unsafe { from_glib_full(ffi::g_match_info_fetch(self.to_glib_none().0, match_num)) }
}

#[doc(alias = "g_match_info_fetch_all")]
pub fn fetch_all(&self) -> Vec<crate::GString> {
unsafe {
FromGlibPtrContainer::from_glib_full(ffi::g_match_info_fetch_all(self.to_glib_none().0))
}
}

#[doc(alias = "g_match_info_fetch_pos")]
pub fn fetch_pos(&self, match_num: i32) -> Option<(i32, i32)> {
unsafe {
let mut start_pos = std::mem::MaybeUninit::uninit();
let mut end_pos = std::mem::MaybeUninit::uninit();
let ret = from_glib(ffi::g_match_info_fetch_pos(
self.to_glib_none().0,
match_num,
start_pos.as_mut_ptr(),
end_pos.as_mut_ptr(),
));
if ret {
Some((start_pos.assume_init(), end_pos.assume_init()))
} else {
None
}
}
}

#[doc(alias = "g_match_info_get_match_count")]
#[doc(alias = "get_match_count")]
pub fn match_count(&self) -> i32 {
unsafe { ffi::g_match_info_get_match_count(self.to_glib_none().0) }
}

#[doc(alias = "g_match_info_get_regex")]
#[doc(alias = "get_regex")]
pub fn regex(&self) -> Regex {
unsafe { from_glib_none(ffi::g_match_info_get_regex(self.to_glib_none().0)) }
}

#[doc(alias = "g_match_info_get_string")]
#[doc(alias = "get_string")]
pub fn string(&self) -> crate::GString {
unsafe { from_glib_none(ffi::g_match_info_get_string(self.to_glib_none().0)) }
}

#[doc(alias = "g_match_info_is_partial_match")]
pub fn is_partial_match(&self) -> bool {
unsafe { from_glib(ffi::g_match_info_is_partial_match(self.to_glib_none().0)) }
}

#[doc(alias = "g_match_info_matches")]
pub fn matches(&self) -> bool {
unsafe { from_glib(ffi::g_match_info_matches(self.to_glib_none().0)) }
}

#[doc(alias = "g_match_info_next")]
pub fn next(&self) -> Result<(), crate::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::g_match_info_next(self.to_glib_none().0, &mut error);
debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
}
8 changes: 8 additions & 0 deletions glib/src/auto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub use self::main_loop::MainLoop;
mod markup_parse_context;
pub use self::markup_parse_context::MarkupParseContext;

mod match_info;
pub use self::match_info::MatchInfo;

mod regex;
pub use self::regex::Regex;

mod source;
pub use self::source::Source;

Expand Down Expand Up @@ -67,6 +73,8 @@ pub use self::flags::LogLevelFlags;
#[cfg_attr(docsrs, doc(cfg(feature = "v2_72")))]
pub use self::flags::MainContextFlags;
pub use self::flags::OptionFlags;
pub use self::flags::RegexCompileFlags;
pub use self::flags::RegexMatchFlags;
pub use self::flags::SpawnFlags;
#[cfg(feature = "v2_66")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_66")))]
Expand Down
Loading

0 comments on commit 03bce03

Please sign in to comment.