Skip to content

Commit cbf68bb

Browse files
committed
use two loops and remove mutable parse_sign_and_digits variable
1 parent 06ae139 commit cbf68bb

1 file changed

Lines changed: 25 additions & 28 deletions

File tree

  • native/spark-expr/src/conversion_funcs

native/spark-expr/src/conversion_funcs/cast.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,39 +2049,36 @@ fn do_parse_string_to_int_legacy<T: Integer + CheckedSub + CheckedNeg + From<u8>
20492049
};
20502050

20512051
let mut result: T = T::zero();
2052-
20532052
let radix = T::from(10_u8);
20542053
let stop_value = min_value / radix;
2055-
let mut parse_sign_and_digits = true;
20562054

2057-
for &ch in &trimmed_bytes[idx..] {
2058-
if parse_sign_and_digits {
2059-
if ch == b'.' {
2060-
// truncate decimal in legacy mode
2061-
parse_sign_and_digits = false;
2062-
continue;
2063-
}
2055+
let mut iter = trimmed_bytes[idx..].iter();
20642056

2065-
if !ch.is_ascii_digit() {
2066-
return Ok(None);
2067-
}
2057+
// Parse integer portion until '.' or end
2058+
for &ch in iter.by_ref() {
2059+
if ch == b'.' {
2060+
break;
2061+
}
20682062

2069-
if result < stop_value {
2070-
return Ok(None);
2071-
}
2072-
let v = result * radix;
2073-
let digit: T = T::from(ch - b'0');
2074-
match v.checked_sub(&digit) {
2075-
Some(x) if x <= T::zero() => result = x,
2076-
_ => {
2077-
return Ok(None);
2078-
}
2079-
}
2080-
} else {
2081-
// in legacy mode we still process chars after the dot and make sure the chars are digits
2082-
if !ch.is_ascii_digit() {
2083-
return Ok(None);
2084-
}
2063+
if !ch.is_ascii_digit() {
2064+
return Ok(None);
2065+
}
2066+
2067+
if result < stop_value {
2068+
return Ok(None);
2069+
}
2070+
let v = result * radix;
2071+
let digit: T = T::from(ch - b'0');
2072+
match v.checked_sub(&digit) {
2073+
Some(x) if x <= T::zero() => result = x,
2074+
_ => return Ok(None),
2075+
}
2076+
}
2077+
2078+
// Validate decimal portion (digits only, values ignored)
2079+
for &ch in iter {
2080+
if !ch.is_ascii_digit() {
2081+
return Ok(None);
20852082
}
20862083
}
20872084

0 commit comments

Comments
 (0)