Skip to content

Commit 3629f70

Browse files
committed
std: merge str::raw::from_buf and str::raw::from_c_str
1 parent bd908d4 commit 3629f70

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

src/libstd/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool) -> Option<~str> {
9393
do buf.as_mut_buf |b, sz| {
9494
if f(b, sz as size_t) {
9595
unsafe {
96-
Some(str::raw::from_buf(b as *u8))
96+
Some(str::raw::from_c_str(b as *c_char))
9797
}
9898
} else {
9999
None

src/libstd/str.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -763,17 +763,6 @@ pub mod raw {
763763
use vec::MutableVector;
764764
use unstable::raw::{Slice, String};
765765

766-
/// Create a Rust string from a null-terminated *u8 buffer
767-
pub unsafe fn from_buf(buf: *u8) -> ~str {
768-
let mut curr = buf;
769-
let mut i = 0u;
770-
while *curr != 0u8 {
771-
i += 1u;
772-
curr = ptr::offset(buf, i as int);
773-
}
774-
return from_buf_len(buf, i);
775-
}
776-
777766
/// Create a Rust string from a *u8 buffer of the given length
778767
pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
779768
let mut v: ~[u8] = vec::with_capacity(len + 1);
@@ -784,17 +773,18 @@ pub mod raw {
784773
v.push(0u8);
785774

786775
assert!(is_utf8(v));
787-
return cast::transmute(v);
776+
cast::transmute(v)
788777
}
789778

790779
/// Create a Rust string from a null-terminated C string
791-
pub unsafe fn from_c_str(c_str: *libc::c_char) -> ~str {
792-
from_buf(c_str as *u8)
793-
}
794-
795-
/// Create a Rust string from a `*c_char` buffer of the given length
796-
pub unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> ~str {
797-
from_buf_len(c_str as *u8, len)
780+
pub unsafe fn from_c_str(buf: *libc::c_char) -> ~str {
781+
let mut curr = buf;
782+
let mut i = 0;
783+
while *curr != 0 {
784+
i += 1;
785+
curr = ptr::offset(buf, i);
786+
}
787+
from_buf_len(buf as *u8, i as uint)
798788
}
799789

800790
/// Converts a vector of bytes to a new owned string.
@@ -2805,11 +2795,11 @@ mod tests {
28052795
}
28062796
28072797
#[test]
2808-
fn test_from_buf() {
2798+
fn test_raw_from_c_str() {
28092799
unsafe {
2810-
let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
2800+
let a = ~[65, 65, 65, 65, 65, 65, 65, 0];
28112801
let b = vec::raw::to_ptr(a);
2812-
let c = raw::from_buf(b);
2802+
let c = raw::from_c_str(b);
28132803
assert_eq!(c, ~"AAAAAAA");
28142804
}
28152805
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010

1111
use std::str;
1212

13-
static a: [u8, ..3] = ['h' as u8, 'i' as u8, 0 as u8];
14-
static c: &'static [u8, ..3] = &a;
15-
static b: *u8 = c as *u8;
13+
static A: [u8, ..2] = ['h' as u8, 'i' as u8];
14+
static B: &'static [u8, ..2] = &A;
15+
static C: *u8 = B as *u8;
1616

1717
pub fn main() {
18-
let foo = &a as *u8;
19-
assert_eq!(unsafe { str::raw::from_bytes(a) }, ~"hi\x00");
20-
assert_eq!(unsafe { str::raw::from_buf(foo) }, ~"hi");
21-
assert_eq!(unsafe { str::raw::from_buf(b) }, ~"hi");
22-
assert!(unsafe { *b == a[0] });
23-
assert!(unsafe { *(&c[0] as *u8) == a[0] });
18+
unsafe {
19+
let foo = &A as *u8;
20+
assert_eq!(str::raw::from_bytes(A), ~"hi");
21+
assert_eq!(str::raw::from_buf_len(foo, A.len()), ~"hi");
22+
assert_eq!(str::raw::from_buf_len(C, B.len()), ~"hi");
23+
assert!(*C == A[0]);
24+
assert!(*(&B[0] as *u8) == A[0]);
25+
26+
let bar = str::raw::from_bytes(A).to_c_str();
27+
assert_eq!(bar.with_ref(|buf| str::raw::from_c_str(buf)), ~"hi");
28+
}
2429
}

0 commit comments

Comments
 (0)