mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-06-12 08:18:47 +08:00
add more functions
This commit is contained in:
28
str_util.cpp
28
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<std::string> str_util::str_split(std::string input, std::string pattern, size_t max) {
|
||||
std::list<std::string> str_util::str_split(std::string input, std::string pattern, size_t max, bool break_quote) {
|
||||
if (max == 0) return {};
|
||||
std::list<std::string> 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<std::string> 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<std::string> 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;
|
||||
}
|
||||
}
|
||||
|
||||
19
str_util.h
19
str_util.h
@@ -3,6 +3,7 @@
|
||||
#include <list>
|
||||
#include <stddef.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
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<std::string> str_split(std::string input, std::string pattern, size_t max = -1);
|
||||
std::list<std::string> 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<std::string> 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
|
||||
|
||||
Reference in New Issue
Block a user