// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::ffi;
use glib::translate::*;

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

    match fn {
        ref => |ptr| ffi::vte_regex_ref(ptr),
        unref => |ptr| ffi::vte_regex_unref(ptr),
        type_ => || ffi::vte_regex_get_type(),
    }
}

impl Regex {
    #[doc(alias = "vte_regex_new_for_match")]
    #[doc(alias = "new_for_match")]
    pub fn for_match(pattern: &str, flags: u32) -> Result<Regex, glib::Error> {
        assert_initialized_main_thread!();
        let pattern_length = pattern.len() as _;
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::vte_regex_new_for_match(
                pattern.to_glib_none().0,
                pattern_length,
                flags,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(feature = "v0_76")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v0_76")))]
    #[doc(alias = "vte_regex_new_for_match_full")]
    #[doc(alias = "new_for_match_full")]
    pub fn for_match_full(
        pattern: &str,
        flags: u32,
        extra_flags: u32,
    ) -> Result<(Regex, usize), glib::Error> {
        assert_initialized_main_thread!();
        let pattern_length = pattern.len() as _;
        unsafe {
            let mut error_offset = std::mem::MaybeUninit::uninit();
            let mut error = std::ptr::null_mut();
            let ret = ffi::vte_regex_new_for_match_full(
                pattern.to_glib_none().0,
                pattern_length,
                flags,
                extra_flags,
                error_offset.as_mut_ptr(),
                &mut error,
            );
            if error.is_null() {
                Ok((from_glib_full(ret), error_offset.assume_init()))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "vte_regex_new_for_search")]
    #[doc(alias = "new_for_search")]
    pub fn for_search(pattern: &str, flags: u32) -> Result<Regex, glib::Error> {
        assert_initialized_main_thread!();
        let pattern_length = pattern.len() as _;
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::vte_regex_new_for_search(
                pattern.to_glib_none().0,
                pattern_length,
                flags,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(feature = "v0_76")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v0_76")))]
    #[doc(alias = "vte_regex_new_for_search_full")]
    #[doc(alias = "new_for_search_full")]
    pub fn for_search_full(
        pattern: &str,
        flags: u32,
        extra_flags: u32,
    ) -> Result<(Regex, usize), glib::Error> {
        assert_initialized_main_thread!();
        let pattern_length = pattern.len() as _;
        unsafe {
            let mut error_offset = std::mem::MaybeUninit::uninit();
            let mut error = std::ptr::null_mut();
            let ret = ffi::vte_regex_new_for_search_full(
                pattern.to_glib_none().0,
                pattern_length,
                flags,
                extra_flags,
                error_offset.as_mut_ptr(),
                &mut error,
            );
            if error.is_null() {
                Ok((from_glib_full(ret), error_offset.assume_init()))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "vte_regex_jit")]
    pub fn jit(&self, flags: u32) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::vte_regex_jit(self.to_glib_none().0, flags, &mut error);
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "vte_regex_substitute")]
    pub fn substitute(
        &self,
        subject: &str,
        replacement: &str,
        flags: u32,
    ) -> Result<glib::GString, glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::vte_regex_substitute(
                self.to_glib_none().0,
                subject.to_glib_none().0,
                replacement.to_glib_none().0,
                flags,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }
}
