Skip to content

std::ascii: Provide a copyless [Ascii] -> str method. #10333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/compiletest/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
let start_kind = idx;
while idx < len && line[idx] != (' ' as u8) { idx += 1u; }

// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
let kind = line.slice(start_kind, idx);
let kind = kind.to_ascii().to_lower().to_str_ascii();
let kind = kind.to_ascii().to_lower().into_str();

// Extract msg:
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
Expand Down
8 changes: 2 additions & 6 deletions src/libextra/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,12 +887,8 @@ mod tests {
// tjc: funny that we have to use parens
fn ile(x: &(&'static str), y: &(&'static str)) -> bool
{
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_move and to_str_move to not do a unnecessary clone.
// (Actually, could just remove the to_str_* call, but needs an deriving(Ord) on
// Ascii)
let x = x.to_ascii().to_lower().to_str_ascii();
let y = y.to_ascii().to_lower().to_str_ascii();
let x = x.to_ascii().to_lower().into_str();
let y = y.to_ascii().to_lower().into_str();
x <= y
}

Expand Down
4 changes: 1 addition & 3 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,8 @@ pub fn build_session_options(binary: @str,
for level in lint_levels.iter() {
let level_name = lint::level_to_str(*level);

// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_move and to_str_move to not do a unnecessary copy.
let level_short = level_name.slice_chars(0, 1);
let level_short = level_short.to_ascii().to_upper().to_str_ascii();
let level_short = level_short.to_ascii().to_upper().into_str();
let flags = vec::append(matches.opt_strs(level_short),
matches.opt_strs(level_name));
for lint_name in flags.iter() {
Expand Down
33 changes: 18 additions & 15 deletions src/libstd/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use str::OwnedStr;
use container::Container;
use cast;
use iter::Iterator;
use vec::{CopyableVector, ImmutableVector, MutableVector};
use vec::{ImmutableVector, MutableVector};
use to_bytes::IterBytes;
use option::{Some, None};

Expand Down Expand Up @@ -154,10 +154,10 @@ impl AsciiCast<Ascii> for char {

/// Trait for copyless casting to an ascii vector.
pub trait OwnedAsciiCast {
/// Take ownership and cast to an ascii vector without trailing zero element.
/// Take ownership and cast to an ascii vector.
fn into_ascii(self) -> ~[Ascii];

/// Take ownership and cast to an ascii vector without trailing zero element.
/// Take ownership and cast to an ascii vector.
/// Does not perform validation checks.
unsafe fn into_ascii_nocheck(self) -> ~[Ascii];
}
Expand Down Expand Up @@ -188,26 +188,26 @@ impl OwnedAsciiCast for ~str {
}
}

/// Trait for converting an ascii type to a string. Needed to convert `&[Ascii]` to `~str`
/// Trait for converting an ascii type to a string. Needed to convert
/// `&[Ascii]` to `&str`.
pub trait AsciiStr {
/// Convert to a string.
fn to_str_ascii(&self) -> ~str;
fn as_str_ascii<'a>(&'a self) -> &'a str;

/// Convert to vector representing a lower cased ascii string.
fn to_lower(&self) -> ~[Ascii];

/// Convert to vector representing a upper cased ascii string.
fn to_upper(&self) -> ~[Ascii];

/// Compares two Ascii strings ignoring case
/// Compares two Ascii strings ignoring case.
fn eq_ignore_case(self, other: &[Ascii]) -> bool;
}

impl<'self> AsciiStr for &'self [Ascii] {
#[inline]
fn to_str_ascii(&self) -> ~str {
let cpy = self.to_owned();
unsafe { cast::transmute(cpy) }
fn as_str_ascii<'a>(&'a self) -> &'a str {
unsafe { cast::transmute(*self) }
}

#[inline]
Expand Down Expand Up @@ -443,12 +443,12 @@ mod tests {
let v = ~[40u8, 32u8, 59u8]; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59]));
let v = ~"( ;"; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59]));

assert_eq!("abCDef&?#".to_ascii().to_lower().to_str_ascii(), ~"abcdef&?#");
assert_eq!("abCDef&?#".to_ascii().to_upper().to_str_ascii(), ~"ABCDEF&?#");
assert_eq!("abCDef&?#".to_ascii().to_lower().into_str(), ~"abcdef&?#");
assert_eq!("abCDef&?#".to_ascii().to_upper().into_str(), ~"ABCDEF&?#");

assert_eq!("".to_ascii().to_lower().to_str_ascii(), ~"");
assert_eq!("YMCA".to_ascii().to_lower().to_str_ascii(), ~"ymca");
assert_eq!("abcDEFxyz:.;".to_ascii().to_upper().to_str_ascii(), ~"ABCDEFXYZ:.;");
assert_eq!("".to_ascii().to_lower().into_str(), ~"");
assert_eq!("YMCA".to_ascii().to_lower().into_str(), ~"ymca");
assert_eq!("abcDEFxyz:.;".to_ascii().to_upper().into_str(), ~"ABCDEFXYZ:.;");

assert!("aBcDeF&?#".to_ascii().eq_ignore_case("AbCdEf&?#".to_ascii()));

Expand All @@ -465,7 +465,10 @@ mod tests {
}

#[test]
fn test_ascii_to_str() { assert_eq!(v2ascii!([40, 32, 59]).to_str_ascii(), ~"( ;"); }
fn test_ascii_as_str() {
let v = v2ascii!([40, 32, 59]);
assert_eq!(v.as_str_ascii(), "( ;");
}

#[test]
fn test_ascii_into_str() {
Expand Down
8 changes: 2 additions & 6 deletions src/test/bench/shootout-k-nucleotide-pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
let (k,v) = (*kv).clone();
unsafe {
let b = str::raw::from_utf8(k);
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_move and to_str_move to not do a unnecessary copy.
buffer.push_str(format!("{} {:0.3f}\n",
b.to_ascii().to_upper().to_str_ascii(), v));
b.into_ascii().to_upper().into_str(), v));
}
}

Expand All @@ -84,9 +82,7 @@ fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {

// given a map, search for the frequency of a pattern
fn find(mm: &HashMap<~[u8], uint>, key: ~str) -> uint {
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
// to_ascii_move and to_str_move to not do a unnecessary copy.
let key = key.to_ascii().to_lower().to_str_ascii();
let key = key.into_ascii().to_lower().into_str();
match mm.find_equiv(&key.as_bytes()) {
option::None => { return 0u; }
option::Some(&num) => { return num; }
Expand Down