From 3e2aeb1feb6b805086aa249a1c814efdb5ad1991 Mon Sep 17 00:00:00 2001 From: "a.navrotskiy" Date: Fri, 23 Sep 2016 17:15:29 +0300 Subject: [PATCH 1/2] Add OsStringExt::from_wide_ptr for windows (like CStr::from_ptr for wchar_t* null-terminated strings) --- src/libstd/sys/windows/ext/ffi.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libstd/sys/windows/ext/ffi.rs b/src/libstd/sys/windows/ext/ffi.rs index 253787546c1dc..f4eacc8c7e645 100644 --- a/src/libstd/sys/windows/ext/ffi.rs +++ b/src/libstd/sys/windows/ext/ffi.rs @@ -14,6 +14,7 @@ use ffi::{OsString, OsStr}; use sys::os_str::Buf; +use slice; use sys_common::wtf8::Wtf8Buf; use sys_common::{FromInner, AsInner}; @@ -30,6 +31,20 @@ pub trait OsStringExt { /// will always return the original code units. #[stable(feature = "rust1", since = "1.0.0")] fn from_wide(wide: &[u16]) -> Self; + + /// Creates an `OsString` from a potentially ill-formed null-terminated + /// UTF-16 pointer + /// + /// This is lossless: calling `.encode_wide()` on the resulting string + /// will always return the original code units. + #[unstable(feature = "from_wide_ptr", issue = "0")] + unsafe fn from_wide_ptr(ptr: *const u16) -> OsString { + let mut len = 0; + while *ptr.offset(len) != 0 { + len += 1; + } + OsStringExt::from_wide(slice::from_raw_parts(ptr, len as usize)) + } } #[stable(feature = "rust1", since = "1.0.0")] From f1a9c577f82b626c24b9d2538ad9a4ede8cd3ad8 Mon Sep 17 00:00:00 2001 From: "a.navrotskiy" Date: Tue, 27 Sep 2016 10:13:46 +0300 Subject: [PATCH 2/2] Rename OsStringExt::from_wide_ptr to OsStringExt::from_wide_null --- src/libstd/sys/windows/ext/ffi.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/sys/windows/ext/ffi.rs b/src/libstd/sys/windows/ext/ffi.rs index f4eacc8c7e645..38f61b23fd1ad 100644 --- a/src/libstd/sys/windows/ext/ffi.rs +++ b/src/libstd/sys/windows/ext/ffi.rs @@ -33,12 +33,12 @@ pub trait OsStringExt { fn from_wide(wide: &[u16]) -> Self; /// Creates an `OsString` from a potentially ill-formed null-terminated - /// UTF-16 pointer + /// UTF-16 pointer. /// /// This is lossless: calling `.encode_wide()` on the resulting string /// will always return the original code units. - #[unstable(feature = "from_wide_ptr", issue = "0")] - unsafe fn from_wide_ptr(ptr: *const u16) -> OsString { + #[unstable(feature = "from_wide_null", issue = "0")] + unsafe fn from_wide_null(ptr: *const u16) -> OsString { let mut len = 0; while *ptr.offset(len) != 0 { len += 1;