mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 15:22:38 +00:00
uucore/format: refactor handling of flags to make clippy happy
This commit is contained in:
parent
3a21d27c1e
commit
a4f626efa2
2 changed files with 52 additions and 34 deletions
|
@ -60,7 +60,7 @@ pub enum PositiveSign {
|
||||||
Space,
|
Space,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum NumberAlignment {
|
pub enum NumberAlignment {
|
||||||
Left,
|
Left,
|
||||||
RightSpace,
|
RightSpace,
|
||||||
|
|
|
@ -87,6 +87,40 @@ enum Length {
|
||||||
LongDouble,
|
LongDouble,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, PartialEq, Eq)]
|
||||||
|
struct Flags {
|
||||||
|
minus: bool,
|
||||||
|
plus: bool,
|
||||||
|
space: bool,
|
||||||
|
hash: bool,
|
||||||
|
zero: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Flags {
|
||||||
|
pub fn parse(rest: &mut &[u8], index: &mut usize) -> Self {
|
||||||
|
let mut flags = Self::default();
|
||||||
|
|
||||||
|
while let Some(x) = rest.get(*index) {
|
||||||
|
match x {
|
||||||
|
b'-' => flags.minus = true,
|
||||||
|
b'+' => flags.plus = true,
|
||||||
|
b' ' => flags.space = true,
|
||||||
|
b'#' => flags.hash = true,
|
||||||
|
b'0' => flags.zero = true,
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
*index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
flags
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether any of the flags is set to true
|
||||||
|
fn any(&self) -> bool {
|
||||||
|
self != &Self::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Spec {
|
impl Spec {
|
||||||
pub fn parse<'a>(rest: &mut &'a [u8]) -> Result<Self, &'a [u8]> {
|
pub fn parse<'a>(rest: &mut &'a [u8]) -> Result<Self, &'a [u8]> {
|
||||||
// Based on the C++ reference, the spec format looks like:
|
// Based on the C++ reference, the spec format looks like:
|
||||||
|
@ -97,28 +131,12 @@ impl Spec {
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
let start = *rest;
|
let start = *rest;
|
||||||
|
|
||||||
let mut minus = false;
|
let flags = Flags::parse(rest, &mut index);
|
||||||
let mut plus = false;
|
|
||||||
let mut space = false;
|
|
||||||
let mut hash = false;
|
|
||||||
let mut zero = false;
|
|
||||||
|
|
||||||
while let Some(x) = rest.get(index) {
|
let positive_sign = match flags {
|
||||||
match x {
|
Flags { plus: true, .. } => PositiveSign::Plus,
|
||||||
b'-' => minus = true,
|
Flags { space: true, .. } => PositiveSign::Space,
|
||||||
b'+' => plus = true,
|
_ => PositiveSign::None,
|
||||||
b' ' => space = true,
|
|
||||||
b'#' => hash = true,
|
|
||||||
b'0' => zero = true,
|
|
||||||
_ => break,
|
|
||||||
}
|
|
||||||
index += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let positive_sign = match (plus, space) {
|
|
||||||
(true, _) => PositiveSign::Plus,
|
|
||||||
(false, true) => PositiveSign::Space,
|
|
||||||
(false, false) => PositiveSign::None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let width = eat_asterisk_or_number(rest, &mut index);
|
let width = eat_asterisk_or_number(rest, &mut index);
|
||||||
|
@ -133,9 +151,9 @@ impl Spec {
|
||||||
// The `0` flag is ignored if `-` is given or a precision is specified.
|
// The `0` flag is ignored if `-` is given or a precision is specified.
|
||||||
// So the only case for RightZero, is when `-` is not given and the
|
// So the only case for RightZero, is when `-` is not given and the
|
||||||
// precision is none.
|
// precision is none.
|
||||||
let alignment = if minus {
|
let alignment = if flags.minus {
|
||||||
NumberAlignment::Left
|
NumberAlignment::Left
|
||||||
} else if zero && precision.is_none() {
|
} else if flags.zero && precision.is_none() {
|
||||||
NumberAlignment::RightZero
|
NumberAlignment::RightZero
|
||||||
} else {
|
} else {
|
||||||
NumberAlignment::RightSpace
|
NumberAlignment::RightSpace
|
||||||
|
@ -153,38 +171,38 @@ impl Spec {
|
||||||
Ok(match type_spec {
|
Ok(match type_spec {
|
||||||
// GNU accepts minus, plus and space even though they are not used
|
// GNU accepts minus, plus and space even though they are not used
|
||||||
b'c' => {
|
b'c' => {
|
||||||
if hash || precision.is_some() {
|
if flags.hash || precision.is_some() {
|
||||||
return Err(&start[..index]);
|
return Err(&start[..index]);
|
||||||
}
|
}
|
||||||
Self::Char {
|
Self::Char {
|
||||||
width,
|
width,
|
||||||
align_left: minus,
|
align_left: flags.minus,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b's' => {
|
b's' => {
|
||||||
if hash {
|
if flags.hash {
|
||||||
return Err(&start[..index]);
|
return Err(&start[..index]);
|
||||||
}
|
}
|
||||||
Self::String {
|
Self::String {
|
||||||
precision,
|
precision,
|
||||||
width,
|
width,
|
||||||
align_left: minus,
|
align_left: flags.minus,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b'b' => {
|
b'b' => {
|
||||||
if hash || minus || plus || space || width.is_some() || precision.is_some() {
|
if flags.any() || width.is_some() || precision.is_some() {
|
||||||
return Err(&start[..index]);
|
return Err(&start[..index]);
|
||||||
}
|
}
|
||||||
Self::EscapedString
|
Self::EscapedString
|
||||||
}
|
}
|
||||||
b'q' => {
|
b'q' => {
|
||||||
if hash || minus || plus || space || width.is_some() || precision.is_some() {
|
if flags.any() || width.is_some() || precision.is_some() {
|
||||||
return Err(&start[..index]);
|
return Err(&start[..index]);
|
||||||
}
|
}
|
||||||
Self::QuotedString
|
Self::QuotedString
|
||||||
}
|
}
|
||||||
b'd' | b'i' => {
|
b'd' | b'i' => {
|
||||||
if hash {
|
if flags.hash {
|
||||||
return Err(&start[..index]);
|
return Err(&start[..index]);
|
||||||
}
|
}
|
||||||
Self::SignedInt {
|
Self::SignedInt {
|
||||||
|
@ -196,10 +214,10 @@ impl Spec {
|
||||||
}
|
}
|
||||||
c @ (b'u' | b'o' | b'x' | b'X') => {
|
c @ (b'u' | b'o' | b'x' | b'X') => {
|
||||||
// Normal unsigned integer cannot have a prefix
|
// Normal unsigned integer cannot have a prefix
|
||||||
if *c == b'u' && hash {
|
if *c == b'u' && flags.hash {
|
||||||
return Err(&start[..index]);
|
return Err(&start[..index]);
|
||||||
}
|
}
|
||||||
let prefix = match hash {
|
let prefix = match flags.hash {
|
||||||
false => Prefix::No,
|
false => Prefix::No,
|
||||||
true => Prefix::Yes,
|
true => Prefix::Yes,
|
||||||
};
|
};
|
||||||
|
@ -227,7 +245,7 @@ impl Spec {
|
||||||
b'a' | b'A' => FloatVariant::Hexadecimal,
|
b'a' | b'A' => FloatVariant::Hexadecimal,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
force_decimal: match hash {
|
force_decimal: match flags.hash {
|
||||||
false => ForceDecimal::No,
|
false => ForceDecimal::No,
|
||||||
true => ForceDecimal::Yes,
|
true => ForceDecimal::Yes,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue