Skip to content

Commit ff3b5df

Browse files
committed
Add some convenience methods to go from CStr -> str
A common problem when working with FFI right now is converting from raw C strings into `&str` or `String`. Right now you're required to say something like let cstr = unsafe { CStr::from_ptr(ptr) }; let result = str::from_utf8(cstr.to_bytes()); This is slightly awkward, and is not particularly intuitive for people who haven't used the ffi module before. We can do a bit better by providing some convenience methods on CStr: fn to_str(&self) -> Result<&str, str::Utf8Error> fn to_string_lossy(&self) -> Cow<str> This will make it immediately apparent to new users of CStr how to get a string from a raw C string, so they can say: let s = unsafe { CStr::from_ptr(ptr).to_string_lossy() };
1 parent dd4dad8 commit ff3b5df

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/libstd/ffi/c_str.rs

+72
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#![unstable(feature = "std_misc")]
1212

13+
use borrow::Cow;
1314
use convert::{Into, From};
1415
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
1516
use error::Error;
@@ -22,6 +23,7 @@ use ops::Deref;
2223
use option::Option::{self, Some, None};
2324
use result::Result::{self, Ok, Err};
2425
use slice;
26+
use str;
2527
use string::String;
2628
use vec::Vec;
2729

@@ -114,6 +116,26 @@ pub struct CString {
114116
/// work(&s);
115117
/// }
116118
/// ```
119+
///
120+
/// Converting a foreign C string into a Rust `String`
121+
///
122+
/// ```no_run
123+
/// # #![feature(libc)]
124+
/// extern crate libc;
125+
/// use std::ffi::CStr;
126+
///
127+
/// extern { fn my_string() -> *const libc::c_char; }
128+
///
129+
/// fn my_string_safe() -> String {
130+
/// unsafe {
131+
/// CStr::from_ptr(my_string()).to_string_lossy().to_owned()
132+
/// }
133+
/// }
134+
///
135+
/// fn main() {
136+
/// println!("string: {}", my_string_safe());
137+
/// }
138+
/// ```
117139
#[derive(Hash)]
118140
#[stable(feature = "rust1", since = "1.0.0")]
119141
pub struct CStr {
@@ -328,6 +350,39 @@ impl CStr {
328350
pub fn to_bytes_with_nul(&self) -> &[u8] {
329351
unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.inner) }
330352
}
353+
354+
/// Yields a `&str` slice if the `CStr` contains valid UTF-8.
355+
///
356+
/// This function will calculate the length of this string and check for
357+
/// UTF-8 validity, and then return the `&str` if it's valid.
358+
///
359+
/// > **Note**: This method is currently implemented to check for validity
360+
/// > after a 0-cost cast, but it is planned to alter its definition in the
361+
/// > future to perform the length calculation in addition to the UTF-8
362+
/// > check whenever this method is called.
363+
#[unstable(feature = "cstr_to_str", reason = "recently added")]
364+
pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
365+
// NB: When CStr is changed to perform the length check in .to_bytes() instead of in
366+
// from_ptr(), it may be worth considering if this should be rewritten to do the UTF-8
367+
// check inline with the length calculation instead of doing it afterwards.
368+
str::from_utf8(self.to_bytes())
369+
}
370+
371+
/// Converts a `CStr` into a `Cow<str>`.
372+
///
373+
/// This function will calculate the length of this string (which normally
374+
/// requires a linear amount of work to be done) and then return the
375+
/// resulting slice as a `Cow<str>`, replacing any invalid UTF-8 sequences
376+
/// with `U+FFFD REPLACEMENT CHARACTER`.
377+
///
378+
/// > **Note**: This method is currently implemented to check for validity
379+
/// > after a 0-cost cast, but it is planned to alter its definition in the
380+
/// > future to perform the length calculation in addition to the UTF-8
381+
/// > check whenever this method is called.
382+
#[unstable(feature = "cstr_to_str", reason = "recently added")]
383+
pub fn to_string_lossy(&self) -> Cow<str> {
384+
String::from_utf8_lossy(self.to_bytes())
385+
}
331386
}
332387

333388
#[stable(feature = "rust1", since = "1.0.0")]
@@ -356,6 +411,7 @@ mod tests {
356411
use prelude::v1::*;
357412
use super::*;
358413
use libc;
414+
use borrow::Cow::{Borrowed, Owned};
359415

360416
#[test]
361417
fn c_to_rust() {
@@ -405,4 +461,20 @@ mod tests {
405461
assert_eq!(s.to_bytes_with_nul(), b"12\0");
406462
}
407463
}
464+
465+
#[test]
466+
fn to_str() {
467+
let data = b"123\xE2\x80\xA6\0";
468+
let ptr = data.as_ptr() as *const libc::c_char;
469+
unsafe {
470+
assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…"));
471+
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…"));
472+
}
473+
let data = b"123\xE2\0";
474+
let ptr = data.as_ptr() as *const libc::c_char;
475+
unsafe {
476+
assert!(CStr::from_ptr(ptr).to_str().is_err());
477+
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
478+
}
479+
}
408480
}

0 commit comments

Comments
 (0)