bakaid commented on a change in pull request #606: MINIFICPP-828: Regex Util URL: https://github.com/apache/nifi-minifi-cpp/pull/606#discussion_r300449390
########## File path: libminifi/src/utils/RegexUtils.cpp ########## @@ -0,0 +1,128 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/RegexUtils.h" +#include "Exception.h" +#include <iostream> +#include <vector> + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { + +RegexUtils::RegexUtils(const std::string &value, + const std::vector<RegexUtils::Mode> &mode) + : regexStr(value) { + // Create regex mode +#if (__cplusplus > 201103L) || defined(_WIN32) + regex_mode = std::regex_constants::ECMAScript; +#else + regex_mode = 0; +#endif + for (const auto m : mode) { + switch (m) { + case Mode::EXTENDED: +#if (__cplusplus > 201103L) || defined(_WIN32) + regex_mode |= std::regex_constants::extended; +#else + regex_mode |= REG_EXTENDED; +#endif + break; + case Mode::ICASE: +#if (__cplusplus > 201103L) || defined(_WIN32) + regex_mode |= std::regex_constants::icase; +#else + regex_mode |= REG_ICASE; +#endif + break; + } + } + // Compile +#if (__cplusplus > 201103L) || defined(_WIN32) + try { + compiledRegex = std::regex(regexStr, regex_mode); + } catch (const std::regex_error &e) { + throw e.what(); + } +#else + int err_code = regcomp(&compiledRegex, regexStr.c_str(), regex_mode); + if (err_code) { + char msg[100]; + regerror(err_code, &compiledRegex, msg, sizeof(msg)); + throw std::string(msg); + } + int maxGroups = std::count(regexStr.begin(), regexStr.end(), '(') + 1; + matches.resize(maxGroups); +#endif +} + +RegexUtils::~RegexUtils() { +#if (__cplusplus > 201103L) || defined(_WIN32) + +#else + regfree(&compiledRegex); +#endif +} + +bool RegexUtils::canMatch(const std::string &pattern) { + results.clear(); + if (pat == pattern) { Review comment: I think it is a possible and reasonable usage of this class to call it with the same string multiple times. In this case it can be a good optimization not to rematch the string, but then we should return with the result of the previous match. With the current implementation subsequent matches of the same string will be unsuccessful. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
