Rollup merge of #128641 - Konippi:standardize-duplicate-processes-in-parser, r=scottmcm

refactor: standardize duplicate processes in parser

## Summary
This PR refactors the `read_number` function to standardize duplicate code, improve readability, and enhance efficiency.

## Changes
- Merged the logic for both `max_digits` cases into a single `read_atomically` closure
- Simplified control flow and reduced code duplication
This commit is contained in:
Matthias Krüger 2024-09-01 03:58:03 +02:00 committed by GitHub
commit da65b9064f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -112,18 +112,18 @@ fn read_number<T: ReadNumberHelper + TryFrom<u32>>(
max_digits: Option<usize>, max_digits: Option<usize>,
allow_zero_prefix: bool, allow_zero_prefix: bool,
) -> Option<T> { ) -> Option<T> {
self.read_atomically(move |p| {
let mut digit_count = 0;
let has_leading_zero = p.peek_char() == Some('0');
// If max_digits.is_some(), then we are parsing a `u8` or `u16` and // If max_digits.is_some(), then we are parsing a `u8` or `u16` and
// don't need to use checked arithmetic since it fits within a `u32`. // don't need to use checked arithmetic since it fits within a `u32`.
if let Some(max_digits) = max_digits { let result = if let Some(max_digits) = max_digits {
// u32::MAX = 4_294_967_295u32, which is 10 digits long. // u32::MAX = 4_294_967_295u32, which is 10 digits long.
// `max_digits` must be less than 10 to not overflow a `u32`. // `max_digits` must be less than 10 to not overflow a `u32`.
debug_assert!(max_digits < 10); debug_assert!(max_digits < 10);
self.read_atomically(move |p| {
let mut result = 0_u32; let mut result = 0_u32;
let mut digit_count = 0;
let has_leading_zero = p.peek_char() == Some('0');
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) { while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
result *= radix; result *= radix;
result += digit; result += digit;
@ -134,19 +134,9 @@ fn read_number<T: ReadNumberHelper + TryFrom<u32>>(
} }
} }
if digit_count == 0 {
None
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
None
} else {
result.try_into().ok() result.try_into().ok()
}
})
} else { } else {
self.read_atomically(move |p| {
let mut result = T::ZERO; let mut result = T::ZERO;
let mut digit_count = 0;
let has_leading_zero = p.peek_char() == Some('0');
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) { while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
result = result.checked_mul(radix)?; result = result.checked_mul(radix)?;
@ -154,16 +144,18 @@ fn read_number<T: ReadNumberHelper + TryFrom<u32>>(
digit_count += 1; digit_count += 1;
} }
Some(result)
};
if digit_count == 0 { if digit_count == 0 {
None None
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 { } else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
None None
} else { } else {
Some(result) result
} }
}) })
} }
}
/// Reads an IPv4 address. /// Reads an IPv4 address.
fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> { fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> {