BurntSushi commented on code in PR #5444:
URL: https://github.com/apache/arrow-rs/pull/5444#discussion_r1516013300


##########
arrow-cast/src/parse.rs:
##########
@@ -556,65 +556,30 @@ fn parse_date(string: &str) -> Option<NaiveDate> {
             .map(|dt| dt.date_naive())
             .ok();
     };
-    let mut digits = [0; 10];
-    let mut mask = 0;
-
-    // Treating all bytes the same way, helps LLVM vectorise this correctly
-    for (idx, (o, i)) in digits.iter_mut().zip(string.bytes()).enumerate() {
-        *o = i.wrapping_sub(b'0');
-        mask |= ((*o < 10) as u16) << idx
-    }
-
-    const HYPHEN: u8 = b'-'.wrapping_sub(b'0');
-
-    //  refer to https://www.rfc-editor.org/rfc/rfc3339#section-3
-    if digits[4] != HYPHEN {
-        let (year, month, day) = match (mask, string.len()) {
-            (0b11111111, 8) => (
-                digits[0] as u16 * 1000
-                    + digits[1] as u16 * 100
-                    + digits[2] as u16 * 10
-                    + digits[3] as u16,
-                digits[4] * 10 + digits[5],
-                digits[6] * 10 + digits[7],
-            ),
-            _ => return None,
-        };
-        return NaiveDate::from_ymd_opt(year as _, month as _, day as _);
-    }
-
-    let (month, day) = match mask {
-        0b1101101111 => {
-            if digits[7] != HYPHEN {
-                return None;
-            }
-            (digits[5] * 10 + digits[6], digits[8] * 10 + digits[9])
-        }
-        0b101101111 => {
-            if digits[7] != HYPHEN {
-                return None;
-            }
-            (digits[5] * 10 + digits[6], digits[8])
-        }
-        0b110101111 => {
-            if digits[6] != HYPHEN {
-                return None;
-            }
-            (digits[5], digits[7] * 10 + digits[8])
+    use regex::Regex;

Review Comment:
   Yeah I'd expect using a regex, particularly with capture groups, to be 
slower than something hand-rolled here.



##########
arrow-cast/src/parse.rs:
##########
@@ -556,65 +556,30 @@ fn parse_date(string: &str) -> Option<NaiveDate> {
             .map(|dt| dt.date_naive())
             .ok();
     };
-    let mut digits = [0; 10];
-    let mut mask = 0;
-
-    // Treating all bytes the same way, helps LLVM vectorise this correctly
-    for (idx, (o, i)) in digits.iter_mut().zip(string.bytes()).enumerate() {
-        *o = i.wrapping_sub(b'0');
-        mask |= ((*o < 10) as u16) << idx
-    }
-
-    const HYPHEN: u8 = b'-'.wrapping_sub(b'0');
-
-    //  refer to https://www.rfc-editor.org/rfc/rfc3339#section-3
-    if digits[4] != HYPHEN {
-        let (year, month, day) = match (mask, string.len()) {
-            (0b11111111, 8) => (
-                digits[0] as u16 * 1000
-                    + digits[1] as u16 * 100
-                    + digits[2] as u16 * 10
-                    + digits[3] as u16,
-                digits[4] * 10 + digits[5],
-                digits[6] * 10 + digits[7],
-            ),
-            _ => return None,
-        };
-        return NaiveDate::from_ymd_opt(year as _, month as _, day as _);
-    }
-
-    let (month, day) = match mask {
-        0b1101101111 => {
-            if digits[7] != HYPHEN {
-                return None;
-            }
-            (digits[5] * 10 + digits[6], digits[8] * 10 + digits[9])
-        }
-        0b101101111 => {
-            if digits[7] != HYPHEN {
-                return None;
-            }
-            (digits[5] * 10 + digits[6], digits[8])
-        }
-        0b110101111 => {
-            if digits[6] != HYPHEN {
-                return None;
-            }
-            (digits[5], digits[7] * 10 + digits[8])
+    use regex::Regex;
+    let re = Regex::new(
+        
r"^(?:(\d{1,2})[/\-](\d{1,2})[/\-](\d{1,4})|(\d{1,4})[/\-](\d{1,2})[/\-](\d{1,2}))$",

Review Comment:
   [Are you sure you want `\d` 
here?](https://docs.rs/regex/latest/regex/#example-validating-a-particular-date-format)
 `\d` is Unicode-aware by default. So in addition to matching `3`, for example, 
it will also match `𝟛`.



-- 
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