Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] Add missing string manipulation functions #1418

Open
AdrianVovk opened this issue May 29, 2024 · 2 comments
Open

[FEATURE REQUEST] Add missing string manipulation functions #1418

AdrianVovk opened this issue May 29, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@AdrianVovk
Copy link

Many things in gstrfuncs.h don't have wrappers. Most importantly, I need g_str_tokenize_and_fold to implement searching in my app.

Other missing functions I've run into:

  • g_str_match_string
  • g_str_to_ascii
  • Potentially others
@AdrianVovk AdrianVovk added the enhancement New feature or request label May 29, 2024
@AdrianVovk
Copy link
Author

Also, g_str_tokenize_and_fold has an optional out variable, so maybe something like this:

fn tokenize_and_casefold_internal(
    arg: &str,
    translit_locale: Option<&str>,
    alts: bool
) -> (glib::StrV, Option<glib::StrV>) {
    // https://github.com/gtk-rs/gtk-rs-core/issues/1418
    use glib::translate::*;
    arg.run_with_gstr(|arg| {
        translit_locale.run_with_gstr(|translit_locale| {
            let mut alternates: *mut *mut std::ffi::c_char = std::ptr::null_mut();
            let alternates_ptr = if alts {
                &mut alternates
            } else {
                std::ptr::null_mut()
            };
            unsafe {
                let result = glib::ffi::g_str_tokenize_and_fold(
                    arg.to_glib_none().0,
                    translit_locale.to_glib_none().0,
                    alternates_ptr
                );
                (
                    glib::StrV::from_glib_full(result),
                    alts.then(|| glib::StrV::from_glib_full(alternates))
                )
            }
        })
    })
}

pub fn tokenize_and_casefold(arg: &str) -> glib::StrV {
    tokenize_and_casefold_internal(arg, None, false).0
}

pub fn tokenize_and_casefold_with_alts(
    arg: &str,
    translit_locale: Option<&str>
) -> (glib::StrV, glib::StrV) {
    let res = tokenize_and_casefold_internal(arg, translit_locale, true);
    (res.0, res.1.unwrap())
}

@sdroege
Copy link
Member

sdroege commented May 31, 2024

It would seem more useful to use Rust APIs for this kind of tasks. If you want to provide a PR that adds bindings for the functions, I'd review that, but it doesn't seem very useful to me. Rust string parsing functions in std and external crates are generally better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants