/// Parse a "+" or "-" sign. Returns the ASCII byte representing the sign, if present. pub(crate) constfn sign(input: &[u8]) -> Option<ParsedItem<'_, u8>> { match input {
[sign @ (b'-' | b'+'), remaining @ ..] => Some(ParsedItem(remaining, *sign)),
_ => None,
}
}
/// Consume the first matching item, returning its associated value. pub(crate) fn first_match<'a, T>(
options: impl IntoIterator<Item = (&'a [u8], T)>,
case_sensitive: bool,
) -> impl FnMut(&'a [u8]) -> Option<ParsedItem<'a, T>> { letmut options = options.into_iter(); move |input| {
options.find_map(|(expected, t)| { if case_sensitive {
Some(ParsedItem(input.strip_prefix(expected)?, t))
} else { let n = expected.len(); if n <= input.len() { let (head, tail) = input.split_at(n); if head.eq_ignore_ascii_case(expected) { return Some(ParsedItem(tail, t));
}
}
None
}
})
}
}
/// Consume zero or more instances of the provided parser. The parser must return the unit value. pub(crate) fn zero_or_more<'a, P: Fn(&'a [u8]) -> Option<ParsedItem<'a, ()>>>(
parser: P,
) -> impl FnMut(&'a [u8]) -> ParsedItem<'a, ()> { move |mut input| { whilelet Some(remaining) = parser(input) {
input = remaining.into_inner();
}
ParsedItem(input, ())
}
}
/// Consume one of or more instances of the provided parser. The parser must produce the unit value. pub(crate) fn one_or_more<'a, P: Fn(&'a [u8]) -> Option<ParsedItem<'a, ()>>>(
parser: P,
) -> implFn(&'a [u8]) -> Option<ParsedItem<'a, ()>> { move |mut input| {
input = parser(input)?.into_inner(); whilelet Some(remaining) = parser(input) {
input = remaining.into_inner();
}
Some(ParsedItem(input, ()))
}
}
/// Consume between `n` and `m` instances of the provided parser. pub(crate) fn n_to_m< 'a, const N: u8, const M: u8,
T,
P: Fn(&'a [u8]) -> Option<ParsedItem<'a, T>>,
>(
parser: P,
) -> implFn(&'a [u8]) -> Option<ParsedItem<'a, &'a [u8]>> {
debug_assert!(M >= N); move |mut input| { // We need to keep this to determine the total length eventually consumed. let orig_input = input;
// Mandatory for _ in0..N {
input = parser(input)?.0;
}
// Optional for _ in N..M { match parser(input) {
Some(parsed) => input = parsed.0,
None => break,
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.