// fill character
if let Some(&(_, c)) = self.cur.peek() {
- match self.cur.clone().skip(1).next() {
+ match self.cur.clone().nth(1) {
Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
spec.fill = Some(c);
self.cur.next();
if word.is_empty() {
self.cur = tmp;
CountImplied
+ } else if self.consume('$') {
+ CountIsName(word)
} else {
- if self.consume('$') {
- CountIsName(word)
- } else {
- self.cur = tmp;
- CountImplied
- }
+ self.cur = tmp;
+ CountImplied
}
}
}
if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' ) {
return Err(());
}
- return Ok(Id { name: name });
+
+ Ok(Id { name })
}
pub fn as_slice(&'a self) -> &'a str {
/// Renders text as string suitable for a label in a .dot file.
/// This includes quotes or suitable delimiters.
pub fn to_dot_string(&self) -> String {
- match self {
- &LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
- &EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)),
- &HtmlStr(ref s) => format!("<{}>", s),
+ match *self {
+ LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
+ EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)),
+ HtmlStr(ref s) => format!("<{}>", s),
}
}
// Check whether we should use scientific notation.
let scientific = if width == 0 {
true
+ } else if exp >= 0 {
+ // 765e3 --> 765000
+ // ^^^
+ // But we shouldn't make the number look more precise than it is.
+ exp as usize > width || digits + exp as usize > precision
} else {
- if exp >= 0 {
- // 765e3 --> 765000
- // ^^^
- // But we shouldn't make the number look more precise than it is.
- exp as usize > width || digits + exp as usize > precision
+ // Power of the most significant digit.
+ let msd = exp + (digits - 1) as ExpInt;
+ if msd >= 0 {
+ // 765e-2 == 7.65
+ false
} else {
- // Power of the most significant digit.
- let msd = exp + (digits - 1) as ExpInt;
- if msd >= 0 {
- // 765e-2 == 7.65
- false
- } else {
- // 765e-5 == 0.00765
- // ^ ^^
- -msd as usize > width
- }
+ // 765e-5 == 0.00765
+ // ^ ^^
+ -msd as usize > width
}
};
// exponent = 1..10
// significand = 1..1
IeeeFloat {
- sig: [!0 & ((1 << S::PRECISION) - 1)],
+ sig: [(1 << S::PRECISION) - 1],
exp: S::MAX_EXP,
category: Category::Normal,
sign: false,
}
// If this is a truncation, perform the shift.
- let mut loss = Loss::ExactlyZero;
- if shift < 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
- loss = sig::shift_right(&mut r.sig, &mut 0, -shift as usize);
- }
+ let loss = if shift < 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
+ sig::shift_right(&mut r.sig, &mut 0, -shift as usize)
+ } else {
+ Loss::ExactlyZero
+ };
// If this is an extension, perform the shift.
if shift > 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
bit_pos -= 4;
if bit_pos >= 0 {
r.sig[0] |= (hex_value as Limb) << bit_pos;
- } else {
- // If zero or one-half (the hexadecimal digit 8) are followed
- // by non-zero, they're a little more than zero or one-half.
- if let Some(ref mut loss) = loss {
- if hex_value != 0 {
- if *loss == Loss::ExactlyZero {
- *loss = Loss::LessThanHalf;
- }
- if *loss == Loss::ExactlyHalf {
- *loss = Loss::MoreThanHalf;
- }
+ // If zero or one-half (the hexadecimal digit 8) are followed
+ // by non-zero, they're a little more than zero or one-half.
+ } else if let Some(ref mut loss) = loss {
+ if hex_value != 0 {
+ if *loss == Loss::ExactlyZero {
+ *loss = Loss::LessThanHalf;
+ }
+ if *loss == Loss::ExactlyHalf {
+ *loss = Loss::MoreThanHalf;
}
- } else {
- loss = Some(match hex_value {
- 0 => Loss::ExactlyZero,
- 1..=7 => Loss::LessThanHalf,
- 8 => Loss::ExactlyHalf,
- 9..=15 => Loss::MoreThanHalf,
- _ => unreachable!(),
- });
}
+ } else {
+ loss = Some(match hex_value {
+ 0 => Loss::ExactlyZero,
+ 1..=7 => Loss::LessThanHalf,
+ 8 => Loss::ExactlyHalf,
+ 9..=15 => Loss::MoreThanHalf,
+ _ => unreachable!(),
+ });
}
} else if c == 'p' || c == 'P' {
if !any_digits {
/// One, not zero, based LSB. That is, returns 0 for a zeroed significand.
pub(super) fn olsb(limbs: &[Limb]) -> usize {
- for i in 0..limbs.len() {
- if limbs[i] != 0 {
- return i * LIMB_BITS + limbs[i].trailing_zeros() as usize + 1;
+ for (i, &limb) in limbs.iter().enumerate() {
+ if limb != 0 {
+ return i * LIMB_BITS + limb.trailing_zeros() as usize + 1;
}
}
/// One, not zero, based MSB. That is, returns 0 for a zeroed significand.
pub(super) fn omsb(limbs: &[Limb]) -> usize {
- for i in (0..limbs.len()).rev() {
- if limbs[i] != 0 {
- return (i + 1) * LIMB_BITS - limbs[i].leading_zeros() as usize;
+ for (i, &limb) in limbs.iter().enumerate().rev() {
+ if limb != 0 {
+ return (i + 1) * LIMB_BITS - limb.leading_zeros() as usize;
}
}
limb = dst[i - jump];
if shift > 0 {
limb <<= shift;
- if i >= jump + 1 {
+ if i > jump {
limb |= dst[i - jump - 1] >> (LIMB_BITS - shift);
}
}
let n = dst_limbs * LIMB_BITS - shift;
if n < src_bits {
let mask = (1 << (src_bits - n)) - 1;
- dst[dst_limbs - 1] |= (src[dst_limbs] & mask) << n % LIMB_BITS;
+ dst[dst_limbs - 1] |= (src[dst_limbs] & mask) << (n % LIMB_BITS);
} else if n > src_bits && src_bits % LIMB_BITS > 0 {
dst[dst_limbs - 1] &= (1 << (src_bits % LIMB_BITS)) - 1;
}
// the JSON parser is not updated to match the structs.
let get_req_field = |name: &str| {
- match obj.find(name)
- .map(|s| s.as_string())
- .and_then(|os| os.map(|s| s.to_string())) {
- Some(val) => Ok(val),
- None => {
- return Err(format!("Field {} in target specification is required", name))
- }
- }
+ obj.find(name)
+ .map(|s| s.as_string())
+ .and_then(|os| os.map(|s| s.to_string()))
+ .ok_or_else(|| format!("Field {} in target specification is required", name))
};
let get_opt_field = |name: &str, default: &str| {
fn to_hex(&self) -> String;
}
-const CHARS: &'static [u8] = b"0123456789abcdef";
+const CHARS: &[u8] = b"0123456789abcdef";
impl ToHex for [u8] {
/// Turn a vector of `u8` bytes into a hexadecimal string.
}
fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
- const BUF: &'static str = " ";
+ const BUF: &str = " ";
while n >= BUF.len() {
wr.write_str(BUF)?;
escape_str(self.writer, name)
} else {
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
- write!(self.writer, "{{\n")?;
+ writeln!(self.writer, "{{")?;
self.curr_indent += self.indent;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "\"variant\": ")?;
escape_str(self.writer, name)?;
- write!(self.writer, ",\n")?;
+ writeln!(self.writer, ",")?;
spaces(self.writer, self.curr_indent)?;
- write!(self.writer, "\"fields\": [\n")?;
+ writeln!(self.writer, "\"fields\": [")?;
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
- write!(self.writer, "\n")?;
+ writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
self.curr_indent -= self.indent;
- write!(self.writer, "]\n")?;
+ writeln!(self.writer, "]")?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "}}")?;
Ok(())
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx != 0 {
- write!(self.writer, ",\n")?;
+ writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
f(self)
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
- write!(self.writer, "\n")?;
+ writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "}}")?;
}
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx == 0 {
- write!(self.writer, "\n")?;
+ writeln!(self.writer)?;
} else {
- write!(self.writer, ",\n")?;
+ writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
escape_str(self.writer, name)?;
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
- write!(self.writer, "\n")?;
+ writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "]")?;
}
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx == 0 {
- write!(self.writer, "\n")?;
+ writeln!(self.writer)?;
} else {
- write!(self.writer, ",\n")?;
+ writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
f(self)
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
- write!(self.writer, "\n")?;
+ writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "}}")?;
}
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx == 0 {
- write!(self.writer, "\n")?;
+ writeln!(self.writer)?;
} else {
- write!(self.writer, ",\n")?;
+ writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
self.is_emitting_map_key = true;
// Used by Parser to test whether the top-most element is an index.
fn last_is_index(&self) -> bool {
- if self.is_empty() { return false; }
- return match *self.stack.last().unwrap() {
- InternalIndex(_) => true,
- _ => false,
+ if let Some(InternalIndex(_)) = self.stack.last() {
+ true
+ } else {
+ false
}
}
}
F64Value(res)
- } else {
- if neg {
- let res = (res as i64).wrapping_neg();
+ } else if neg {
+ let res = (res as i64).wrapping_neg();
- // Make sure we didn't underflow.
- if res > 0 {
- Error(SyntaxError(InvalidNumber, self.line, self.col))
- } else {
- I64Value(res)
- }
+ // Make sure we didn't underflow.
+ if res > 0 {
+ Error(SyntaxError(InvalidNumber, self.line, self.col))
} else {
- U64Value(res)
+ I64Value(res)
}
+ } else {
+ U64Value(res)
}
}
loop {
let mut byte = (value as u8) & 0x7f;
value >>= 7;
- let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
- ((value == -1) && ((byte & 0x40) != 0))));
+ let more = !(((value == 0) && ((byte & 0x40) == 0)) ||
+ ((value == -1) && ((byte & 0x40) != 0)));
if more {
byte |= 0x80; // Mark this byte to show that more bytes will follow.
fn cause(&self) -> Option<&dyn error::Error> {
use self::Error::*;
- match self {
- &IoError(ref e) => Some(e),
+ match *self {
+ IoError(ref e) => Some(e),
_ => None,
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
- match self {
- &TermUnset => Ok(()),
- &MalformedTerminfo(ref e) => e.fmt(f),
- &IoError(ref e) => e.fmt(f),
+ match *self {
+ TermUnset => Ok(()),
+ MalformedTerminfo(ref e) => e.fmt(f),
+ IoError(ref e) => e.fmt(f),
}
}
}
}
// Keep the metadata small
fn _from_path(path: &Path) -> Result<TermInfo, Error> {
- let file = File::open(path).map_err(|e| Error::IoError(e))?;
+ let file = File::open(path).map_err(Error::IoError)?;
let mut reader = BufReader::new(file);
- parse(&mut reader, false).map_err(|e| Error::MalformedTerminfo(e))
+ parse(&mut reader, false).map_err(Error::MalformedTerminfo)
}
}
use self::Param::*;
use self::States::*;
-use self::FormatState::*;
-use self::FormatOp::*;
use std::iter::repeat;
#[derive(Copy, PartialEq, Clone)]
enum FormatState {
- FormatStateFlags,
- FormatStateWidth,
- FormatStatePrecision,
+ Flags,
+ Width,
+ Precision,
}
/// Types of parameters a capability can use
if let Some(arg) = stack.pop() {
let flags = Flags::new();
let res = format(arg, FormatOp::from_char(cur), flags)?;
- output.extend(res.iter().map(|x| *x));
+ output.extend(res.iter().cloned());
} else {
return Err("stack is empty".to_string());
}
}
':' | '#' | ' ' | '.' | '0'..='9' => {
let mut flags = Flags::new();
- let mut fstate = FormatStateFlags;
+ let mut fstate = FormatState::Flags;
match cur {
':' => (),
'#' => flags.alternate = true,
' ' => flags.space = true,
- '.' => fstate = FormatStatePrecision,
+ '.' => fstate = FormatState::Precision,
'0'..='9' => {
flags.width = cur as usize - '0' as usize;
- fstate = FormatStateWidth;
+ fstate = FormatState::Width;
}
_ => unreachable!(),
}
(_, 'd') | (_, 'o') | (_, 'x') | (_, 'X') | (_, 's') => {
if let Some(arg) = stack.pop() {
let res = format(arg, FormatOp::from_char(cur), *flags)?;
- output.extend(res.iter().map(|x| *x));
+ output.extend(res.iter().cloned());
// will cause state to go to Nothing
old_state = FormatPattern(*flags, *fstate);
} else {
return Err("stack is empty".to_string());
}
}
- (FormatStateFlags, '#') => {
+ (FormatState::Flags, '#') => {
flags.alternate = true;
}
- (FormatStateFlags, '-') => {
+ (FormatState::Flags, '-') => {
flags.left = true;
}
- (FormatStateFlags, '+') => {
+ (FormatState::Flags, '+') => {
flags.sign = true;
}
- (FormatStateFlags, ' ') => {
+ (FormatState::Flags, ' ') => {
flags.space = true;
}
- (FormatStateFlags, '0'..='9') => {
+ (FormatState::Flags, '0'..='9') => {
flags.width = cur as usize - '0' as usize;
- *fstate = FormatStateWidth;
+ *fstate = FormatState::Width;
}
- (FormatStateFlags, '.') => {
- *fstate = FormatStatePrecision;
+ (FormatState::Flags, '.') => {
+ *fstate = FormatState::Precision;
}
- (FormatStateWidth, '0'..='9') => {
+ (FormatState::Width, '0'..='9') => {
let old = flags.width;
flags.width = flags.width * 10 + (cur as usize - '0' as usize);
if flags.width < old {
return Err("format width overflow".to_string());
}
}
- (FormatStateWidth, '.') => {
- *fstate = FormatStatePrecision;
+ (FormatState::Width, '.') => {
+ *fstate = FormatState::Precision;
}
- (FormatStatePrecision, '0'..='9') => {
+ (FormatState::Precision, '0'..='9') => {
let old = flags.precision;
flags.precision = flags.precision * 10 + (cur as usize - '0' as usize);
if flags.precision < old {
#[derive(Copy, Clone)]
enum FormatOp {
- FormatDigit,
- FormatOctal,
- FormatHex,
- FormatHEX,
- FormatString,
+ Digit,
+ Octal,
+ LowerHex,
+ UpperHex,
+ String,
}
impl FormatOp {
fn from_char(c: char) -> FormatOp {
match c {
- 'd' => FormatDigit,
- 'o' => FormatOctal,
- 'x' => FormatHex,
- 'X' => FormatHEX,
- 's' => FormatString,
+ 'd' => FormatOp::Digit,
+ 'o' => FormatOp::Octal,
+ 'x' => FormatOp::LowerHex,
+ 'X' => FormatOp::UpperHex,
+ 's' => FormatOp::String,
_ => panic!("bad FormatOp char"),
}
}
fn to_char(self) -> char {
match self {
- FormatDigit => 'd',
- FormatOctal => 'o',
- FormatHex => 'x',
- FormatHEX => 'X',
- FormatString => 's',
+ FormatOp::Digit => 'd',
+ FormatOp::Octal => 'o',
+ FormatOp::LowerHex => 'x',
+ FormatOp::UpperHex => 'X',
+ FormatOp::String => 's',
}
}
}
let mut s = match val {
Number(d) => {
match op {
- FormatDigit => {
+ FormatOp::Digit => {
if flags.sign {
format!("{:+01$}", d, flags.precision)
} else if d < 0 {
format!("{:01$}", d, flags.precision)
}
}
- FormatOctal => {
+ FormatOp::Octal => {
if flags.alternate {
// Leading octal zero counts against precision.
format!("0{:01$o}", d, flags.precision.saturating_sub(1))
format!("{:01$o}", d, flags.precision)
}
}
- FormatHex => {
+ FormatOp::LowerHex => {
if flags.alternate && d != 0 {
format!("0x{:01$x}", d, flags.precision)
} else {
format!("{:01$x}", d, flags.precision)
}
}
- FormatHEX => {
+ FormatOp::UpperHex => {
if flags.alternate && d != 0 {
format!("0X{:01$X}", d, flags.precision)
} else {
format!("{:01$X}", d, flags.precision)
}
}
- FormatString => return Err("non-number on stack with %s".to_string()),
+ FormatOp::String => return Err("non-number on stack with %s".to_string()),
}
.into_bytes()
}
Words(s) => {
match op {
- FormatString => {
+ FormatOp::String => {
let mut s = s.into_bytes();
if flags.precision > 0 && flags.precision < s.len() {
s.truncate(flags.precision);
Ok(true)
}
- fn get_ref<'a>(&'a self) -> &'a T {
+ fn get_ref(&self) -> &T {
&self.buf
}
- fn get_mut<'a>(&'a mut self) -> &'a mut T {
+ fn get_mut(&mut self) -> &mut T {
&mut self.buf
}