[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r295064908 ## File path: libminifi/include/utils/StringUtils.h ## @@ -92,172 +103,104 @@ class StringUtils { * @returns modified string */ - static inline std::string trimRight(std::string s) { -s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(isspace))).base(), s.end()); + inline std::string trimRight(std::string s) { +s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(std::isspace))).base(), s.end()); return s; } + /** + * Trims a string left to right + * @param s incoming string + * @returns modified string + */ + inline std::string trim(std::string s) { +return trimRight(trimLeft(s)); + } + /** * Compares strings by lower casing them. */ - static inline bool equalsIgnoreCase(const std::string &left, const std::string right) { + inline bool equalsIgnoreCase(const std::string &left, const std::string right) { if (left.length() == right.length()) { - return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return tolower(lc) == tolower(rc);}); + return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return std::tolower(lc) == std::tolower(rc);}); } else { return false; } } - static std::vector split(const std::string &str, const std::string &delimiter) { -std::vector result; -auto curr = str.begin(); -auto end = str.end(); -auto is_func = [delimiter](int s) { - return delimiter.at(0) == s; -}; -while (curr != end) { - curr = std::find_if_not(curr, end, is_func); - if (curr == end) { -break; - } - auto next = std::find_if(curr, end, is_func); - result.push_back(std::string(curr, next)); - curr = next; -} - -return result; - } + std::vector split(const std::string &str, const std::string &delimiter); /** * Converts a string to a float * @param input input string * @param output output float * @param cp failure policy */ - static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) { -try { - output = std::stof(input); -} catch (const std::invalid_argument &ie) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ie; - } -} catch (const std::out_of_range &ofr) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ofr; + bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN); - } -} - -return true; + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string); - } - - static std::string replaceEnvironmentVariables(std::string& original_string) { -int32_t beg_seq = 0; -int32_t end_seq = 0; -std::string source_string = original_string; -do { - beg_seq = source_string.find("${", beg_seq); - if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { -beg_seq += 2; -continue; - } - if (beg_seq < 0) -break; - end_seq = source_string.find("}", beg_seq + 2); - if (end_seq < 0) -break; - if (end_seq - (beg_seq + 2) < 0) { -beg_seq += 2; -continue; - } - const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); - const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); - if (env_field.empty()) { -continue; - } - const auto strVal = std::getenv(env_field.c_str()); - std::string env_value; - if (strVal != nullptr) -env_value = strVal; - source_string = replaceAll(source_string, env_field_wrapped, env_value); - beg_seq = 0; // restart -} while (beg_seq >= 0); - -source_string = replaceAll(source_string, "\\$", "$"); - -return source_string; - } + std::string replaceMap(std::string source_string, const std::map &replace_map); - static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { -std::size_t loc = 0; -std::size_t lastFound; -while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { - source_string.replace(lastFound, from_string.size(), to_string); - loc = lastFound + to_string.size(); -} -return source_string; - } + std::string replaceEnvironmentVariables(const std::string& original_string); - inline static bool
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r292969566 ## File path: libminifi/include/utils/StringUtils.h ## @@ -92,172 +103,104 @@ class StringUtils { * @returns modified string */ - static inline std::string trimRight(std::string s) { -s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(isspace))).base(), s.end()); + inline std::string trimRight(std::string s) { +s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(std::isspace))).base(), s.end()); return s; } + /** + * Trims a string left to right + * @param s incoming string + * @returns modified string + */ + inline std::string trim(std::string s) { +return trimRight(trimLeft(s)); + } + /** * Compares strings by lower casing them. */ - static inline bool equalsIgnoreCase(const std::string &left, const std::string right) { + inline bool equalsIgnoreCase(const std::string &left, const std::string right) { if (left.length() == right.length()) { - return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return tolower(lc) == tolower(rc);}); + return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return std::tolower(lc) == std::tolower(rc);}); } else { return false; } } - static std::vector split(const std::string &str, const std::string &delimiter) { -std::vector result; -auto curr = str.begin(); -auto end = str.end(); -auto is_func = [delimiter](int s) { - return delimiter.at(0) == s; -}; -while (curr != end) { - curr = std::find_if_not(curr, end, is_func); - if (curr == end) { -break; - } - auto next = std::find_if(curr, end, is_func); - result.push_back(std::string(curr, next)); - curr = next; -} - -return result; - } + std::vector split(const std::string &str, const std::string &delimiter); /** * Converts a string to a float * @param input input string * @param output output float * @param cp failure policy */ - static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) { -try { - output = std::stof(input); -} catch (const std::invalid_argument &ie) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ie; - } -} catch (const std::out_of_range &ofr) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ofr; + bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN); - } -} - -return true; + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string); - } - - static std::string replaceEnvironmentVariables(std::string& original_string) { -int32_t beg_seq = 0; -int32_t end_seq = 0; -std::string source_string = original_string; -do { - beg_seq = source_string.find("${", beg_seq); - if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { -beg_seq += 2; -continue; - } - if (beg_seq < 0) -break; - end_seq = source_string.find("}", beg_seq + 2); - if (end_seq < 0) -break; - if (end_seq - (beg_seq + 2) < 0) { -beg_seq += 2; -continue; - } - const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); - const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); - if (env_field.empty()) { -continue; - } - const auto strVal = std::getenv(env_field.c_str()); - std::string env_value; - if (strVal != nullptr) -env_value = strVal; - source_string = replaceAll(source_string, env_field_wrapped, env_value); - beg_seq = 0; // restart -} while (beg_seq >= 0); - -source_string = replaceAll(source_string, "\\$", "$"); - -return source_string; - } + std::string replaceMap(std::string source_string, const std::map &replace_map); - static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { -std::size_t loc = 0; -std::size_t lastFound; -while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { - source_string.replace(lastFound, from_string.size(), to_string); - loc = lastFound + to_string.size(); -} -return source_string; - } + std::string replaceEnvironmentVariables(const std::string& original_string); - inline static bool
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r292969566 ## File path: libminifi/include/utils/StringUtils.h ## @@ -92,172 +103,104 @@ class StringUtils { * @returns modified string */ - static inline std::string trimRight(std::string s) { -s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(isspace))).base(), s.end()); + inline std::string trimRight(std::string s) { +s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(std::isspace))).base(), s.end()); return s; } + /** + * Trims a string left to right + * @param s incoming string + * @returns modified string + */ + inline std::string trim(std::string s) { +return trimRight(trimLeft(s)); + } + /** * Compares strings by lower casing them. */ - static inline bool equalsIgnoreCase(const std::string &left, const std::string right) { + inline bool equalsIgnoreCase(const std::string &left, const std::string right) { if (left.length() == right.length()) { - return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return tolower(lc) == tolower(rc);}); + return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return std::tolower(lc) == std::tolower(rc);}); } else { return false; } } - static std::vector split(const std::string &str, const std::string &delimiter) { -std::vector result; -auto curr = str.begin(); -auto end = str.end(); -auto is_func = [delimiter](int s) { - return delimiter.at(0) == s; -}; -while (curr != end) { - curr = std::find_if_not(curr, end, is_func); - if (curr == end) { -break; - } - auto next = std::find_if(curr, end, is_func); - result.push_back(std::string(curr, next)); - curr = next; -} - -return result; - } + std::vector split(const std::string &str, const std::string &delimiter); /** * Converts a string to a float * @param input input string * @param output output float * @param cp failure policy */ - static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) { -try { - output = std::stof(input); -} catch (const std::invalid_argument &ie) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ie; - } -} catch (const std::out_of_range &ofr) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ofr; + bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN); - } -} - -return true; + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string); - } - - static std::string replaceEnvironmentVariables(std::string& original_string) { -int32_t beg_seq = 0; -int32_t end_seq = 0; -std::string source_string = original_string; -do { - beg_seq = source_string.find("${", beg_seq); - if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { -beg_seq += 2; -continue; - } - if (beg_seq < 0) -break; - end_seq = source_string.find("}", beg_seq + 2); - if (end_seq < 0) -break; - if (end_seq - (beg_seq + 2) < 0) { -beg_seq += 2; -continue; - } - const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); - const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); - if (env_field.empty()) { -continue; - } - const auto strVal = std::getenv(env_field.c_str()); - std::string env_value; - if (strVal != nullptr) -env_value = strVal; - source_string = replaceAll(source_string, env_field_wrapped, env_value); - beg_seq = 0; // restart -} while (beg_seq >= 0); - -source_string = replaceAll(source_string, "\\$", "$"); - -return source_string; - } + std::string replaceMap(std::string source_string, const std::map &replace_map); - static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { -std::size_t loc = 0; -std::size_t lastFound; -while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { - source_string.replace(lastFound, from_string.size(), to_string); - loc = lastFound + to_string.size(); -} -return source_string; - } + std::string replaceEnvironmentVariables(const std::string& original_string); - inline static bool
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r292969566 ## File path: libminifi/include/utils/StringUtils.h ## @@ -92,172 +103,104 @@ class StringUtils { * @returns modified string */ - static inline std::string trimRight(std::string s) { -s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(isspace))).base(), s.end()); + inline std::string trimRight(std::string s) { +s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(std::isspace))).base(), s.end()); return s; } + /** + * Trims a string left to right + * @param s incoming string + * @returns modified string + */ + inline std::string trim(std::string s) { +return trimRight(trimLeft(s)); + } + /** * Compares strings by lower casing them. */ - static inline bool equalsIgnoreCase(const std::string &left, const std::string right) { + inline bool equalsIgnoreCase(const std::string &left, const std::string right) { if (left.length() == right.length()) { - return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return tolower(lc) == tolower(rc);}); + return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return std::tolower(lc) == std::tolower(rc);}); } else { return false; } } - static std::vector split(const std::string &str, const std::string &delimiter) { -std::vector result; -auto curr = str.begin(); -auto end = str.end(); -auto is_func = [delimiter](int s) { - return delimiter.at(0) == s; -}; -while (curr != end) { - curr = std::find_if_not(curr, end, is_func); - if (curr == end) { -break; - } - auto next = std::find_if(curr, end, is_func); - result.push_back(std::string(curr, next)); - curr = next; -} - -return result; - } + std::vector split(const std::string &str, const std::string &delimiter); /** * Converts a string to a float * @param input input string * @param output output float * @param cp failure policy */ - static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) { -try { - output = std::stof(input); -} catch (const std::invalid_argument &ie) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ie; - } -} catch (const std::out_of_range &ofr) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ofr; + bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN); - } -} - -return true; + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string); - } - - static std::string replaceEnvironmentVariables(std::string& original_string) { -int32_t beg_seq = 0; -int32_t end_seq = 0; -std::string source_string = original_string; -do { - beg_seq = source_string.find("${", beg_seq); - if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { -beg_seq += 2; -continue; - } - if (beg_seq < 0) -break; - end_seq = source_string.find("}", beg_seq + 2); - if (end_seq < 0) -break; - if (end_seq - (beg_seq + 2) < 0) { -beg_seq += 2; -continue; - } - const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); - const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); - if (env_field.empty()) { -continue; - } - const auto strVal = std::getenv(env_field.c_str()); - std::string env_value; - if (strVal != nullptr) -env_value = strVal; - source_string = replaceAll(source_string, env_field_wrapped, env_value); - beg_seq = 0; // restart -} while (beg_seq >= 0); - -source_string = replaceAll(source_string, "\\$", "$"); - -return source_string; - } + std::string replaceMap(std::string source_string, const std::map &replace_map); - static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { -std::size_t loc = 0; -std::size_t lastFound; -while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { - source_string.replace(lastFound, from_string.size(), to_string); - loc = lastFound + to_string.size(); -} -return source_string; - } + std::string replaceEnvironmentVariables(const std::string& original_string); - inline static bool
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r292969566 ## File path: libminifi/include/utils/StringUtils.h ## @@ -92,172 +103,104 @@ class StringUtils { * @returns modified string */ - static inline std::string trimRight(std::string s) { -s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(isspace))).base(), s.end()); + inline std::string trimRight(std::string s) { +s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(std::isspace))).base(), s.end()); return s; } + /** + * Trims a string left to right + * @param s incoming string + * @returns modified string + */ + inline std::string trim(std::string s) { +return trimRight(trimLeft(s)); + } + /** * Compares strings by lower casing them. */ - static inline bool equalsIgnoreCase(const std::string &left, const std::string right) { + inline bool equalsIgnoreCase(const std::string &left, const std::string right) { if (left.length() == right.length()) { - return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return tolower(lc) == tolower(rc);}); + return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return std::tolower(lc) == std::tolower(rc);}); } else { return false; } } - static std::vector split(const std::string &str, const std::string &delimiter) { -std::vector result; -auto curr = str.begin(); -auto end = str.end(); -auto is_func = [delimiter](int s) { - return delimiter.at(0) == s; -}; -while (curr != end) { - curr = std::find_if_not(curr, end, is_func); - if (curr == end) { -break; - } - auto next = std::find_if(curr, end, is_func); - result.push_back(std::string(curr, next)); - curr = next; -} - -return result; - } + std::vector split(const std::string &str, const std::string &delimiter); /** * Converts a string to a float * @param input input string * @param output output float * @param cp failure policy */ - static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) { -try { - output = std::stof(input); -} catch (const std::invalid_argument &ie) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ie; - } -} catch (const std::out_of_range &ofr) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ofr; + bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN); - } -} - -return true; + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string); - } - - static std::string replaceEnvironmentVariables(std::string& original_string) { -int32_t beg_seq = 0; -int32_t end_seq = 0; -std::string source_string = original_string; -do { - beg_seq = source_string.find("${", beg_seq); - if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { -beg_seq += 2; -continue; - } - if (beg_seq < 0) -break; - end_seq = source_string.find("}", beg_seq + 2); - if (end_seq < 0) -break; - if (end_seq - (beg_seq + 2) < 0) { -beg_seq += 2; -continue; - } - const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); - const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); - if (env_field.empty()) { -continue; - } - const auto strVal = std::getenv(env_field.c_str()); - std::string env_value; - if (strVal != nullptr) -env_value = strVal; - source_string = replaceAll(source_string, env_field_wrapped, env_value); - beg_seq = 0; // restart -} while (beg_seq >= 0); - -source_string = replaceAll(source_string, "\\$", "$"); - -return source_string; - } + std::string replaceMap(std::string source_string, const std::map &replace_map); - static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { -std::size_t loc = 0; -std::size_t lastFound; -while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { - source_string.replace(lastFound, from_string.size(), to_string); - loc = lastFound + to_string.size(); -} -return source_string; - } + std::string replaceEnvironmentVariables(const std::string& original_string); - inline static bool
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r292937396 ## File path: libminifi/include/utils/StringUtils.h ## @@ -92,172 +103,104 @@ class StringUtils { * @returns modified string */ - static inline std::string trimRight(std::string s) { -s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(isspace))).base(), s.end()); + inline std::string trimRight(std::string s) { +s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(std::isspace))).base(), s.end()); return s; } + /** + * Trims a string left to right + * @param s incoming string + * @returns modified string + */ + inline std::string trim(std::string s) { +return trimRight(trimLeft(s)); + } + /** * Compares strings by lower casing them. */ - static inline bool equalsIgnoreCase(const std::string &left, const std::string right) { + inline bool equalsIgnoreCase(const std::string &left, const std::string right) { if (left.length() == right.length()) { - return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return tolower(lc) == tolower(rc);}); + return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return std::tolower(lc) == std::tolower(rc);}); } else { return false; } } - static std::vector split(const std::string &str, const std::string &delimiter) { -std::vector result; -auto curr = str.begin(); -auto end = str.end(); -auto is_func = [delimiter](int s) { - return delimiter.at(0) == s; -}; -while (curr != end) { - curr = std::find_if_not(curr, end, is_func); - if (curr == end) { -break; - } - auto next = std::find_if(curr, end, is_func); - result.push_back(std::string(curr, next)); - curr = next; -} - -return result; - } + std::vector split(const std::string &str, const std::string &delimiter); /** * Converts a string to a float * @param input input string * @param output output float * @param cp failure policy */ - static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) { -try { - output = std::stof(input); -} catch (const std::invalid_argument &ie) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ie; - } -} catch (const std::out_of_range &ofr) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ofr; + bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN); - } -} - -return true; + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string); - } - - static std::string replaceEnvironmentVariables(std::string& original_string) { -int32_t beg_seq = 0; -int32_t end_seq = 0; -std::string source_string = original_string; -do { - beg_seq = source_string.find("${", beg_seq); - if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { -beg_seq += 2; -continue; - } - if (beg_seq < 0) -break; - end_seq = source_string.find("}", beg_seq + 2); - if (end_seq < 0) -break; - if (end_seq - (beg_seq + 2) < 0) { -beg_seq += 2; -continue; - } - const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); - const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); - if (env_field.empty()) { -continue; - } - const auto strVal = std::getenv(env_field.c_str()); - std::string env_value; - if (strVal != nullptr) -env_value = strVal; - source_string = replaceAll(source_string, env_field_wrapped, env_value); - beg_seq = 0; // restart -} while (beg_seq >= 0); - -source_string = replaceAll(source_string, "\\$", "$"); - -return source_string; - } + std::string replaceMap(std::string source_string, const std::map &replace_map); - static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { -std::size_t loc = 0; -std::size_t lastFound; -while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { - source_string.replace(lastFound, from_string.size(), to_string); - loc = lastFound + to_string.size(); -} -return source_string; - } + std::string replaceEnvironmentVariables(const std::string& original_string); - inline static bool
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r292418283 ## File path: libminifi/include/utils/StringUtils.h ## @@ -92,172 +103,104 @@ class StringUtils { * @returns modified string */ - static inline std::string trimRight(std::string s) { -s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(isspace))).base(), s.end()); + inline std::string trimRight(std::string s) { +s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::pointer_to_unary_function(std::isspace))).base(), s.end()); return s; } + /** + * Trims a string left to right + * @param s incoming string + * @returns modified string + */ + inline std::string trim(std::string s) { +return trimRight(trimLeft(s)); + } + /** * Compares strings by lower casing them. */ - static inline bool equalsIgnoreCase(const std::string &left, const std::string right) { + inline bool equalsIgnoreCase(const std::string &left, const std::string right) { if (left.length() == right.length()) { - return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return tolower(lc) == tolower(rc);}); + return std::equal(right.begin(), right.end(), left.begin(), [](unsigned char lc, unsigned char rc) {return std::tolower(lc) == std::tolower(rc);}); } else { return false; } } - static std::vector split(const std::string &str, const std::string &delimiter) { -std::vector result; -auto curr = str.begin(); -auto end = str.end(); -auto is_func = [delimiter](int s) { - return delimiter.at(0) == s; -}; -while (curr != end) { - curr = std::find_if_not(curr, end, is_func); - if (curr == end) { -break; - } - auto next = std::find_if(curr, end, is_func); - result.push_back(std::string(curr, next)); - curr = next; -} - -return result; - } + std::vector split(const std::string &str, const std::string &delimiter); /** * Converts a string to a float * @param input input string * @param output output float * @param cp failure policy */ - static bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN) { -try { - output = std::stof(input); -} catch (const std::invalid_argument &ie) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ie; - } -} catch (const std::out_of_range &ofr) { - switch (cp) { -case RETURN: -case NOTHING: - return false; -case EXIT: - exit(1); -case EXCEPT: - throw ofr; + bool StringToFloat(std::string input, float &output, FailurePolicy cp = RETURN); - } -} - -return true; + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string); - } - - static std::string replaceEnvironmentVariables(std::string& original_string) { -int32_t beg_seq = 0; -int32_t end_seq = 0; -std::string source_string = original_string; -do { - beg_seq = source_string.find("${", beg_seq); - if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { -beg_seq += 2; -continue; - } - if (beg_seq < 0) -break; - end_seq = source_string.find("}", beg_seq + 2); - if (end_seq < 0) -break; - if (end_seq - (beg_seq + 2) < 0) { -beg_seq += 2; -continue; - } - const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); - const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); - if (env_field.empty()) { -continue; - } - const auto strVal = std::getenv(env_field.c_str()); - std::string env_value; - if (strVal != nullptr) -env_value = strVal; - source_string = replaceAll(source_string, env_field_wrapped, env_value); - beg_seq = 0; // restart -} while (beg_seq >= 0); - -source_string = replaceAll(source_string, "\\$", "$"); - -return source_string; - } + std::string replaceMap(std::string source_string, const std::map &replace_map); - static std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { -std::size_t loc = 0; -std::size_t lastFound; -while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { - source_string.replace(lastFound, from_string.size(), to_string); - loc = lastFound + to_string.size(); -} -return source_string; - } + std::string replaceEnvironmentVariables(const std::string& original_string); - inline static bool
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r291477891 ## File path: libminifi/test/unit/StringUtilsTests.cpp ## @@ -91,5 +95,19 @@ TEST_CASE("TestStringUtils::testEnv5", "[test split classname]") { std::string expected = "hello world "; - REQUIRE(expected == StringUtils::replaceEnvironmentVariables(test_string)); + REQUIRE(expected == replaceEnvironmentVariables(test_string)); +} + +TEST_CASE("TestStringUtils::testJoin", "[test string join]") { + std::set strings = {"3", "2", "1"}; + REQUIRE(join(",", strings) == "1,2,3"); Review comment: Good idea, thanks, will add! 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r291472638 ## File path: libminifi/test/unit/StringUtilsTests.cpp ## @@ -17,32 +17,36 @@ */ #include +#include +#include #include #include #include "../TestBase.h" #include "core/Core.h" #include "utils/StringUtils.h" -using org::apache::nifi::minifi::utils::StringUtils; +using org::apache::nifi::minifi::utils::StringUtils::split; +using org::apache::nifi::minifi::utils::StringUtils::replaceEnvironmentVariables; +using org::apache::nifi::minifi::utils::StringUtils::join; TEST_CASE("TestStringUtils::split", "[test split no delimiter]") { std::vector expected = { "hello" }; - REQUIRE(expected == StringUtils::split("hello", ",")); + REQUIRE(expected == split("hello", ",")); } TEST_CASE("TestStringUtils::split2", "[test split single delimiter]") { std::vector expected = { "hello", "world" }; - REQUIRE(expected == StringUtils::split("hello world", " ")); + REQUIRE(expected == split("hello world", " ")); } TEST_CASE("TestStringUtils::split3", "[test split multiple delimiter]") { std::vector expected = { "hello", "world", "I'm", "a", "unit", "test" }; - REQUIRE(expected == StringUtils::split("hello world I'm a unit test", " ")); + REQUIRE(expected == split("hello world I'm a unit test", " ")); } TEST_CASE("TestStringUtils::split4", "[test split classname]") { std::vector expected = { "org", "apache", "nifi", "minifi", "utils", "StringUtils" }; - REQUIRE(expected == StringUtils::split(org::apache::nifi::minifi::core::getClassName(), "::")); + REQUIRE(expected == split("org::apache::nifi::minifi::utils::StringUtils", "::")); Review comment: That's true, but it basically it wasn't a nice idea to add a bunch of util function to a class instead of a namespace. I agree that this is a useful test, I will find another class and use split on the name of that. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r291472638 ## File path: libminifi/test/unit/StringUtilsTests.cpp ## @@ -17,32 +17,36 @@ */ #include +#include +#include #include #include #include "../TestBase.h" #include "core/Core.h" #include "utils/StringUtils.h" -using org::apache::nifi::minifi::utils::StringUtils; +using org::apache::nifi::minifi::utils::StringUtils::split; +using org::apache::nifi::minifi::utils::StringUtils::replaceEnvironmentVariables; +using org::apache::nifi::minifi::utils::StringUtils::join; TEST_CASE("TestStringUtils::split", "[test split no delimiter]") { std::vector expected = { "hello" }; - REQUIRE(expected == StringUtils::split("hello", ",")); + REQUIRE(expected == split("hello", ",")); } TEST_CASE("TestStringUtils::split2", "[test split single delimiter]") { std::vector expected = { "hello", "world" }; - REQUIRE(expected == StringUtils::split("hello world", " ")); + REQUIRE(expected == split("hello world", " ")); } TEST_CASE("TestStringUtils::split3", "[test split multiple delimiter]") { std::vector expected = { "hello", "world", "I'm", "a", "unit", "test" }; - REQUIRE(expected == StringUtils::split("hello world I'm a unit test", " ")); + REQUIRE(expected == split("hello world I'm a unit test", " ")); } TEST_CASE("TestStringUtils::split4", "[test split classname]") { std::vector expected = { "org", "apache", "nifi", "minifi", "utils", "StringUtils" }; - REQUIRE(expected == StringUtils::split(org::apache::nifi::minifi::core::getClassName(), "::")); + REQUIRE(expected == split("org::apache::nifi::minifi::utils::StringUtils", "::")); Review comment: That's true, but basically it wasn't a nice idea to add a bunch of util function to a class instead of a namespace. I agree that this is a useful test, I will find another class and use split on the name of that. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r291472089 ## File path: libminifi/src/utils/StringUtils.cpp ## @@ -0,0 +1,162 @@ +/** + * 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/StringUtils.h" + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace utils { +namespace StringUtils { + + std::string& replaceAll(std::string& source_string, const std::string &from_string, const std::string &to_string) { // NOLINT +std::size_t loc = 0; +std::size_t lastFound; +while ((lastFound = source_string.find(from_string, loc)) != std::string::npos) { + source_string.replace(lastFound, from_string.size(), to_string); + loc = lastFound + to_string.size(); +} +return source_string; + } + + std::string replaceEnvironmentVariables(const std::string& original_string) { +int32_t beg_seq = 0; +int32_t end_seq = 0; +std::string source_string = original_string; +do { + beg_seq = source_string.find("${", beg_seq); + if (beg_seq > 0 && source_string.at(beg_seq - 1) == '\\') { +beg_seq += 2; +continue; + } + if (beg_seq < 0) +break; + end_seq = source_string.find("}", beg_seq + 2); + if (end_seq < 0) +break; + if (end_seq - (beg_seq + 2) < 0) { +beg_seq += 2; +continue; + } + const std::string env_field = source_string.substr(beg_seq + 2, end_seq - (beg_seq + 2)); + const std::string env_field_wrapped = source_string.substr(beg_seq, end_seq + 1); + if (env_field.empty()) { +continue; + } + const auto strVal = std::getenv(env_field.c_str()); + std::string env_value; + if (strVal != nullptr) +env_value = strVal; + source_string = replaceAll(source_string, env_field_wrapped, env_value); + beg_seq = 0; // restart +} while (beg_seq >= 0); + +source_string = replaceAll(source_string, "\\$", "$"); + +return source_string; + } + + std::string replaceMap(std::string source_string, const std::map &replace_map) { +auto result_string = source_string; + +std::vector>> replacements; +for (const auto &replace_pair : replace_map) { + size_t replace_pos = 0; + while ((replace_pos = source_string.find(replace_pair.first, replace_pos)) != std::string::npos) { +replacements.emplace_back(std::make_pair(replace_pos, std::make_pair(replace_pair.first.length(), replace_pair.second))); +replace_pos += replace_pair.first.length(); + } +} + +std::sort(replacements.begin(), replacements.end(), [](const std::pair> a, + const std::pair> &b) { + return a.first > b.first; +}); + +for (const auto &replacement : replacements) { + result_string = source_string.replace(replacement.first, replacement.second.first, replacement.second.second); +} + +return result_string; + } + + bool StringToFloat(std::string input, float &output, FailurePolicy cp) { // NOLINT Review comment: Yes it was, just in the header, where linter didn't catch this. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability
arpadboda commented on a change in pull request #585: MINIFICPP-910 - Extend StringUtils with string join capability URL: https://github.com/apache/nifi-minifi-cpp/pull/585#discussion_r291381791 ## File path: libminifi/src/utils/StringUtils.cpp ## @@ -0,0 +1,162 @@ +/** Review comment: The content of this file is mostly moved from the header, the changes I made were to make linter happy. Some of these functions still leave space for improvement. 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: us...@infra.apache.org With regards, Apache Git Services