Skip to content
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

Format libcore with rustfmt #66691

Merged
merged 2 commits into from
Nov 27, 2019
Merged
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
18 changes: 12 additions & 6 deletions src/libcore/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#![stable(feature = "core_ascii", since = "1.26.0")]

use crate::fmt;
use crate::ops::Range;
use crate::iter::FusedIterator;
use crate::ops::Range;
use crate::str::from_utf8_unchecked;

/// An iterator over the escaped version of a byte.
Expand Down Expand Up @@ -100,15 +100,15 @@ pub fn escape_default(c: u8) -> EscapeDefault {
b'\\' => ([b'\\', b'\\', 0, 0], 2),
b'\'' => ([b'\\', b'\'', 0, 0], 2),
b'"' => ([b'\\', b'"', 0, 0], 2),
b'\x20' ..= b'\x7e' => ([c, 0, 0, 0], 1),
b'\x20'..=b'\x7e' => ([c, 0, 0, 0], 1),
_ => ([b'\\', b'x', hexify(c >> 4), hexify(c & 0xf)], 4),
};

return EscapeDefault { range: 0..len, data };

fn hexify(b: u8) -> u8 {
match b {
0 ..= 9 => b'0' + b,
0..=9 => b'0' + b,
_ => b'a' + b - 10,
}
}
Expand All @@ -117,9 +117,15 @@ pub fn escape_default(c: u8) -> EscapeDefault {
#[stable(feature = "rust1", since = "1.0.0")]
impl Iterator for EscapeDefault {
type Item = u8;
fn next(&mut self) -> Option<u8> { self.range.next().map(|i| self.data[i]) }
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
fn last(mut self) -> Option<u8> { self.next_back() }
fn next(&mut self) -> Option<u8> {
self.range.next().map(|i| self.data[i])
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
fn last(mut self) -> Option<u8> {
self.next_back()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl DoubleEndedIterator for EscapeDefault {
Expand Down
31 changes: 10 additions & 21 deletions src/libcore/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ impl From<u8> for char {
}
}


/// An error which can be returned when parsing a char.
#[stable(feature = "char_from_str", since = "1.20.0")]
#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -167,16 +166,16 @@ pub struct ParseCharError {
}

impl ParseCharError {
#[unstable(feature = "char_error_internals",
reason = "this method should not be available publicly",
issue = "0")]
#[unstable(
feature = "char_error_internals",
reason = "this method should not be available publicly",
issue = "0"
)]
#[doc(hidden)]
pub fn __description(&self) -> &str {
match self.kind {
CharErrorKind::EmptyString => {
"cannot parse char from empty string"
},
CharErrorKind::TooManyChars => "too many characters in string"
CharErrorKind::EmptyString => "cannot parse char from empty string",
CharErrorKind::TooManyChars => "too many characters in string",
}
}
}
Expand All @@ -194,7 +193,6 @@ impl fmt::Display for ParseCharError {
}
}


#[stable(feature = "char_from_str", since = "1.20.0")]
impl FromStr for char {
type Err = ParseCharError;
Expand All @@ -203,18 +201,13 @@ impl FromStr for char {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut chars = s.chars();
match (chars.next(), chars.next()) {
(None, _) => {
Err(ParseCharError { kind: CharErrorKind::EmptyString })
},
(None, _) => Err(ParseCharError { kind: CharErrorKind::EmptyString }),
(Some(c), None) => Ok(c),
_ => {
Err(ParseCharError { kind: CharErrorKind::TooManyChars })
}
_ => Err(ParseCharError { kind: CharErrorKind::TooManyChars }),
}
}
}


#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<u32> for char {
type Error = CharTryFromError;
Expand Down Expand Up @@ -304,11 +297,7 @@ pub fn from_digit(num: u32, radix: u32) -> Option<char> {
}
if num < radix {
let num = num as u8;
if num < 10 {
Some((b'0' + num) as char)
} else {
Some((b'a' + num - 10) as char)
}
if num < 10 { Some((b'0' + num) as char) } else { Some((b'a' + num - 10) as char) }
} else {
None
}
Expand Down
10 changes: 4 additions & 6 deletions src/libcore/char/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use super::from_u32_unchecked;
#[stable(feature = "decode_utf16", since = "1.9.0")]
#[derive(Clone, Debug)]
pub struct DecodeUtf16<I>
where I: Iterator<Item = u16>
where
I: Iterator<Item = u16>,
{
iter: I,
buf: Option<u16>,
Expand Down Expand Up @@ -70,10 +71,7 @@ pub struct DecodeUtf16Error {
#[stable(feature = "decode_utf16", since = "1.9.0")]
#[inline]
pub fn decode_utf16<I: IntoIterator<Item = u16>>(iter: I) -> DecodeUtf16<I::IntoIter> {
DecodeUtf16 {
iter: iter.into_iter(),
buf: None,
}
DecodeUtf16 { iter: iter.into_iter(), buf: None }
}

#[stable(feature = "decode_utf16", since = "1.9.0")]
Expand All @@ -83,7 +81,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
fn next(&mut self) -> Option<Result<char, DecodeUtf16Error>> {
let u = match self.buf.take() {
Some(buf) => buf,
None => self.iter.next()?
None => self.iter.next()?,
};

if u < 0xD800 || 0xDFFF < u {
Expand Down
18 changes: 3 additions & 15 deletions src/libcore/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ impl char {
}
};

if val < radix {
Some(val)
} else {
None
}
if val < radix { Some(val) } else { None }
}

/// Returns an iterator that yields the hexadecimal Unicode escape of a
Expand Down Expand Up @@ -950,11 +946,7 @@ impl char {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline]
pub fn to_ascii_uppercase(&self) -> char {
if self.is_ascii() {
(*self as u8).to_ascii_uppercase() as char
} else {
*self
}
if self.is_ascii() { (*self as u8).to_ascii_uppercase() as char } else { *self }
}

/// Makes a copy of the value in its ASCII lower case equivalent.
Expand Down Expand Up @@ -982,11 +974,7 @@ impl char {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline]
pub fn to_ascii_lowercase(&self) -> char {
if self.is_ascii() {
(*self as u8).to_ascii_lowercase() as char
} else {
*self
}
if self.is_ascii() { (*self as u8).to_ascii_lowercase() as char } else { *self }
}

/// Checks that two values are an ASCII case-insensitive match.
Expand Down
81 changes: 40 additions & 41 deletions src/libcore/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ mod decode;
mod methods;

// stable re-exports
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::convert::{from_u32, from_digit};
#[stable(feature = "char_from_unchecked", since = "1.5.0")]
pub use self::convert::from_u32_unchecked;
#[stable(feature = "char_from_str", since = "1.20.0")]
pub use self::convert::ParseCharError;
#[stable(feature = "try_from", since = "1.34.0")]
pub use self::convert::CharTryFromError;
#[stable(feature = "char_from_str", since = "1.20.0")]
pub use self::convert::ParseCharError;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::convert::{from_digit, from_u32};
#[stable(feature = "decode_utf16", since = "1.9.0")]
pub use self::decode::{decode_utf16, DecodeUtf16, DecodeUtf16Error};

Expand All @@ -45,13 +45,13 @@ use crate::fmt::{self, Write};
use crate::iter::FusedIterator;

// UTF-8 ranges and tags for encoding characters
const TAG_CONT: u8 = 0b1000_0000;
const TAG_TWO_B: u8 = 0b1100_0000;
const TAG_THREE_B: u8 = 0b1110_0000;
const TAG_FOUR_B: u8 = 0b1111_0000;
const MAX_ONE_B: u32 = 0x80;
const MAX_TWO_B: u32 = 0x800;
const MAX_THREE_B: u32 = 0x10000;
const TAG_CONT: u8 = 0b1000_0000;
const TAG_TWO_B: u8 = 0b1100_0000;
const TAG_THREE_B: u8 = 0b1110_0000;
const TAG_FOUR_B: u8 = 0b1111_0000;
const MAX_ONE_B: u32 = 0x80;
const MAX_TWO_B: u32 = 0x800;
const MAX_THREE_B: u32 = 0x10000;
Comment on lines -48 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to register exceptions from rustfmt? The manual alignment here, at least for the first tags that are written in binary, was quite useful IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, but I don't think the alignment is enough of a readability improvement to be worth making an exception here. I think the underscore grouping is sufficient to read them clearly. I am amenable in general to making rustfmt exceptions but I think it doesn't make enough of a difference in this case.


/*
Lu Uppercase_Letter an uppercase letter
Expand Down Expand Up @@ -190,11 +190,11 @@ impl Iterator for EscapeUnicode {
match self.state {
EscapeUnicodeState::Done => None,

EscapeUnicodeState::RightBrace |
EscapeUnicodeState::Value |
EscapeUnicodeState::LeftBrace |
EscapeUnicodeState::Type |
EscapeUnicodeState::Backslash => Some('}'),
EscapeUnicodeState::RightBrace
| EscapeUnicodeState::Value
| EscapeUnicodeState::LeftBrace
| EscapeUnicodeState::Type
| EscapeUnicodeState::Backslash => Some('}'),
}
}
}
Expand All @@ -204,14 +204,15 @@ impl ExactSizeIterator for EscapeUnicode {
#[inline]
fn len(&self) -> usize {
// The match is a single memory access with no branching
self.hex_digit_idx + match self.state {
EscapeUnicodeState::Done => 0,
EscapeUnicodeState::RightBrace => 1,
EscapeUnicodeState::Value => 2,
EscapeUnicodeState::LeftBrace => 3,
EscapeUnicodeState::Type => 4,
EscapeUnicodeState::Backslash => 5,
}
self.hex_digit_idx
+ match self.state {
EscapeUnicodeState::Done => 0,
EscapeUnicodeState::RightBrace => 1,
EscapeUnicodeState::Value => 2,
EscapeUnicodeState::LeftBrace => 3,
EscapeUnicodeState::Type => 4,
EscapeUnicodeState::Backslash => 5,
}
}
}

Expand All @@ -238,7 +239,7 @@ impl fmt::Display for EscapeUnicode {
#[derive(Clone, Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct EscapeDefault {
state: EscapeDefaultState
state: EscapeDefaultState,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -284,24 +285,20 @@ impl Iterator for EscapeDefault {
EscapeDefaultState::Backslash(c) if n == 0 => {
self.state = EscapeDefaultState::Char(c);
Some('\\')
},
}
EscapeDefaultState::Backslash(c) if n == 1 => {
self.state = EscapeDefaultState::Done;
Some(c)
},
}
EscapeDefaultState::Backslash(_) => {
self.state = EscapeDefaultState::Done;
None
},
}
EscapeDefaultState::Char(c) => {
self.state = EscapeDefaultState::Done;

if n == 0 {
Some(c)
} else {
None
}
},
if n == 0 { Some(c) } else { None }
}
EscapeDefaultState::Done => None,
EscapeDefaultState::Unicode(ref mut i) => i.nth(n),
}
Expand Down Expand Up @@ -355,12 +352,16 @@ pub struct EscapeDebug(EscapeDefault);
#[stable(feature = "char_escape_debug", since = "1.20.0")]
impl Iterator for EscapeDebug {
type Item = char;
fn next(&mut self) -> Option<char> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
fn next(&mut self) -> Option<char> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}

#[stable(feature = "char_escape_debug", since = "1.20.0")]
impl ExactSizeIterator for EscapeDebug { }
impl ExactSizeIterator for EscapeDebug {}

#[stable(feature = "fused", since = "1.26.0")]
impl FusedIterator for EscapeDebug {}
Expand Down Expand Up @@ -440,7 +441,7 @@ impl CaseMappingIter {
fn new(chars: [char; 3]) -> CaseMappingIter {
if chars[2] == '\0' {
if chars[1] == '\0' {
CaseMappingIter::One(chars[0]) // Including if chars[0] == '\0'
CaseMappingIter::One(chars[0]) // Including if chars[0] == '\0'
} else {
CaseMappingIter::Two(chars[0], chars[1])
}
Expand Down Expand Up @@ -493,9 +494,7 @@ impl fmt::Display for CaseMappingIter {
f.write_char(b)?;
f.write_char(c)
}
CaseMappingIter::One(c) => {
f.write_char(c)
}
CaseMappingIter::One(c) => f.write_char(c),
CaseMappingIter::Zero => Ok(()),
}
}
Expand Down
Loading