Skip to content

Commit 486509b

Browse files
committed
std: change std::{from_bytes,from_bytes_with_null} to consume vec
1 parent 73b7604 commit 486509b

File tree

4 files changed

+94
-98
lines changed

4 files changed

+94
-98
lines changed

src/compiletest/procsrv.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ pub fn run(lib_path: &str,
6161
for input.iter().advance |input| {
6262
proc.input().write_str(*input);
6363
}
64-
let output = proc.finish_with_output();
64+
let run::ProcessOutput { status, output, error, _ } = proc.finish_with_output();
6565

6666
Result {
67-
status: output.status,
68-
out: str::from_utf8(output.output),
69-
err: str::from_utf8(output.error)
67+
status: status,
68+
out: str::from_utf8(output),
69+
err: str::from_utf8(error)
7070
}
7171
}

src/librustc/util/ppaux.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,9 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
432432
ty_err => ~"[type error]",
433433
ty_param(param_ty {idx: id, def_id: did}) => {
434434
if cx.sess.verbose() {
435-
fmt!("'%s:%?",
436-
str::from_utf8([('a' as u8) + (id as u8)]),
437-
did)
435+
fmt!("'%s:%?", str::from_byte(('a' as u8) + (id as u8)), did)
438436
} else {
439-
fmt!("'%s",
440-
str::from_utf8([('a' as u8) + (id as u8)]))
437+
fmt!("'%s", str::from_byte(('a' as u8) + (id as u8)))
441438
}
442439
}
443440
ty_self(*) => ~"Self",

src/libstd/str.rs

+85-88
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,32 @@ Section: Creating a string
5555
*
5656
* Raises the `not_utf8` condition if invalid UTF-8
5757
*/
58-
59-
pub fn from_utf8(vv: &[u8]) -> ~str {
58+
pub fn from_utf8(v: ~[u8]) -> ~str {
6059
use str::not_utf8::cond;
6160

62-
if !is_utf8(vv) {
63-
let first_bad_byte = vec::find(vv, |b| !is_utf8([*b])).get();
61+
if !is_utf8(v) {
62+
let first_bad_byte = vec::find(v, |b| !is_utf8([*b])).get();
6463
cond.raise(fmt!("from_utf8: input is not UTF-8; first bad byte is %u",
6564
first_bad_byte as uint))
6665
}
6766
else {
68-
return unsafe { raw::from_utf8(vv) }
67+
return unsafe { raw::from_utf8(v) }
6968
}
7069
}
7170

7271
/**
7372
* Convert a vector of bytes to a UTF-8 string.
7473
* The vector needs to be one byte longer than the string, and end with a 0 byte.
7574
*
76-
* Compared to `from_utf8()`, this fn doesn't need to allocate a new owned str.
77-
*
7875
* # Failure
7976
*
8077
* Fails if invalid UTF-8
8178
* Fails if not null terminated
8279
*/
83-
pub fn from_utf8_with_null<'a>(vv: &'a [u8]) -> &'a str {
84-
assert_eq!(vv[vv.len() - 1], 0);
85-
assert!(is_utf8(vv));
86-
return unsafe { raw::from_utf8_with_null(vv) };
80+
pub fn from_utf8_with_null(v: ~[u8]) -> ~str {
81+
assert_eq!(v[v.len() - 1], 0);
82+
assert!(is_utf8(v));
83+
unsafe { raw::from_utf8_with_null(v) }
8784
}
8885

8986
/**
@@ -783,16 +780,14 @@ pub mod raw {
783780
}
784781

785782
/// Converts a vector of bytes to a new owned string.
786-
pub unsafe fn from_utf8(v: &const [u8]) -> ~str {
787-
do vec::as_const_buf(v) |buf, len| {
788-
from_buf_len(buf, len)
789-
}
783+
pub unsafe fn from_utf8(mut v: ~[u8]) -> ~str {
784+
// Make sure the string is NULL terminated.
785+
v.push(0);
786+
from_utf8_with_null(v)
790787
}
791788

792-
/// Converts a vector of bytes to a string.
793-
/// The byte slice needs to contain valid utf8 and needs to be one byte longer than
794-
/// the string, if possible ending in a 0 byte.
795-
pub unsafe fn from_utf8_with_null<'a>(v: &'a [u8]) -> &'a str {
789+
/// Converts a vector of bytes with a trailing null to a new owned string.
790+
pub unsafe fn from_utf8_with_null(v: ~[u8]) -> ~str {
796791
cast::transmute(v)
797792
}
798793

@@ -811,7 +806,7 @@ pub mod raw {
811806
}
812807

813808
/// Converts a byte to a string.
814-
pub unsafe fn from_byte(u: u8) -> ~str { raw::from_utf8([u]) }
809+
pub unsafe fn from_byte(u: u8) -> ~str { raw::from_utf8_with_null(~[u, 0]) }
815810

816811
/// Form a slice from a C string. Unsafe because the caller must ensure the
817812
/// C string has the static lifetime, or else the return value may be
@@ -2247,17 +2242,19 @@ impl Zero for @str {
22472242
22482243
#[cfg(test)]
22492244
mod tests {
2250-
use iterator::IteratorUtil;
2245+
use super::*;
2246+
use char;
2247+
use cmp::{TotalOrd, Less, Equal, Greater};
22512248
use container::Container;
2252-
use option::Some;
2249+
use iterator::IteratorUtil;
22532250
use libc::c_char;
22542251
use libc;
22552252
use old_iter::BaseIter;
2253+
use option::Some;
22562254
use ptr;
2257-
use str::*;
2258-
use vec;
2255+
use uint;
22592256
use vec::{ImmutableVector, CopyableVector};
2260-
use cmp::{TotalOrd, Less, Equal, Greater};
2257+
use vec;
22612258
22622259
#[test]
22632260
fn test_eq() {
@@ -2777,15 +2774,15 @@ mod tests {
27772774
fn test_from_utf8() {
27782775
let ss = ~"ศไทย中华Việt Nam";
27792776
let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8,
2780-
0xe0_u8, 0xb9_u8, 0x84_u8,
2781-
0xe0_u8, 0xb8_u8, 0x97_u8,
2782-
0xe0_u8, 0xb8_u8, 0xa2_u8,
2783-
0xe4_u8, 0xb8_u8, 0xad_u8,
2784-
0xe5_u8, 0x8d_u8, 0x8e_u8,
2785-
0x56_u8, 0x69_u8, 0xe1_u8,
2786-
0xbb_u8, 0x87_u8, 0x74_u8,
2787-
0x20_u8, 0x4e_u8, 0x61_u8,
2788-
0x6d_u8];
2777+
0xe0_u8, 0xb9_u8, 0x84_u8,
2778+
0xe0_u8, 0xb8_u8, 0x97_u8,
2779+
0xe0_u8, 0xb8_u8, 0xa2_u8,
2780+
0xe4_u8, 0xb8_u8, 0xad_u8,
2781+
0xe5_u8, 0x8d_u8, 0x8e_u8,
2782+
0x56_u8, 0x69_u8, 0xe1_u8,
2783+
0xbb_u8, 0x87_u8, 0x74_u8,
2784+
0x20_u8, 0x4e_u8, 0x61_u8,
2785+
0x6d_u8];
27892786
27902787
assert_eq!(ss, from_utf8(bb));
27912788
}
@@ -2795,48 +2792,48 @@ mod tests {
27952792
fn test_from_utf8_fail() {
27962793
use str::not_utf8::cond;
27972794
2798-
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
2799-
0xe0_u8, 0xb9_u8, 0x84_u8,
2800-
0xe0_u8, 0xb8_u8, 0x97_u8,
2801-
0xe0_u8, 0xb8_u8, 0xa2_u8,
2802-
0xe4_u8, 0xb8_u8, 0xad_u8,
2803-
0xe5_u8, 0x8d_u8, 0x8e_u8,
2804-
0x56_u8, 0x69_u8, 0xe1_u8,
2805-
0xbb_u8, 0x87_u8, 0x74_u8,
2806-
0x20_u8, 0x4e_u8, 0x61_u8,
2807-
0x6d_u8];
2808-
28092795
let mut error_happened = false;
28102796
let _x = do cond.trap(|err| {
28112797
assert_eq!(err, ~"from_utf8: input is not UTF-8; first bad byte is 255");
28122798
error_happened = true;
28132799
~""
28142800
}).in {
2801+
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
2802+
0xe0_u8, 0xb9_u8, 0x84_u8,
2803+
0xe0_u8, 0xb8_u8, 0x97_u8,
2804+
0xe0_u8, 0xb8_u8, 0xa2_u8,
2805+
0xe4_u8, 0xb8_u8, 0xad_u8,
2806+
0xe5_u8, 0x8d_u8, 0x8e_u8,
2807+
0x56_u8, 0x69_u8, 0xe1_u8,
2808+
0xbb_u8, 0x87_u8, 0x74_u8,
2809+
0x20_u8, 0x4e_u8, 0x61_u8,
2810+
0x6d_u8];
2811+
28152812
from_utf8(bb)
28162813
};
28172814
assert!(error_happened);
28182815
}
28192816
28202817
#[test]
28212818
fn test_unsafe_from_utf8_with_null() {
2822-
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
2819+
let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
28232820
let b = unsafe { raw::from_utf8_with_null(a) };
2824-
assert_eq!(b, "AAAAAAA");
2821+
assert_eq!(b, ~"AAAAAAA");
28252822
}
28262823
28272824
#[test]
28282825
fn test_from_utf8_with_null() {
2829-
let ss = "ศไทย中华Việt Nam";
2830-
let bb = [0xe0_u8, 0xb8_u8, 0xa8_u8,
2831-
0xe0_u8, 0xb9_u8, 0x84_u8,
2832-
0xe0_u8, 0xb8_u8, 0x97_u8,
2833-
0xe0_u8, 0xb8_u8, 0xa2_u8,
2834-
0xe4_u8, 0xb8_u8, 0xad_u8,
2835-
0xe5_u8, 0x8d_u8, 0x8e_u8,
2836-
0x56_u8, 0x69_u8, 0xe1_u8,
2837-
0xbb_u8, 0x87_u8, 0x74_u8,
2838-
0x20_u8, 0x4e_u8, 0x61_u8,
2839-
0x6d_u8, 0x0_u8];
2826+
let ss = ~"ศไทย中华Việt Nam";
2827+
let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8,
2828+
0xe0_u8, 0xb9_u8, 0x84_u8,
2829+
0xe0_u8, 0xb8_u8, 0x97_u8,
2830+
0xe0_u8, 0xb8_u8, 0xa2_u8,
2831+
0xe4_u8, 0xb8_u8, 0xad_u8,
2832+
0xe5_u8, 0x8d_u8, 0x8e_u8,
2833+
0x56_u8, 0x69_u8, 0xe1_u8,
2834+
0xbb_u8, 0x87_u8, 0x74_u8,
2835+
0x20_u8, 0x4e_u8, 0x61_u8,
2836+
0x6d_u8, 0x0_u8];
28402837
28412838
assert_eq!(ss, from_utf8_with_null(bb));
28422839
}
@@ -2845,16 +2842,16 @@ mod tests {
28452842
#[should_fail]
28462843
#[ignore(cfg(windows))]
28472844
fn test_from_utf8_with_null_fail() {
2848-
let bb = [0xff_u8, 0xb8_u8, 0xa8_u8,
2849-
0xe0_u8, 0xb9_u8, 0x84_u8,
2850-
0xe0_u8, 0xb8_u8, 0x97_u8,
2851-
0xe0_u8, 0xb8_u8, 0xa2_u8,
2852-
0xe4_u8, 0xb8_u8, 0xad_u8,
2853-
0xe5_u8, 0x8d_u8, 0x8e_u8,
2854-
0x56_u8, 0x69_u8, 0xe1_u8,
2855-
0xbb_u8, 0x87_u8, 0x74_u8,
2856-
0x20_u8, 0x4e_u8, 0x61_u8,
2857-
0x6d_u8, 0x0_u8];
2845+
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
2846+
0xe0_u8, 0xb9_u8, 0x84_u8,
2847+
0xe0_u8, 0xb8_u8, 0x97_u8,
2848+
0xe0_u8, 0xb8_u8, 0xa2_u8,
2849+
0xe4_u8, 0xb8_u8, 0xad_u8,
2850+
0xe5_u8, 0x8d_u8, 0x8e_u8,
2851+
0x56_u8, 0x69_u8, 0xe1_u8,
2852+
0xbb_u8, 0x87_u8, 0x74_u8,
2853+
0x20_u8, 0x4e_u8, 0x61_u8,
2854+
0x6d_u8, 0x0_u8];
28582855
28592856
let _x = from_utf8_with_null(bb);
28602857
}
@@ -2863,16 +2860,16 @@ mod tests {
28632860
#[should_fail]
28642861
#[ignore(cfg(windows))]
28652862
fn test_from_utf8_with_null_fail_2() {
2866-
let bb = [0xff_u8, 0xb8_u8, 0xa8_u8,
2867-
0xe0_u8, 0xb9_u8, 0x84_u8,
2868-
0xe0_u8, 0xb8_u8, 0x97_u8,
2869-
0xe0_u8, 0xb8_u8, 0xa2_u8,
2870-
0xe4_u8, 0xb8_u8, 0xad_u8,
2871-
0xe5_u8, 0x8d_u8, 0x8e_u8,
2872-
0x56_u8, 0x69_u8, 0xe1_u8,
2873-
0xbb_u8, 0x87_u8, 0x74_u8,
2874-
0x20_u8, 0x4e_u8, 0x61_u8,
2875-
0x6d_u8, 0x60_u8];
2863+
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
2864+
0xe0_u8, 0xb9_u8, 0x84_u8,
2865+
0xe0_u8, 0xb8_u8, 0x97_u8,
2866+
0xe0_u8, 0xb8_u8, 0xa2_u8,
2867+
0xe4_u8, 0xb8_u8, 0xad_u8,
2868+
0xe5_u8, 0x8d_u8, 0x8e_u8,
2869+
0x56_u8, 0x69_u8, 0xe1_u8,
2870+
0xbb_u8, 0x87_u8, 0x74_u8,
2871+
0x20_u8, 0x4e_u8, 0x61_u8,
2872+
0x6d_u8, 0x60_u8];
28762873
28772874
let _x = from_utf8_with_null(bb);
28782875
}
@@ -3121,21 +3118,21 @@ mod tests {
31213118
31223119
#[test]
31233120
fn vec_str_conversions() {
3124-
let s1: ~str = ~"All mimsy were the borogoves";
3121+
let s1 = ~"All mimsy were the borogoves";
3122+
let n1 = s1.len();
3123+
let v = s1.as_bytes().to_owned();
3124+
let n2 = v.len();
31253125
3126-
let v: ~[u8] = s1.as_bytes().to_owned();
3127-
let s2: ~str = from_utf8(v);
3128-
let mut i: uint = 0u;
3129-
let n1: uint = s1.len();
3130-
let n2: uint = v.len();
31313126
assert_eq!(n1, n2);
3132-
while i < n1 {
3133-
let a: u8 = s1[i];
3134-
let b: u8 = s2[i];
3127+
3128+
let s2 = from_utf8(v);
3129+
3130+
for uint::range(0, n1) |i| {
3131+
let a = s1[i];
3132+
let b = s2[i];
31353133
debug!(a);
31363134
debug!(b);
31373135
assert_eq!(a, b);
3138-
i += 1u;
31393136
}
31403137
}
31413138

src/libstd/str/ascii.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ impl Ascii {
6565

6666
impl ToStr for Ascii {
6767
#[inline]
68-
fn to_str(&self) -> ~str { str::from_utf8(['\'' as u8, self.chr, '\'' as u8]) }
68+
fn to_str(&self) -> ~str {
69+
str::from_utf8_with_null(~['\'' as u8, self.chr, '\'' as u8, 0])
70+
}
6971
}
7072

7173
/// Trait for converting into an ascii type.

0 commit comments

Comments
 (0)