Skip to content

Commit 142dab4

Browse files
committed
auto merge of #8960 : Kimundi/rust/master, r=alexcrichton
Changed ToStr impl for Ascii Added ToStr impl for char
2 parents b659fe3 + 8d610e1 commit 142dab4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/libstd/char.rs

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use option::{None, Option, Some};
1414
use int;
1515
use str::StrSlice;
1616
use unicode::{derived_property, general_category, decompose};
17+
use to_str::ToStr;
18+
use str;
1719

1820
#[cfg(test)] use str::OwnedStr;
1921

@@ -316,6 +318,13 @@ pub fn len_utf8_bytes(c: char) -> uint {
316318
)
317319
}
318320

321+
impl ToStr for char {
322+
#[inline]
323+
fn to_str(&self) -> ~str {
324+
str::from_char(*self)
325+
}
326+
}
327+
319328
#[allow(missing_doc)]
320329
pub trait Char {
321330
fn is_alphabetic(&self) -> bool;
@@ -502,3 +511,9 @@ fn test_escape_unicode() {
502511
assert_eq!(string('\u011b'), ~"\\u011b");
503512
assert_eq!(string('\U0001d4b6'), ~"\\U0001d4b6");
504513
}
514+
515+
#[test]
516+
fn test_to_str() {
517+
let s = 't'.to_str();
518+
assert_eq!(s, ~"t");
519+
}

src/libstd/str/ascii.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ impl Ascii {
6060

6161
impl ToStr for Ascii {
6262
#[inline]
63-
fn to_str(&self) -> ~str { str::from_bytes(['\'' as u8, self.chr, '\'' as u8]) }
63+
fn to_str(&self) -> ~str {
64+
// self.chr is allways a valid utf8 byte, no need for the check
65+
unsafe { str::raw::from_byte(self.chr) }
66+
}
6467
}
6568

6669
/// Trait for converting into an ascii type.
@@ -506,4 +509,12 @@ mod tests {
506509
i += 1;
507510
}
508511
}
512+
513+
#[test]
514+
fn test_to_str() {
515+
let s = Ascii{ chr: 't' as u8 }.to_str();
516+
assert_eq!(s, ~"t");
517+
}
518+
519+
509520
}

0 commit comments

Comments
 (0)