The caller owns the returned regex. Returns NULL if the pattern could not be compiled.
| 975 | |
| 976 | // The caller owns the returned regex. Returns NULL if the pattern could not be compiled. |
| 977 | re2::RE2* CompileRegex(const StringVal& pattern, string* error_str, |
| 978 | const StringVal& match_parameter) { |
| 979 | DCHECK(error_str != NULL); |
| 980 | re2::StringPiece pattern_sp(reinterpret_cast<char*>(pattern.ptr), pattern.len); |
| 981 | re2::RE2::Options options; |
| 982 | // Disable error logging in case e.g. every row causes an error |
| 983 | options.set_log_errors(false); |
| 984 | // Return the leftmost longest match (rather than the first match). |
| 985 | options.set_longest_match(true); |
| 986 | // Set the maximum memory used by re2's regex engine for storage |
| 987 | StringFunctions::SetRE2MemOpt(&options); |
| 988 | if (!match_parameter.is_null && |
| 989 | !StringFunctions::SetRE2Options(match_parameter, error_str, &options)) { |
| 990 | return NULL; |
| 991 | } |
| 992 | re2::RE2* re = new re2::RE2(pattern_sp, options); |
| 993 | if (!re->ok()) { |
| 994 | stringstream ss; |
| 995 | ss << "Could not compile regexp pattern: " << AnyValUtil::ToString(pattern) << endl |
| 996 | << "Error: " << re->error(); |
| 997 | *error_str = ss.str(); |
| 998 | delete re; |
| 999 | return NULL; |
| 1000 | } |
| 1001 | return re; |
| 1002 | } |
| 1003 | |
| 1004 | // This function sets options in the RE2 library before pattern matching. |
| 1005 | bool StringFunctions::SetRE2Options(const StringVal& match_parameter, |
no test coverage detected