Skip to content

Fixed ToStr for char and Ascii #8960

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

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 15 additions & 0 deletions src/libstd/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
13 changes: 12 additions & 1 deletion src/libstd/str/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
}


}