Skip to content

Commit

Permalink
std: change std::{from_bytes,from_bytes_with_null} to consume vec
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Jun 19, 2013
1 parent 73b7604 commit 486509b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 98 deletions.
8 changes: 4 additions & 4 deletions src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ pub fn run(lib_path: &str,
for input.iter().advance |input| {
proc.input().write_str(*input);
}
let output = proc.finish_with_output();
let run::ProcessOutput { status, output, error, _ } = proc.finish_with_output();

Result {
status: output.status,
out: str::from_utf8(output.output),
err: str::from_utf8(output.error)
status: status,
out: str::from_utf8(output),
err: str::from_utf8(error)
}
}
7 changes: 2 additions & 5 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,9 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
ty_err => ~"[type error]",
ty_param(param_ty {idx: id, def_id: did}) => {
if cx.sess.verbose() {
fmt!("'%s:%?",
str::from_utf8([('a' as u8) + (id as u8)]),
did)
fmt!("'%s:%?", str::from_byte(('a' as u8) + (id as u8)), did)
} else {
fmt!("'%s",
str::from_utf8([('a' as u8) + (id as u8)]))
fmt!("'%s", str::from_byte(('a' as u8) + (id as u8)))
}
}
ty_self(*) => ~"Self",
Expand Down
173 changes: 85 additions & 88 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,32 @@ Section: Creating a string
*
* Raises the `not_utf8` condition if invalid UTF-8
*/

pub fn from_utf8(vv: &[u8]) -> ~str {
pub fn from_utf8(v: ~[u8]) -> ~str {
use str::not_utf8::cond;

if !is_utf8(vv) {
let first_bad_byte = vec::find(vv, |b| !is_utf8([*b])).get();
if !is_utf8(v) {
let first_bad_byte = vec::find(v, |b| !is_utf8([*b])).get();
cond.raise(fmt!("from_utf8: input is not UTF-8; first bad byte is %u",
first_bad_byte as uint))
}
else {
return unsafe { raw::from_utf8(vv) }
return unsafe { raw::from_utf8(v) }
}
}

/**
* Convert a vector of bytes to a UTF-8 string.
* The vector needs to be one byte longer than the string, and end with a 0 byte.
*
* Compared to `from_utf8()`, this fn doesn't need to allocate a new owned str.
*
* # Failure
*
* Fails if invalid UTF-8
* Fails if not null terminated
*/
pub fn from_utf8_with_null<'a>(vv: &'a [u8]) -> &'a str {
assert_eq!(vv[vv.len() - 1], 0);
assert!(is_utf8(vv));
return unsafe { raw::from_utf8_with_null(vv) };
pub fn from_utf8_with_null(v: ~[u8]) -> ~str {
assert_eq!(v[v.len() - 1], 0);
assert!(is_utf8(v));
unsafe { raw::from_utf8_with_null(v) }
}

/**
Expand Down Expand Up @@ -783,16 +780,14 @@ pub mod raw {
}

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

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

Expand All @@ -811,7 +806,7 @@ pub mod raw {
}

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

/// Form a slice from a C string. Unsafe because the caller must ensure the
/// C string has the static lifetime, or else the return value may be
Expand Down Expand Up @@ -2247,17 +2242,19 @@ impl Zero for @str {

#[cfg(test)]
mod tests {
use iterator::IteratorUtil;
use super::*;
use char;
use cmp::{TotalOrd, Less, Equal, Greater};
use container::Container;
use option::Some;
use iterator::IteratorUtil;
use libc::c_char;
use libc;
use old_iter::BaseIter;
use option::Some;
use ptr;
use str::*;
use vec;
use uint;
use vec::{ImmutableVector, CopyableVector};
use cmp::{TotalOrd, Less, Equal, Greater};
use vec;

#[test]
fn test_eq() {
Expand Down Expand Up @@ -2777,15 +2774,15 @@ mod tests {
fn test_from_utf8() {
let ss = ~"ศไทย中华Việt Nam";
let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];

assert_eq!(ss, from_utf8(bb));
}
Expand All @@ -2795,48 +2792,48 @@ mod tests {
fn test_from_utf8_fail() {
use str::not_utf8::cond;

let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];

let mut error_happened = false;
let _x = do cond.trap(|err| {
assert_eq!(err, ~"from_utf8: input is not UTF-8; first bad byte is 255");
error_happened = true;
~""
}).in {
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8];

from_utf8(bb)
};
assert!(error_happened);
}

#[test]
fn test_unsafe_from_utf8_with_null() {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
let b = unsafe { raw::from_utf8_with_null(a) };
assert_eq!(b, "AAAAAAA");
assert_eq!(b, ~"AAAAAAA");
}

#[test]
fn test_from_utf8_with_null() {
let ss = "ศไทย中华Việt Nam";
let bb = [0xe0_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8, 0x0_u8];
let ss = ~"ศไทย中华Việt Nam";
let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8, 0x0_u8];

assert_eq!(ss, from_utf8_with_null(bb));
}
Expand All @@ -2845,16 +2842,16 @@ mod tests {
#[should_fail]
#[ignore(cfg(windows))]
fn test_from_utf8_with_null_fail() {
let bb = [0xff_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8, 0x0_u8];
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8, 0x0_u8];

let _x = from_utf8_with_null(bb);
}
Expand All @@ -2863,16 +2860,16 @@ mod tests {
#[should_fail]
#[ignore(cfg(windows))]
fn test_from_utf8_with_null_fail_2() {
let bb = [0xff_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8, 0x60_u8];
let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8,
0xe0_u8, 0xb9_u8, 0x84_u8,
0xe0_u8, 0xb8_u8, 0x97_u8,
0xe0_u8, 0xb8_u8, 0xa2_u8,
0xe4_u8, 0xb8_u8, 0xad_u8,
0xe5_u8, 0x8d_u8, 0x8e_u8,
0x56_u8, 0x69_u8, 0xe1_u8,
0xbb_u8, 0x87_u8, 0x74_u8,
0x20_u8, 0x4e_u8, 0x61_u8,
0x6d_u8, 0x60_u8];

let _x = from_utf8_with_null(bb);
}
Expand Down Expand Up @@ -3121,21 +3118,21 @@ mod tests {

#[test]
fn vec_str_conversions() {
let s1: ~str = ~"All mimsy were the borogoves";
let s1 = ~"All mimsy were the borogoves";
let n1 = s1.len();
let v = s1.as_bytes().to_owned();
let n2 = v.len();

let v: ~[u8] = s1.as_bytes().to_owned();
let s2: ~str = from_utf8(v);
let mut i: uint = 0u;
let n1: uint = s1.len();
let n2: uint = v.len();
assert_eq!(n1, n2);
while i < n1 {
let a: u8 = s1[i];
let b: u8 = s2[i];

let s2 = from_utf8(v);

for uint::range(0, n1) |i| {
let a = s1[i];
let b = s2[i];
debug!(a);
debug!(b);
assert_eq!(a, b);
i += 1u;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/libstd/str/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ impl Ascii {

impl ToStr for Ascii {
#[inline]
fn to_str(&self) -> ~str { str::from_utf8(['\'' as u8, self.chr, '\'' as u8]) }
fn to_str(&self) -> ~str {
str::from_utf8_with_null(~['\'' as u8, self.chr, '\'' as u8, 0])
}
}

/// Trait for converting into an ascii type.
Expand Down

13 comments on commit 486509b

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from erickt
at erickt@486509b

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/str-cleanup = 486509b into auto

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

erickt/rust/str-cleanup = 486509b merged ok, testing candidate = 545baaa2

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from erickt
at erickt@486509b

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/str-cleanup = 486509b into auto

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

erickt/rust/str-cleanup = 486509b merged ok, testing candidate = 8072331d

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from erickt
at erickt@486509b

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/str-cleanup = 486509b into auto

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 21, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/str-cleanup = 486509b into auto failed

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from erickt
at erickt@486509b

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/str-cleanup = 486509b into auto

@bors
Copy link
Contributor

@bors bors commented on 486509b Jun 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/str-cleanup = 486509b into auto failed

Please sign in to comment.