Skip to content

Commit f8cd834

Browse files
committed
Deprecated str::raw::from_buf_len
Replaced by `string::raw::from_buf_len` [breaking-change]
1 parent 091ce19 commit f8cd834

File tree

6 files changed

+43
-34
lines changed

6 files changed

+43
-34
lines changed

src/libcollections/str.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -558,21 +558,16 @@ pub mod raw {
558558
use core::mem;
559559
use core::raw::Slice;
560560
use core::ptr::RawPtr;
561-
562561
use string::{mod, String};
563562
use vec::Vec;
564563

565564
pub use core::str::raw::{from_utf8, c_str_to_static_slice, slice_bytes};
566565
pub use core::str::raw::{slice_unchecked};
567566

568-
/// Create a Rust string from a *u8 buffer of the given length
567+
/// Deprecated. Replaced by `string::raw::from_buf_len`
568+
#[deprecated = "Use string::raw::from_buf_len"]
569569
pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> String {
570-
let mut result = String::new();
571-
result.push_bytes(mem::transmute(Slice {
572-
data: buf,
573-
len: len,
574-
}));
575-
result
570+
string::raw::from_buf_len(buf, len)
576571
}
577572

578573
/// Deprecated. Use `CString::as_str().unwrap().to_string()`
@@ -596,22 +591,10 @@ pub mod raw {
596591
string::raw::from_utf8(v)
597592
}
598593

599-
/// Deprecated. Use `String::from_bytes`
600-
#[deprecated = "Use String::from_bytes"]
594+
/// Deprecated. Use `string::raw::from_utf8`
595+
#[deprecated = "Use string::raw::from_utf8"]
601596
pub unsafe fn from_byte(u: u8) -> String {
602-
String::from_bytes(vec![u])
603-
}
604-
605-
#[test]
606-
fn test_from_buf_len() {
607-
use slice::ImmutableVector;
608-
609-
unsafe {
610-
let a = vec![65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
611-
let b = a.as_ptr();
612-
let c = from_buf_len(b, 3u);
613-
assert_eq!(c, String::from_str("AAA"));
614-
}
597+
string::raw::from_utf8(vec![u])
615598
}
616599
}
617600

src/libcollections/string.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,9 @@ impl<S: Str> Add<S, String> for String {
571571
}
572572

573573
pub mod raw {
574+
use core::mem;
575+
use core::raw::Slice;
576+
574577
use super::String;
575578
use vec::Vec;
576579

@@ -581,6 +584,20 @@ pub mod raw {
581584
pub unsafe fn from_utf8(bytes: Vec<u8>) -> String {
582585
String { vec: bytes }
583586
}
587+
588+
/// Create a Rust string from a *u8 buffer of the given length
589+
///
590+
/// This function is unsafe because of two reasons:
591+
/// * A raw pointer is dereferenced and transmuted to `&[u8]`
592+
/// * The slice is not checked to see whether it contains valid UTF-8
593+
pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> String {
594+
use slice::CloneableVector;
595+
let slice: &[u8] = mem::transmute(Slice {
596+
data: buf,
597+
len: len,
598+
});
599+
self::from_utf8(slice.to_vec())
600+
}
584601
}
585602

586603
#[cfg(test)]
@@ -740,6 +757,14 @@ mod tests {
740757
String::from_str("\uFFFD𐒋\uFFFD"));
741758
}
742759

760+
#[test]
761+
fn test_from_buf_len() {
762+
unsafe {
763+
let a = vec![65u8, 65, 65, 65, 65, 65, 65, 0];
764+
assert_eq!(super::raw::from_buf_len(a.as_ptr(), 3), String::from_str("AAA"));
765+
}
766+
}
767+
743768
#[test]
744769
fn test_push_bytes() {
745770
let mut s = String::from_str("ABC");

src/librustc/metadata/loader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ use std::io;
233233
use std::mem;
234234
use std::ptr;
235235
use std::slice;
236-
use std::str;
236+
use std::string;
237237

238238
use std::collections::{HashMap, HashSet};
239239
use flate;
@@ -772,7 +772,7 @@ fn get_metadata_section_imp(os: abi::Os, filename: &Path) -> Result<MetadataBlob
772772
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
773773
let mut name_buf = ptr::null();
774774
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
775-
let name = str::raw::from_buf_len(name_buf as *const u8,
775+
let name = string::raw::from_buf_len(name_buf as *const u8,
776776
name_len as uint);
777777
debug!("get_metadata_section: name {}", name);
778778
if read_meta_section_name(os).as_slice() == name.as_slice() {

src/librustdoc/html/markdown.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use std::cell::{RefCell, Cell};
3232
use std::fmt;
3333
use std::slice;
3434
use std::str;
35+
use std::string;
3536
use std::collections::HashMap;
3637

3738
use html::toc::TocBuilder;
@@ -222,7 +223,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
222223
"".to_string()
223224
} else {
224225
unsafe {
225-
str::raw::from_buf_len((*text).data, (*text).size as uint)
226+
string::raw::from_buf_len((*text).data, (*text).size as uint)
226227
}
227228
};
228229

src/libstd/os.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ use ptr;
4747
use result::{Err, Ok, Result};
4848
use slice::{Vector, ImmutableVector, MutableVector, ImmutableEqVector};
4949
use str::{Str, StrSlice, StrAllocating};
50-
use str;
5150
use string::String;
5251
use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
52+
use to_str::ToString;
5353
use vec::Vec;
5454

5555
#[cfg(unix)]
@@ -135,7 +135,7 @@ pub fn getcwd() -> Path {
135135
fail!();
136136
}
137137
}
138-
Path::new(String::from_utf16(str::truncate_utf16_at_nul(buf))
138+
Path::new(String::from_utf16(::str::truncate_utf16_at_nul(buf))
139139
.expect("GetCurrentDirectoryW returned invalid UTF-16"))
140140
}
141141

@@ -413,7 +413,7 @@ pub fn setenv<T: BytesContainer>(n: &str, v: T) {
413413
fn _setenv(n: &str, v: &[u8]) {
414414
let n: Vec<u16> = n.utf16_units().collect();
415415
let n = n.append_one(0);
416-
let v: Vec<u16> = str::from_utf8(v).unwrap().utf16_units().collect();
416+
let v: Vec<u16> = ::str::from_utf8(v).unwrap().utf16_units().collect();
417417
let v = v.append_one(0);
418418

419419
unsafe {
@@ -1045,7 +1045,7 @@ pub fn error_string(errnum: uint) -> String {
10451045
return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
10461046
}
10471047

1048-
let msg = String::from_utf16(str::truncate_utf16_at_nul(buf));
1048+
let msg = String::from_utf16(::str::truncate_utf16_at_nul(buf));
10491049
match msg {
10501050
Some(msg) => format!("OS Error {}: {}", errnum, msg),
10511051
None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum),
@@ -1202,7 +1202,7 @@ fn real_args() -> Vec<String> {
12021202

12031203
// Push it onto the list.
12041204
let opt_s = slice::raw::buf_as_slice(ptr as *const _, len, |buf| {
1205-
String::from_utf16(str::truncate_utf16_at_nul(buf))
1205+
String::from_utf16(::str::truncate_utf16_at_nul(buf))
12061206
});
12071207
opt_s.expect("CommandLineToArgvW returned invalid UTF-16")
12081208
});

src/test/run-pass/const-str-ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::str;
11+
use std::{str, string};
1212

1313
static A: [u8, ..2] = ['h' as u8, 'i' as u8];
1414
static B: &'static [u8, ..2] = &A;
@@ -18,8 +18,8 @@ pub fn main() {
1818
unsafe {
1919
let foo = &A as *const u8;
2020
assert_eq!(str::raw::from_utf8(A), "hi");
21-
assert_eq!(str::raw::from_buf_len(foo, A.len()), "hi".to_string());
22-
assert_eq!(str::raw::from_buf_len(C, B.len()), "hi".to_string());
21+
assert_eq!(string::raw::from_buf_len(foo, A.len()), "hi".to_string());
22+
assert_eq!(string::raw::from_buf_len(C, B.len()), "hi".to_string());
2323
assert!(*C == A[0]);
2424
assert!(*(&B[0] as *const u8) == A[0]);
2525

0 commit comments

Comments
 (0)