Skip to content

Commit 544f88d

Browse files
aochagaviaalexcrichton
authored andcommitted
---
yaml --- r: 153727 b: refs/heads/try2 c: 6e509d3 h: refs/heads/master i: 153725: b0632e9 153723: b9ea70a 153719: e407e30 153711: fb34c2e 153695: a62c744 153663: 2633d88 153599: 3e3c49e v: v3
1 parent c35004f commit 544f88d

File tree

7 files changed

+44
-35
lines changed

7 files changed

+44
-35
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: feeae27a56f034a0d041a4985fa731ee5b42d315
8+
refs/heads/try2: 6e509d3462d20b696a1b5d18f14884b4e391a6ba
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcollections/str.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,6 @@ 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

@@ -567,14 +566,10 @@ pub mod raw {
567566
pub use core::str::raw::{from_utf8, c_str_to_static_slice, slice_bytes};
568567
pub use core::str::raw::{slice_unchecked};
569568

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

580575
/// Deprecated. Use `CString::as_str().unwrap().to_string()`
@@ -598,22 +593,10 @@ pub mod raw {
598593
string::raw::from_utf8(v)
599594
}
600595

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

branches/try2/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");

branches/try2/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() {

branches/try2/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

branches/try2/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
});

branches/try2/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)