diff --git a/str_util.cpp b/str_util.cpp index 9f9cff2..daf79de 100644 --- a/str_util.cpp +++ b/str_util.cpp @@ -30,22 +30,33 @@ std::string str_util::str_replace(std::string input, std::string pattern, std::s return input; } -std::list str_util::str_split(std::string input, std::string pattern, size_t max) { +std::list str_util::str_split(std::string input, std::string pattern, size_t max, bool break_quote) { if (max == 0) return {}; std::list li; auto loc = input.find(pattern, 0); auto last_loc = 0; + auto pre_loc = 0; auto len = pattern.length(); while (loc != -1 && li.size() < max) { - li.push_back(input.substr(last_loc, loc - last_loc)); + auto s = input.substr(pre_loc, loc - pre_loc); + if (break_quote || (s.front() != '"' && s.front() != '\'') || s.back() == s.front()) { + li.push_back(s); + pre_loc = loc + len; + } last_loc = loc + len; if (last_loc < input.length()) loc = input.find(pattern, max(0, last_loc)); else break; } - if (last_loc <= input.length()) li.push_back(input.substr(last_loc, input.length() - last_loc)); + if (last_loc <= input.length()) li.push_back(input.substr(pre_loc, input.length() - pre_loc)); return li; } +std::vector str_util::str_splitv(std::string input, std::string pattern, size_t max, bool break_quote) { + auto list = str_split(input, pattern, max, break_quote); + std::vector vec(list.begin(), list.end()); + return vec; +} + std::string str_util::str_hex(std::string input) { if (input.empty()) return ""; const char* t = "0123456789abcdef"; @@ -65,3 +76,14 @@ bool str_util::str_endswith(std::string input, std::string pattern) { auto i = input.rfind(pattern); return i == ilen - plen; } + +std::string str_util::remove_quote(std::string input) { + if (input.length() < 2) return input; + if (input[0] == '"' && input[input.length() - 1] == '"') { + return input.substr(1, input.length() - 2); + } else if (input[0] == '\'' && input[input.length() - 1] == '\'') { + return input.substr(1, input.length() - 2); + } else { + return input; + } +} diff --git a/str_util.h b/str_util.h index f8d4895..48529e2 100644 --- a/str_util.h +++ b/str_util.h @@ -3,6 +3,7 @@ #include #include #include +#include namespace str_util { /** * @brief Convert string to lowercase @@ -24,9 +25,19 @@ namespace str_util { * @param input Input string * @param pattern Partten * @param max Maximum count of result. + * @param break_quote Break quote. * @return Result. */ - std::list str_split(std::string input, std::string pattern, size_t max = -1); + std::list str_split(std::string input, std::string pattern, size_t max = -1, bool break_quote = true); + /** + * @brief Split string with pattern. + * @param input Input string + * @param pattern Partten + * @param max Maximum count of result. + * @param break_quote Break quote. + * @return Result. + */ + std::vector str_splitv(std::string input, std::string pattern, size_t max = -1, bool break_quote = true); /** * @brief Convert data to a lowercase hexadecimal string * @param input Input data @@ -40,5 +51,11 @@ namespace str_util { * @return true if input ends with pattern */ bool str_endswith(std::string input, std::string pattern); + /** + * @brief Remove quote from a string + * @param input Input string + * @return Result + */ + std::string remove_quote(std::string input); } #endif