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

Add OsStringExt::from_wide_ptr for windows #36671

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libstd/sys/windows/ext/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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_null", issue = "0")]
unsafe fn from_wide_null(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")]
Expand Down