-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Implement std::convert traits for char #35755
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
#![stable(feature = "core_char", since = "1.2.0")] | ||
|
||
use char_private::is_printable; | ||
use convert::TryFrom; | ||
use fmt; | ||
use iter::FusedIterator; | ||
use mem::transmute; | ||
|
||
|
@@ -122,12 +124,7 @@ pub const MAX: char = '\u{10ffff}'; | |
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn from_u32(i: u32) -> Option<char> { | ||
// catch out-of-bounds and surrogates | ||
if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) { | ||
None | ||
} else { | ||
Some(unsafe { from_u32_unchecked(i) }) | ||
} | ||
char::try_from(i).ok() | ||
} | ||
|
||
/// Converts a `u32` to a `char`, ignoring validity. | ||
|
@@ -175,6 +172,66 @@ pub unsafe fn from_u32_unchecked(i: u32) -> char { | |
transmute(i) | ||
} | ||
|
||
#[stable(feature = "char_convert", since = "1.13.0")] | ||
impl From<char> for u32 { | ||
#[inline] | ||
fn from(c: char) -> Self { | ||
c as u32 | ||
} | ||
} | ||
|
||
/// Maps a byte in 0x00...0xFF to a `char` whose code point has the same value, in U+0000 to U+00FF. | ||
/// | ||
/// Unicode is designed such that this effectively decodes bytes | ||
/// with the character encoding that IANA calls ISO-8859-1. | ||
/// This encoding is compatible with ASCII. | ||
/// | ||
/// Note that this is different from ISO/IEC 8859-1 a.k.a. ISO 8859-1 (with one less hypen), | ||
/// which leaves some "blanks", byte values that are not assigned to any character. | ||
/// ISO-8859-1 (the IANA one) assigns them to the C0 and C1 control codes. | ||
/// | ||
/// Note that this is *also* different from Windows-1252 a.k.a. code page 1252, | ||
/// which is a superset ISO/IEC 8859-1 that assigns some (not all!) blanks | ||
/// to punctuation and various Latin characters. | ||
/// | ||
/// To confuse things further, [on the Web](https://encoding.spec.whatwg.org/) | ||
/// `ascii`, `iso-8859-1`, and `windows-1252` are all aliases | ||
/// for a superset of Windows-1252 that fills the remaining blanks with corresponding | ||
/// C0 and C1 control codes. | ||
#[stable(feature = "char_convert", since = "1.13.0")] | ||
impl From<u8> for char { | ||
#[inline] | ||
fn from(i: u8) -> Self { | ||
i as char | ||
} | ||
} | ||
|
||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl TryFrom<u32> for char { | ||
type Err = CharTryFromError; | ||
|
||
#[inline] | ||
fn try_from(i: u32) -> Result<Self, Self::Err> { | ||
if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) { | ||
Err(CharTryFromError(())) | ||
} else { | ||
Ok(unsafe { from_u32_unchecked(i) }) | ||
} | ||
} | ||
} | ||
|
||
/// The error type returned when a conversion from u32 to char fails. | ||
#[unstable(feature = "try_from", issue = "33417")] | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub struct CharTryFromError(()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
#[unstable(feature = "try_from", issue = "33417")] | ||
impl fmt::Display for CharTryFromError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
"converted integer out of range for `char`".fmt(f) | ||
} | ||
} | ||
|
||
/// Converts a digit in the given radix to a `char`. | ||
/// | ||
/// A 'radix' here is sometimes also called a 'base'. A radix of two | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a good idea to add documentation to this method as it's not completely obvious how it's implemented. Specifically it should mention the code page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with using the term "code page", but I’ve added a detailed doc-comment.