From bfe68e6a833699c91c3959026e9271e05ebcfeec Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 9 Apr 2023 13:51:32 +0200 Subject: [PATCH] common.rs: helper to convert from C-string of unknown length to wide On the C++ side we have an overload that called std::wcslen(), this is the equivalent one. --- fish-rust/src/common.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index ab1b23c72..0e89d6e99 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -1110,6 +1110,19 @@ pub fn str2wcstring(inp: &[u8]) -> WString { result } +pub fn cstr2wcstring(input: &[u8]) -> WString { + let strlen = input.iter().position(|c| *c == b'\0').unwrap(); + str2wcstring(&input[0..strlen]) +} + +pub fn charptr2wcstring(input: *const libc::c_char) -> WString { + let input: &[u8] = unsafe { + let strlen = libc::strlen(input); + slice::from_raw_parts(input.cast(), strlen) + }; + str2wcstring(input) +} + /// Returns a newly allocated multibyte character string equivalent of the specified wide character /// string. ///