wcstod: extract wcstod_inner()

This function can be called with any char iterator, not just IntoCharIter
values.
This commit is contained in:
Xiretza 2023-03-05 15:30:54 +01:00 committed by Johannes Altmanninger
parent 6b687adb40
commit be2ea8edf0

View file

@ -2,28 +2,11 @@ use super::errors::Error;
use crate::wchar::IntoCharIter;
use fast_float::parse_partial_iter;
/// Parses a 64-bit floating point number.
///
/// Leading whitespace and trailing characters are ignored. If the input
/// string does not contain a valid floating point number (where e.g.
/// `"."` is seen as a valid floating point number), `None` is returned.
/// Otherwise the parsed floating point number is returned.
///
/// The `decimal_sep` parameter is used to specify the decimal separator.
/// '.' is a normal default.
///
/// The `consumed` parameter is used to return the number of characters
/// consumed, similar to the "end" parameter to strtod.
/// This is only meaningful if parsing succeeds.
///
/// Error::Overflow is returned if the value is too large in magnitude.
pub fn wcstod<Chars>(input: Chars, decimal_sep: char, consumed: &mut usize) -> Result<f64, Error>
fn wcstod_inner<I>(mut chars: I, decimal_sep: char, consumed: &mut usize) -> Result<f64, Error>
where
Chars: IntoCharIter,
I: Iterator<Item = char> + Clone,
{
let mut chars = input.chars();
let mut whitespace_skipped = 0;
// Skip leading whitespace.
loop {
match chars.clone().next() {
@ -73,6 +56,28 @@ where
Ok(val)
}
/// Parses a 64-bit floating point number.
///
/// Leading whitespace and trailing characters are ignored. If the input
/// string does not contain a valid floating point number (where e.g.
/// `"."` is seen as a valid floating point number), `None` is returned.
/// Otherwise the parsed floating point number is returned.
///
/// The `decimal_sep` parameter is used to specify the decimal separator.
/// '.' is a normal default.
///
/// The `consumed` parameter is used to return the number of characters
/// consumed, similar to the "end" parameter to strtod.
/// This is only meaningful if parsing succeeds.
///
/// Error::Overflow is returned if the value is too large in magnitude.
pub fn wcstod<Chars>(input: Chars, decimal_sep: char, consumed: &mut usize) -> Result<f64, Error>
where
Chars: IntoCharIter,
{
wcstod_inner(input.chars(), decimal_sep, consumed)
}
/// Check if a character iterator appears to be a hex float.
/// That is, an optional + or -, followed by 0x or 0X, and a hex digit.
pub fn is_hex_float<Chars: Iterator<Item = char>>(mut chars: Chars) -> bool {