summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-09-04 01:35:58 -0700
committerbors <bors@rust-lang.org>2013-09-04 01:35:58 -0700
commit142dab4e4f9a7262ac9c38387a28e115a094e9e3 (patch)
tree304da39774b4ca40ee6114797cb4a32d2d2640c8 /src/libstd
parentb659fe3e5000c88d8d9be9af39355346f33acd58 (diff)
parent8d610e1d2d92238a17c6c7802e11f609f21815aa (diff)
downloadrust-142dab4e4f9a7262ac9c38387a28e115a094e9e3.tar.gz
rust-142dab4e4f9a7262ac9c38387a28e115a094e9e3.zip
auto merge of #8960 : Kimundi/rust/master, r=alexcrichton
Changed ToStr impl for Ascii
Added ToStr impl for char
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/char.rs15
-rw-r--r--src/libstd/str/ascii.rs13
2 files changed, 27 insertions, 1 deletions
diff --git a/src/libstd/char.rs b/src/libstd/char.rs
index fcfd54e814b..87314b98c51 100644
--- a/src/libstd/char.rs
+++ b/src/libstd/char.rs
@@ -14,6 +14,8 @@ use option::{None, Option, Some};
 use int;
 use str::StrSlice;
 use unicode::{derived_property, general_category, decompose};
+use to_str::ToStr;
+use str;
 
 #[cfg(test)] use str::OwnedStr;
 
@@ -316,6 +318,13 @@ pub fn len_utf8_bytes(c: char) -> uint {
     )
 }
 
+impl ToStr for char {
+    #[inline]
+    fn to_str(&self) -> ~str {
+        str::from_char(*self)
+    }
+}
+
 #[allow(missing_doc)]
 pub trait Char {
     fn is_alphabetic(&self) -> bool;
@@ -502,3 +511,9 @@ fn test_escape_unicode() {
     assert_eq!(string('\u011b'), ~"\\u011b");
     assert_eq!(string('\U0001d4b6'), ~"\\U0001d4b6");
 }
+
+#[test]
+fn test_to_str() {
+    let s = 't'.to_str();
+    assert_eq!(s, ~"t");
+}
diff --git a/src/libstd/str/ascii.rs b/src/libstd/str/ascii.rs
index 1cfbf841537..57730349e01 100644
--- a/src/libstd/str/ascii.rs
+++ b/src/libstd/str/ascii.rs
@@ -60,7 +60,10 @@ impl Ascii {
 
 impl ToStr for Ascii {
     #[inline]
-    fn to_str(&self) -> ~str { str::from_bytes(['\'' as u8, self.chr, '\'' as u8]) }
+    fn to_str(&self) -> ~str {
+        // self.chr is allways a valid utf8 byte, no need for the check
+        unsafe { str::raw::from_byte(self.chr) }
+    }
 }
 
 /// Trait for converting into an ascii type.
@@ -506,4 +509,12 @@ mod tests {
             i += 1;
         }
     }
+
+    #[test]
+    fn test_to_str() {
+        let s = Ascii{ chr: 't' as u8 }.to_str();
+        assert_eq!(s, ~"t");
+    }
+
+
 }