Skip to content

Split CString::new into CString::new and CString::new_owned #18122

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
4 changes: 2 additions & 2 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,13 +1348,13 @@ mod tests {
#[test]
fn test_slice_shift_char() {
let data = "ประเทศไทย中";
assert_eq!(data.slice_shift_char(), (Some('ป'), "ระเทศไทย中"));
assert_eq!(data.slice_shift_char(), Some(('ป', "ระเทศไทย中")));
}

#[test]
fn test_slice_shift_char_2() {
let empty = "";
assert_eq!(empty.slice_shift_char(), (None, ""));
assert_eq!(empty.slice_shift_char(), None);
}

#[test]
Expand Down
18 changes: 9 additions & 9 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,21 +1784,21 @@ pub trait StrSlice<'a> {
/// it. This does not allocate a new string; instead, it returns a
/// slice that point one character beyond the character that was
/// shifted. If the string does not contain any characters,
/// a tuple of None and an empty string is returned instead.
/// None is returned instead.
///
/// # Example
///
/// ```rust
/// let s = "Löwe 老虎 Léopard";
/// let (c, s1) = s.slice_shift_char();
/// assert_eq!(c, Some('L'));
/// let (c, s1) = s.slice_shift_char().unwrap();
/// assert_eq!(c, 'L');
/// assert_eq!(s1, "öwe 老虎 Léopard");
///
/// let (c, s2) = s1.slice_shift_char();
/// assert_eq!(c, Some('ö'));
/// let (c, s2) = s1.slice_shift_char().unwrap();
/// assert_eq!(c, 'ö');
/// assert_eq!(s2, "we 老虎 Léopard");
/// ```
fn slice_shift_char(&self) -> (Option<char>, &'a str);
fn slice_shift_char(&self) -> Option<(char, &'a str)>;

/// Returns the byte offset of an inner slice relative to an enclosing outer slice.
///
Expand Down Expand Up @@ -2148,13 +2148,13 @@ impl<'a> StrSlice<'a> for &'a str {
}

#[inline]
fn slice_shift_char(&self) -> (Option<char>, &'a str) {
fn slice_shift_char(&self) -> Option<(char, &'a str)> {
if self.is_empty() {
return (None, *self);
return None;
} else {
let CharRange {ch, next} = self.char_range_at(0u);
let next_s = unsafe { raw::slice_bytes(*self, next, self.len()) };
return (Some(ch), next_s);
return Some((ch, next_s));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/libcoretest/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fn test_ptr_array_each_with_len() {
let mut ctr = 0;
let mut iteration_count = 0;
array_each_with_len(arr.as_ptr(), arr.len(), |e| {
let actual = CString::new(e, false);
let actual = CString::new(e);
assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
ctr += 1;
iteration_count += 1;
Expand Down Expand Up @@ -241,7 +241,7 @@ fn test_ptr_array_each() {
let mut ctr = 0u;
let mut iteration_count = 0u;
array_each(arr_ptr, |e| {
let actual = CString::new(e, false);
let actual = CString::new(e);
assert_eq!(actual.as_str(), expected_arr[ctr].as_str());
ctr += 1;
iteration_count += 1;
Expand All @@ -255,7 +255,7 @@ fn test_ptr_array_each() {
fn test_ptr_array_each_with_len_null_ptr() {
unsafe {
array_each_with_len(0 as *const *const libc::c_char, 1, |e| {
CString::new(e, false).as_str().unwrap();
CString::new(e).as_str().unwrap();
});
}
}
Expand All @@ -264,7 +264,7 @@ fn test_ptr_array_each_with_len_null_ptr() {
fn test_ptr_array_each_null_ptr() {
unsafe {
array_each(0 as *const *const libc::c_char, |e| {
CString::new(e, false).as_str().unwrap();
CString::new(e).as_str().unwrap();
});
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/libglob/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,11 @@ impl Pattern {
return EntirePatternDoesntMatch;
}

let (some_c, next) = file.slice_shift_char();
if require_literal(some_c.unwrap()) {
let (c, next) = file.slice_shift_char().unwrap();
if require_literal(c) {
return SubPatternDoesntMatch;
}
prev_char.set(some_c);
prev_char.set(Some(c));
file = next;
}
}
Expand All @@ -417,8 +417,7 @@ impl Pattern {
return EntirePatternDoesntMatch;
}

let (some_c, next) = file.slice_shift_char();
let c = some_c.unwrap();
let (c, next) = file.slice_shift_char().unwrap();
let matches = match *token {
AnyChar => {
!require_literal(c)
Expand All @@ -445,7 +444,7 @@ impl Pattern {
if !matches {
return SubPatternDoesntMatch;
}
prev_char.set(some_c);
prev_char.set(Some(c));
file = next;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libnative/io/addrinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn get_error(s: c_int) -> IoError {
use std::c_str::CString;

let err_str = unsafe {
CString::new(gai_strerror(s), false).as_str().unwrap().to_string()
CString::new(gai_strerror(s)).as_str().unwrap().to_string()
};
IoError {
code: s as uint,
Expand Down
4 changes: 2 additions & 2 deletions src/libnative/io/file_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
use libc::{opendir, readdir_r, closedir};

fn prune(root: &CString, dirs: Vec<Path>) -> Vec<CString> {
let root = unsafe { CString::new(root.as_ptr(), false) };
let root = unsafe { CString::new(root.as_ptr()) };
let root = Path::new(root);

dirs.into_iter().filter(|path| {
Expand All @@ -376,7 +376,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
while unsafe { readdir_r(dir_ptr, ptr, &mut entry_ptr) == 0 } {
if entry_ptr.is_null() { break }
let cstr = unsafe {
CString::new(rust_list_dir_val(entry_ptr), false)
CString::new(rust_list_dir_val(entry_ptr))
};
paths.push(Path::new(cstr));
}
Expand Down
4 changes: 2 additions & 2 deletions src/libnative/io/file_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ pub fn mkdir(p: &CString, _mode: uint) -> IoResult<()> {

pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
fn prune(root: &CString, dirs: Vec<Path>) -> Vec<CString> {
let root = unsafe { CString::new(root.as_ptr(), false) };
let root = unsafe { CString::new(root.as_ptr()) };
let root = Path::new(root);

dirs.into_iter().filter(|path| {
Expand All @@ -352,7 +352,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
}

let star = Path::new(unsafe {
CString::new(p.as_ptr(), false)
CString::new(p.as_ptr())
}).join("*");
let path = try!(to_utf16(&star.to_c_str()));

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ pub enum OutputType {
pub fn llvm_err(handler: &diagnostic::Handler, msg: String) -> ! {
unsafe {
let cstr = llvm::LLVMRustGetLastError();
if cstr == ptr::null() {
if cstr as *const i8 == ptr::null() {
handler.fatal(msg.as_slice());
} else {
let err = CString::new(cstr, true);
let err = CString::new_owned(cstr);
let err = String::from_utf8_lossy(err.as_bytes());
handler.fatal(format!("{}: {}",
msg.as_slice(),
Expand Down Expand Up @@ -367,7 +367,7 @@ unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef, user: *mut c_vo

match llvm::diagnostic::Diagnostic::unpack(info) {
llvm::diagnostic::Optimization(opt) => {
let pass_name = CString::new(opt.pass_name, false);
let pass_name = CString::new(opt.pass_name);
let pass_name = pass_name.as_str().expect("got a non-UTF8 pass name from LLVM");
let enabled = match cgcx.remark {
AllPasses => true,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,7 @@ fn internalize_symbols(cx: &SharedCrateContext, reachable: &HashSet<String>) {
continue
}

let name = CString::new(llvm::LLVMGetValueName(val), false);
let name = CString::new(llvm::LLVMGetValueName(val));
declared.insert(name);
}
}
Expand All @@ -2981,7 +2981,7 @@ fn internalize_symbols(cx: &SharedCrateContext, reachable: &HashSet<String>) {
continue
}

let name = CString::new(llvm::LLVMGetValueName(val), false);
let name = CString::new(llvm::LLVMGetValueName(val));
if !declared.contains(&name) &&
!reachable.contains_equiv(&name.as_str().unwrap()) {
llvm::SetLinkage(val, llvm::InternalLinkage);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ extern {

/** Returns a string describing the last error caused by an LLVMRust*
call. */
pub fn LLVMRustGetLastError() -> *const c_char;
pub fn LLVMRustGetLastError() -> *mut c_char;

/// Print the pass timings since static dtors aren't picking them up.
pub fn LLVMRustPrintPassTimings();
Expand Down
Loading