Skip to content

Commit fd9d7aa

Browse files
committedJun 20, 2017
added FromStr Impl for char
1 parent 5579677 commit fd9d7aa

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `char_error_internals`
2+
3+
This feature is internal to the Rust compiler and is not intended for general use.
4+
5+
------------------------

‎src/libcore/char.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use char_private::is_printable;
1919
use convert::TryFrom;
2020
use fmt::{self, Write};
2121
use slice;
22-
use str::from_utf8_unchecked_mut;
22+
use str::{from_utf8_unchecked_mut, FromStr};
2323
use iter::FusedIterator;
2424
use mem::transmute;
2525

@@ -208,6 +208,63 @@ impl From<u8> for char {
208208
}
209209
}
210210

211+
212+
/// An error which can be returned when parsing a char.
213+
#[stable(feature = "char_from_str", since = "1.19.0")]
214+
#[derive(Clone, Debug)]
215+
pub struct ParseCharError {
216+
kind: CharErrorKind,
217+
}
218+
219+
impl ParseCharError {
220+
#[unstable(feature = "char_error_internals",
221+
reason = "this method should not be available publicly",
222+
issue = "0")]
223+
#[doc(hidden)]
224+
pub fn __description(&self) -> &str {
225+
match self.kind {
226+
CharErrorKind::EmptyString => {
227+
"cannot parse char from empty string"
228+
},
229+
CharErrorKind::TooManyChars => "too many characters in string"
230+
}
231+
}
232+
}
233+
234+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
235+
enum CharErrorKind {
236+
EmptyString,
237+
TooManyChars,
238+
}
239+
240+
#[stable(feature = "char_from_str", since = "1.19.0")]
241+
impl fmt::Display for ParseCharError {
242+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
243+
self.__description().fmt(f)
244+
}
245+
}
246+
247+
248+
#[stable(feature = "char_from_str", since = "1.19.0")]
249+
impl FromStr for char {
250+
type Err = ParseCharError;
251+
252+
#[inline]
253+
fn from_str(s: &str) -> Result<Self, Self::Err> {
254+
let mut chars = s.chars();
255+
match (chars.next(), chars.next()) {
256+
(None, _) => {
257+
Err(ParseCharError { kind: CharErrorKind::EmptyString })
258+
},
259+
(Some(c), None) => Ok(c),
260+
_ => {
261+
Err(ParseCharError { kind: CharErrorKind::TooManyChars })
262+
}
263+
}
264+
}
265+
}
266+
267+
211268
#[unstable(feature = "try_from", issue = "33417")]
212269
impl TryFrom<u32> for char {
213270
type Error = CharTryFromError;

‎src/libcore/tests/char.rs

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use std::{char,str};
1212
use std::convert::TryFrom;
13+
use std::str::FromStr;
1314

1415
#[test]
1516
fn test_convert() {
@@ -28,6 +29,16 @@ fn test_convert() {
2829
assert!(char::try_from(0xFFFF_FFFF_u32).is_err());
2930
}
3031

32+
#[test]
33+
fn test_from_str() {
34+
assert_eq!(char::from_str("a").unwrap(), 'a');
35+
assert_eq!(char::try_from("a").unwrap(), 'a');
36+
assert_eq!(char::from_str("\0").unwrap(), '\0');
37+
assert_eq!(char::from_str("\u{D7FF}").unwrap(), '\u{d7FF}');
38+
assert!(char::from_str("").is_err());
39+
assert!(char::from_str("abc").is_err());
40+
}
41+
3142
#[test]
3243
fn test_is_lowercase() {
3344
assert!('a'.is_lowercase());

‎src/libstd/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ impl Error for char::CharTryFromError {
321321
}
322322
}
323323

324+
#[stable(feature = "char_from_str", since = "1.19.0")]
325+
impl Error for char::ParseCharError {
326+
fn description(&self) -> &str {
327+
self.__description()
328+
}
329+
}
330+
331+
324332
// copied from any.rs
325333
impl Error + 'static {
326334
/// Returns true if the boxed type is the same as `T`

‎src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
#![feature(cfg_target_thread_local)]
254254
#![feature(cfg_target_vendor)]
255255
#![feature(char_escape_debug)]
256+
#![feature(char_error_internals)]
256257
#![feature(char_internals)]
257258
#![feature(collections)]
258259
#![feature(collections_range)]

‎src/libstd_unicode/char.rs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use tables::{conversions, derived_property, general_category, property};
3838
pub use core::char::{MAX, from_digit, from_u32, from_u32_unchecked};
3939
#[stable(feature = "rust1", since = "1.0.0")]
4040
pub use core::char::{EscapeDebug, EscapeDefault, EscapeUnicode};
41+
#[stable(feature = "char_from_str", since = "1.19.0")]
42+
pub use core::char::ParseCharError;
4143

4244
// unstable reexports
4345
#[unstable(feature = "try_from", issue = "33417")]

0 commit comments

Comments
 (0)
Please sign in to comment.