auto merge of #8296 : erickt/rust/remove-str-trailing-nulls, r=erickt
This PR fixes #7235 and #3371, which removes trailing nulls from `str` types. Instead, it replaces the creation of c strings with a new type, `std::c_str::CString`, which wraps a malloced byte array, and respects: * No interior nulls * Ends with a trailing null
This commit is contained in:
commit
60f5011005
47 changed files with 1237 additions and 620 deletions
|
|
@ -15,6 +15,7 @@ Dynamic library facilities.
|
|||
A simple wrapper over the platforms dynamic library facilities
|
||||
|
||||
*/
|
||||
use c_str::ToCStr;
|
||||
use cast;
|
||||
use path;
|
||||
use libc;
|
||||
|
|
@ -65,7 +66,7 @@ impl DynamicLibrary {
|
|||
// T but that feature is still unimplemented
|
||||
|
||||
let maybe_symbol_value = do dl::check_for_errors_in {
|
||||
do symbol.as_c_str |raw_string| {
|
||||
do symbol.to_c_str().with_ref |raw_string| {
|
||||
dl::symbol(self.handle, raw_string)
|
||||
}
|
||||
};
|
||||
|
|
@ -135,6 +136,7 @@ mod test {
|
|||
#[cfg(target_os = "macos")]
|
||||
#[cfg(target_os = "freebsd")]
|
||||
mod dl {
|
||||
use c_str::ToCStr;
|
||||
use libc;
|
||||
use path;
|
||||
use ptr;
|
||||
|
|
@ -143,7 +145,7 @@ mod dl {
|
|||
use result::*;
|
||||
|
||||
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
|
||||
do filename.to_str().as_c_str |raw_name| {
|
||||
do filename.to_c_str().with_ref |raw_name| {
|
||||
dlopen(raw_name, Lazy as libc::c_int)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -549,12 +549,14 @@ pub mod rt {
|
|||
// For strings, precision is the maximum characters
|
||||
// displayed
|
||||
let unpadded = match cv.precision {
|
||||
CountImplied => s,
|
||||
CountIs(max) => if (max as uint) < s.char_len() {
|
||||
s.slice(0, max as uint)
|
||||
} else {
|
||||
s
|
||||
}
|
||||
CountImplied => s,
|
||||
CountIs(max) => {
|
||||
if (max as uint) < s.char_len() {
|
||||
s.slice(0, max as uint)
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
};
|
||||
pad(cv, unpadded, None, PadNozero, buf);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
//! Runtime calls emitted by the compiler.
|
||||
|
||||
use c_str::ToCStr;
|
||||
use cast::transmute;
|
||||
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t};
|
||||
use str;
|
||||
|
|
@ -28,7 +29,7 @@ pub fn fail_bounds_check(file: *c_char, line: size_t,
|
|||
index: size_t, len: size_t) {
|
||||
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
|
||||
len as int, index as int);
|
||||
do msg.as_c_str |buf| {
|
||||
do msg.to_c_str().with_ref |buf| {
|
||||
fail_(buf, file, line);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue