Skip to content

Commit 42a6d64

Browse files
committed
perf_string_to_int
1 parent 0ae6515 commit 42a6d64

File tree

1 file changed

+21
-6
lines changed
  • native/spark-expr/src/conversion_funcs

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,22 +1965,37 @@ fn do_cast_string_to_int<
19651965
type_name: &str,
19661966
min_value: T,
19671967
) -> SparkResult<Option<T>> {
1968-
let trimmed_str = str.trim();
1969-
if trimmed_str.is_empty() {
1968+
1969+
let bytes = str.as_bytes();
1970+
let mut start = 0;
1971+
let mut end = bytes.len();
1972+
1973+
while start < end && bytes[start].is_ascii_whitespace() {
1974+
start += 1;
1975+
}
1976+
while end > start && bytes[end - 1].is_ascii_whitespace() {
1977+
end -= 1;
1978+
}
1979+
1980+
if start == end {
19701981
return none_or_err(eval_mode, type_name, str);
19711982
}
1983+
let trimmed_str = &str[start..end];
19721984
let len = trimmed_str.len();
1985+
let trimmed_bytes = trimmed_str.as_bytes();
1986+
19731987
let mut result: T = T::zero();
19741988
let mut negative = false;
19751989
let radix = T::from(10);
19761990
let stop_value = min_value / radix;
19771991
let mut parse_sign_and_digits = true;
19781992

1979-
for (i, ch) in trimmed_str.char_indices() {
1993+
for i in 0..len {
1994+
let ch = trimmed_bytes[i];
19801995
if parse_sign_and_digits {
19811996
if i == 0 {
1982-
negative = ch == '-';
1983-
let positive = ch == '+';
1997+
negative = ch == b'-';
1998+
let positive = ch == b'+';
19841999
if negative || positive {
19852000
if i + 1 == len {
19862001
// input string is just "+" or "-"
@@ -1991,7 +2006,7 @@ fn do_cast_string_to_int<
19912006
}
19922007
}
19932008

1994-
if ch == '.' {
2009+
if ch == b'.' {
19952010
if eval_mode == EvalMode::Legacy {
19962011
// truncate decimal in legacy mode
19972012
parse_sign_and_digits = false;

0 commit comments

Comments
 (0)