Prefer taking native Rust strings instead of wcharz_t

We should only be dealing with wcharz_t at the language boundary.
Rust callers should prefer the equivalent &wstr.
Since wcsfilecmp() is no longer exposed directly it can take &wstr only.
This commit is contained in:
Johannes Altmanninger 2023-02-04 12:29:06 +01:00
parent a446a16471
commit dcca3cfe3c
2 changed files with 8 additions and 20 deletions

View file

@ -166,7 +166,7 @@ impl features_t {
/// The special group name "all" may be used for those who like to live on the edge.
/// Unknown features are silently ignored.
#[widestrs]
pub fn set_from_string(&mut self, str: wcharz_t) {
pub fn set_from_string<'a>(&mut self, str: impl Into<&'a wstr>) {
let str: &wstr = str.into();
let whitespace = "\t\n\0x0B\0x0C\r "L.as_char_slice();
for entry in str.as_char_slice().split(|c| *c == ',') {
@ -232,12 +232,10 @@ pub fn feature_metadata() -> [feature_metadata_t; metadata.len()] {
#[test]
#[widestrs]
fn test_feature_flags() {
use crate::wchar_ffi::wcharz;
let mut f = features_t::new();
f.set_from_string(wcharz!("stderr-nocaret,nonsense"L));
f.set_from_string("stderr-nocaret,nonsense"L);
assert!(f.test(feature_flag_t::stderr_nocaret));
f.set_from_string(wcharz!("stderr-nocaret,no-stderr-nocaret,nonsense"L));
f.set_from_string("stderr-nocaret,no-stderr-nocaret,nonsense"L);
assert!(f.test(feature_flag_t::stderr_nocaret));
// Ensure every metadata is represented once.

View file

@ -30,11 +30,11 @@ fn ordering_to_int(ord: Ordering) -> i32 {
}
fn wcsfilecmp_glob_ffi(a: wcharz_t, b: wcharz_t) -> i32 {
ordering_to_int(wcsfilecmp_glob(a, b))
ordering_to_int(wcsfilecmp_glob(a.into(), b.into()))
}
fn wcsfilecmp_ffi(a: wcharz_t, b: wcharz_t) -> i32 {
ordering_to_int(wcsfilecmp(a, b))
ordering_to_int(wcsfilecmp(a.into(), b.into()))
}
/// Compares two wide character strings with an (arguably) intuitive ordering. This function tries
@ -64,10 +64,7 @@ fn wcsfilecmp_ffi(a: wcharz_t, b: wcharz_t) -> i32 {
/// a 'file1' and 'File1' will not be considered identical, and hence their internal sort order is
/// not arbitrary, but the names 'file1', 'File2' and 'file3' will still be sorted in the order
/// given above.
pub fn wcsfilecmp(a: wcharz_t, b: wcharz_t) -> Ordering {
// TODO This should return `std::cmp::Ordering`.
let a: &wstr = a.into();
let b: &wstr = b.into();
pub fn wcsfilecmp(a: &wstr, b: &wstr) -> Ordering {
let mut retval = Ordering::Equal;
let mut ai = 0;
let mut bi = 0;
@ -134,10 +131,7 @@ pub fn wcsfilecmp(a: wcharz_t, b: wcharz_t) -> Ordering {
}
/// wcsfilecmp, but frozen in time for glob usage.
pub fn wcsfilecmp_glob(a: wcharz_t, b: wcharz_t) -> Ordering {
// TODO This should return `std::cmp::Ordering`.
let a: &wstr = a.into();
let b: &wstr = b.into();
pub fn wcsfilecmp_glob(a: &wstr, b: &wstr) -> Ordering {
let mut retval = Ordering::Equal;
let mut ai = 0;
let mut bi = 0;
@ -268,14 +262,10 @@ fn wcsfilecmp_leading_digits(a: &wstr, b: &wstr) -> (Ordering, usize, usize) {
#[test]
fn test_wcsfilecmp() {
use crate::wchar::L;
use crate::wchar_ffi::wcharz;
macro_rules! validate {
($str1:expr, $str2:expr, $expected_rc:expr) => {
assert_eq!(
wcsfilecmp(wcharz!(L!($str1)), wcharz!(L!($str2))),
$expected_rc
)
assert_eq!(wcsfilecmp(L!($str1), L!($str2)), $expected_rc)
};
}