fish-shell/fish-rust/src/wutil/gettext.rs
Johannes Altmanninger c6756e9324 Canonicalize some wide string imports
wchar.rs should not import let alone reexport FFI strings.
Stop re-exporting utf32str! because we use L! instead.

In wchar_ffi.rs, stop re-exporting cxx::CxxWString because that hasn't
seen adoption.

I think we should use re-exports only for aliases like "wstr" or for aliases
into internal modules.
So I'd probably remove `pub use wchar_ffi::wcharz_t = crate::ffi::wcharz_t`
as well.
2023-03-05 10:32:20 +01:00

46 lines
1.4 KiB
Rust

use crate::ffi;
use crate::wchar::wstr;
use crate::wchar_ffi::{wchar_t, wcslen};
/// Support for wgettext.
/// Implementation detail for wgettext!.
pub fn wgettext_impl_do_not_use_directly(text: &[wchar_t]) -> &'static wstr {
assert_eq!(text.last(), Some(&0), "should be nul-terminated");
let res: *const wchar_t = ffi::wgettext_ptr(text.as_ptr());
let slice = unsafe { std::slice::from_raw_parts(res as *const u32, wcslen(res)) };
wstr::from_slice(slice).expect("Invalid UTF-32")
}
/// Get a (possibly translated) string from a string literal.
/// This returns a &'static wstr.
macro_rules! wgettext {
($string:expr) => {
crate::wutil::gettext::wgettext_impl_do_not_use_directly(
crate::wchar_ffi::u32cstr!($string).as_slice_with_nul(),
)
};
}
pub(crate) use wgettext;
/// Like wgettext, but applies a sprintf format string.
/// The result is a WString.
macro_rules! wgettext_fmt {
(
$string:expr // format string
$(, $args:expr)* // list of expressions
$(,)? // optional trailing comma
) => {
crate::wutil::sprintf!(&crate::wutil::wgettext!($string), $($args),*)
};
}
pub(crate) use wgettext_fmt;
use crate::ffi_tests::add_test;
add_test!("test_untranslated", || {
let s: &'static wstr = wgettext!("abc");
assert_eq!(s, "abc");
let s2: &'static wstr = wgettext!("static");
assert_eq!(s2, "static");
});