| 190 | }; |
| 191 | |
| 192 | std::string PcreRegex::compile() |
| 193 | { |
| 194 | if (mRe) |
| 195 | return "regular expression has already been compiled"; |
| 196 | |
| 197 | const char *pcreCompileErrorStr = nullptr; |
| 198 | int erroffset = 0; |
| 199 | pcre * const re = pcre_compile(mPattern.c_str(),0,&pcreCompileErrorStr,&erroffset,nullptr); |
| 200 | if (!re) { |
| 201 | if (pcreCompileErrorStr) |
| 202 | return pcreCompileErrorStr; |
| 203 | return "unknown error"; |
| 204 | } |
| 205 | |
| 206 | // Optimize the regex, but only if PCRE_CONFIG_JIT is available |
| 207 | #ifdef PCRE_CONFIG_JIT |
| 208 | const char *pcreStudyErrorStr = nullptr; |
| 209 | pcre_extra * const pcreExtra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr); |
| 210 | // pcre_study() returns NULL for both errors and when it can not optimize the regex. |
| 211 | // The last argument is how one checks for errors. |
| 212 | // It is NULL if everything works, and points to an error string otherwise. |
| 213 | if (pcreStudyErrorStr) { |
| 214 | // pcre_compile() worked, but pcre_study() returned an error. Free the resources allocated by pcre_compile(). |
| 215 | pcre_free(re); |
| 216 | return std::string(pcreStudyErrorStr) + " (pcre_study)"; |
| 217 | } |
| 218 | mExtra = pcreExtra; |
| 219 | #endif |
| 220 | |
| 221 | mRe = re; |
| 222 | |
| 223 | return ""; |
| 224 | } |
| 225 | |
| 226 | std::string PcreRegex::match(const std::string& str, const MatchFn& matchFn) const |
| 227 | { |
no outgoing calls
no test coverage detected