Skip to content

Add OsStr::from_bytes() #26730

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use mem;
use string::String;
use ops;
use cmp;
use str;
use hash::{Hash, Hasher};
use vec::Vec;

Expand Down Expand Up @@ -265,6 +266,23 @@ impl OsStr {
}
}

/// Converts a byte slice to an `OsStr` slice.
///
/// # Platform behavior
///
/// On Unix systems, this is a no-op.
///
/// On Windows systems, only UTF-8 byte sequences will successfully
/// convert; non UTF-8 data will produce `None`.
#[unstable(feature = "convert", reason = "recently added")]
pub fn from_bytes_opt(bytes: &[u8]) -> Option<&OsStr> {
if cfg!(windows) {
str::from_utf8(bytes).ok().map(|s| s.as_ref())
} else {
Some(unsafe { mem::transmute(bytes) })
}
}

/// Creates a `CString` containing this `OsStr` data.
///
/// Fails if the `OsStr` contains interior nulls.
Expand Down
49 changes: 49 additions & 0 deletions src/test/run-pass/osstr_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(convert)]

use std::ffi::{OsStr, OsString};

fn main() {
// Valid UTF-8
let vec1: Vec<u8> = b"t\xC3\xA9st".to_vec();
let oso1: OsString = OsString::from_bytes(vec1).unwrap();
assert!(oso1.to_bytes() == Some(b"t\xC3\xA9st"));
assert!(oso1.to_str() == Some("t\u{E9}st"));
// Not UTF-8
let vec2: Vec<u8> = b"t\xE9st".to_vec();
let oso2: OsString = OsString::from_bytes(vec2).unwrap();
if cfg!(windows) {
assert!(oso2.to_bytes() == None);
} else {
assert!(oso2.to_bytes() == Some(b"t\xE9st"));
}
assert_eq!(oso2.to_str(), None);

// Valid UTF-8
let by1: &[u8] = b"t\xC3\xA9st";
let oss1: &OsStr = OsStr::from_bytes_opt(by1).unwrap();
assert_eq!(oss1.to_bytes().unwrap().as_ptr(), by1.as_ptr());
assert_eq!(oss1.to_str().unwrap().as_ptr(), by1.as_ptr());
// Not UTF-8
let by2: &[u8] = b"t\xE9st";
let oss2: &OsStr = OsStr::from_bytes_opt(by2).unwrap();
if cfg!(windows) {
assert_eq!(oss2.to_bytes(), None);
} else {
assert_eq!(oss2.to_bytes().unwrap().as_ptr(), by2.as_ptr());
}
assert_eq!(oss2.to_str(), None);

if cfg!(windows) {
// FIXME: needs valid-windows-utf16-invalid-unicode test cases
}
}