diff --git a/re2/re2.cc b/re2/re2.cc index 91d9880f4d02aa56d848b80ca69d7c2769c439d0..3285dc0954eb303961c87dc49f4b162dbcd08639 100644 --- a/re2/re2.cc +++ b/re2/re2.cc @@ -976,47 +976,14 @@ namespace re2 bool RE2::CheckRewriteString(const StringPiece &rewrite, std::string *error) const { - int max_token = -1; - for (const char *s = rewrite.data(), *end = s + rewrite.size(); - s < end; s++) - { - int c = *s; - if (c != '\\') - { - continue; - } - if (++s == end) - { - *error = "Rewrite schema error: '\\' not allowed at end."; - return false; - } - c = *s; - if (c == '\\') - { - continue; - } - if (!isdigit(c)) - { - *error = "Rewrite schema error: " - "'\\' must be followed by a digit or '\\'."; - return false; - } - int n = (c - '0'); - if (max_token < n) - { - max_token = n; - } - } - - if (max_token > NumberOfCapturingGroups()) - { - // *error = StringPrintf( - // "Rewrite schema requests %d matches, but the regexp only has %d " - // "parenthesized subexpressions.", - // max_token, NumberOfCapturingGroups()); + int num_caps = NumberOfCapturingGroups(); + bool result = rure_check_rewrite_string(rewrite.data(), num_caps); + if(!result){ + *error = "Rewrite schema error"; return false; } - return true; + return true; + } // Returns the maximum submatch needed for the rewrite to be done by Replace(). diff --git a/regex-capi/include/rure.h b/regex-capi/include/rure.h index 67194866ae31bab1343f5179f1b316710b166d07..c8e2af5162d8f1d59afa1fb9e2f45d2a5cf1bd5e 100644 --- a/regex-capi/include/rure.h +++ b/regex-capi/include/rure.h @@ -611,6 +611,8 @@ const char *rure_replace_all(rure *re, const uint8_t *haystack, size_t len_h, rure *rure_new(const uint8_t *pattern, size_t length); bool rure_consume(rure *re, const uint8_t *haystack, size_t length, rure_match *match); int rure_max_submatch(const char *rewrite); +bool rure_check_rewrite_string(const char *rewrite, int max_token); + #ifdef __cplusplus } diff --git a/regex-capi/src/rure.rs b/regex-capi/src/rure.rs index b88ffc4bfb5886823dd9af9b6b672c8373fb2ed0..0636326f4d2c8869b054f756fecdcfd16331472d 100644 --- a/regex-capi/src/rure.rs +++ b/regex-capi/src/rure.rs @@ -747,3 +747,49 @@ ffi_fn! { max } } + +ffi_fn! { + fn rure_check_rewrite_string(rewrite: *const c_char, cap_num: i32) -> bool { + let len = unsafe { CStr::from_ptr(rewrite).to_bytes().len() }; + let pat = rewrite as *const u8; + let text = unsafe { slice::from_raw_parts(pat, len) }; + let s = std::str::from_utf8(text).unwrap(); + let mut max_token = -1; + let chars = s.chars().collect::>(); + let mut i = 0; + while i < chars.len() { + if chars[i] != '\\' { + i += 1; + continue; + } + i += 1; + + if i == chars.len() { + println!("Rewrite schema error: '\\' not allowed at end."); + return false; + } + // i += 1; + if chars[i] == '\\' { + i += 1; + continue; + } + if !chars[i].is_ascii_digit() { + println!("'\\' must be followed by a digit or '\\'."); + return false; + } + + let n = chars[i] as i32 - '0' as i32; + println!("n = {}", n); + i += 1; + + if n > max_token { + max_token = n; + } + } + + if max_token > cap_num { + return false; + } + return true; + } +}