@@ -188,15 +188,15 @@ namespace {
188188 std::string PcreRegex::compile ()
189189 {
190190 if (mRe )
191- return " pcre_compile failed: regular expression has already been compiled" ;
191+ return " regular expression has already been compiled" ;
192192
193193 const char *pcreCompileErrorStr = nullptr ;
194194 int erroffset = 0 ;
195195 pcre * const re = pcre_compile (mPattern .c_str (),0 ,&pcreCompileErrorStr,&erroffset,nullptr );
196196 if (!re) {
197197 if (pcreCompileErrorStr)
198- return " pcre_compile failed: " + std::string ( pcreCompileErrorStr) ;
199- return " pcre_compile failed: unknown error" ;
198+ return pcreCompileErrorStr;
199+ return " unknown error" ;
200200 }
201201
202202 // Optimize the regex, but only if PCRE_CONFIG_JIT is available
@@ -209,7 +209,7 @@ namespace {
209209 if (pcreStudyErrorStr) {
210210 // pcre_compile() worked, but pcre_study() returned an error. Free the resources allocated by pcre_compile().
211211 pcre_free (re);
212- return " pcre_study failed: " + std::string (pcreStudyErrorStr);
212+ return std::string (pcreStudyErrorStr) + " (pcre_study) " ;
213213 }
214214 mExtra = pcreExtra;
215215#endif
@@ -222,7 +222,7 @@ namespace {
222222 std::string PcreRegex::match (const std::string& str, const MatchFn& match) const
223223 {
224224 if (!mRe )
225- return " pcre_exec failed: regular expression has not been compiled yet" ;
225+ return " regular expression has not been compiled yet" ;
226226
227227 int pos = 0 ;
228228 int ovector[30 ]= {0 };
@@ -231,7 +231,7 @@ namespace {
231231 if (pcreExecRet == PCRE_ERROR_NOMATCH)
232232 return " " ;
233233 if (pcreExecRet < 0 ) {
234- return " pcre_exec failed (pos: " + std::to_string (pos) + " ): " + pcreErrorCodeToString (pcreExecRet) ;
234+ return pcreErrorCodeToString (pcreExecRet) + " (pos: " + std::to_string (pos) + " )" ;
235235 }
236236 const auto pos1 = static_cast <unsigned int >(ovector[0 ]);
237237 const auto pos2 = static_cast <unsigned int >(ovector[1 ]);
@@ -246,10 +246,22 @@ namespace {
246246 }
247247}
248248
249- std::shared_ptr<Regex> Regex::create (std::string pattern, std::string& err)
249+ template <typename T>
250+ static T* createAndCompileRegex (std::string pattern, std::string& err)
250251{
251- auto * regex = new PcreRegex (std::move (pattern));
252+ T * regex = new T (std::move (pattern));
252253 err = regex->compile ();
254+ return regex;
255+ }
256+
257+ std::shared_ptr<Regex> Regex::create (std::string pattern, Engine engine, std::string& err)
258+ {
259+ Regex* regex = nullptr ;
260+ if (engine == Engine::Pcre)
261+ regex = createAndCompileRegex<PcreRegex>(std::move (pattern), err);
262+ else {
263+ err = " unknown regular expression engine" ;
264+ }
253265 if (!err.empty ()) {
254266 delete regex;
255267 return nullptr ;
0 commit comments