Jefffrey commented on code in PR #10396:
URL: https://github.com/apache/arrow-rs/pull/10396#discussion_r3642453324


##########
arrow-cast/src/parse.rs:
##########
@@ -454,49 +457,56 @@ impl Parser for Float16Type {
 
 impl Parser for Float32Type {
     fn parse(string: &str) -> Option<f32> {
-        let string = truncate_white_space(string);
+        if let Ok(raw_float) = lexical_core::parse(string.as_bytes()) {
+            return Some(raw_float);
+        }
+        let string = trim_pre_and_post_whitespace(string);
         lexical_core::parse(string.as_bytes()).ok()
     }
 }
 
 impl Parser for Float64Type {
     fn parse(string: &str) -> Option<f64> {
-        let string = truncate_white_space(string);
+        let string = trim_pre_and_post_whitespace(string);

Review Comment:
   looks like f64 doesnt have latest changes



##########
arrow-cast/src/parse.rs:
##########
@@ -454,49 +457,56 @@ impl Parser for Float16Type {
 
 impl Parser for Float32Type {
     fn parse(string: &str) -> Option<f32> {
-        let string = truncate_white_space(string);
+        if let Ok(raw_float) = lexical_core::parse(string.as_bytes()) {
+            return Some(raw_float);
+        }
+        let string = trim_pre_and_post_whitespace(string);
         lexical_core::parse(string.as_bytes()).ok()
     }
 }
 
 impl Parser for Float64Type {
     fn parse(string: &str) -> Option<f64> {
-        let string = truncate_white_space(string);
+        let string = trim_pre_and_post_whitespace(string);
         lexical_core::parse(string.as_bytes()).ok()
     }
 }
+
+/// this is a no-op if the string starts and ends with a digit, otherwise it 
will trim whitespace from the start and end of the string.
 #[inline]
-fn truncate_white_space(string: &str) -> &str {
-    if string
-        .as_bytes()
-        .first()
-        .is_some_and(|first_byte| !first_byte.is_ascii_digit())
-    {
-        string.trim_ascii_start()
-    } else {
-        string
+fn trim_pre_and_post_whitespace(string: &str) -> &str {
+    let bytes = string.as_bytes();
+    let prefix = bytes.first().is_some_and(|b| !b.is_ascii_digit());
+    let suffix = bytes.last().is_some_and(|b| !b.is_ascii_digit());
+    match (prefix, suffix) {
+        (false, false) => string,
+        (true, false) => string.trim_ascii_start(),
+        (false, true) => string.trim_ascii_end(),
+        (true, true) => string.trim_ascii(),
     }
 }
 
 macro_rules! parser_primitive {
     ($t:ty) => {
         impl Parser for $t {
-            fn parse(mut string: &str) -> Option<Self::Native> {
-                if !string.as_bytes().last().is_some_and(|x| 
x.is_ascii_digit()) {
-                    return None;
+            fn parse(string: &str) -> Option<Self::Native> {

Review Comment:
   perhaps we can add one benchmark for i32 just so we have a reference



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to