| 1 |
#include <davix_internal_config.hpp> |
| 2 |
#include "stringutils.hpp" |
| 3 |
#include <cstring> |
| 4 |
#include <algorithm> |
| 5 |
|
| 6 |
namespace StrUtil{ |
| 7 |
|
| 8 |
std::vector<std::string> tokenSplit(const std::string & str, const std::string & delimiter){ |
| 9 |
std::vector<std::string> res; |
| 10 |
std::string::const_iterator it_prev, it_cur; |
| 11 |
for(it_prev = it_cur = str.begin(); it_cur < str.end(); ((it_prev != str.end())?(it_prev++):(it_prev))){ |
| 12 |
it_cur = std::find_first_of(it_prev, str.end(), delimiter.begin(), delimiter.end()); |
| 13 |
if(it_prev != it_cur) |
| 14 |
res.push_back(std::string(it_prev,it_cur)); |
| 15 |
it_prev = it_cur; |
| 16 |
} |
| 17 |
return res; |
| 18 |
} |
| 19 |
|
| 20 |
std::string stringReplace(std::string str, const std::string & search, const std::string & replace) { |
| 21 |
size_t pos = 0; |
| 22 |
|
| 23 |
if (search.size() == 0) { return str; } |
| 24 |
|
| 25 |
while ((pos = str.find(search, pos)) != std::string::npos) { |
| 26 |
str.replace(pos, search.length(), replace); |
| 27 |
pos += replace.length(); |
| 28 |
} |
| 29 |
|
| 30 |
return str; |
| 31 |
} |
| 32 |
|
| 33 |
size_t findInsensitive(const std::string& str, const std::string& search) { |
| 34 |
auto it = std::search( |
| 35 |
str.begin(), str.end(), |
| 36 |
search.begin(), search.end(), |
| 37 |
[](unsigned char c1, unsigned c2) { |
| 38 |
return std::tolower(c1) == std::tolower(c2); |
| 39 |
} |
| 40 |
); |
| 41 |
|
| 42 |
if (it == str.end()) { |
| 43 |
return std::string::npos; |
| 44 |
} |
| 45 |
|
| 46 |
return std::distance(str.begin(), it); |
| 47 |
} |
| 48 |
|
| 49 |
int compare_ncase(const std::string & str1, const std::string & str2){ |
| 50 |
return strcasecmp(str1.c_str(), str2.c_str()); |
| 51 |
} |
| 52 |
|
| 53 |
int compare_ncase(const std::string &str1, const std::string &str2, size_t max){ |
| 54 |
return strncasecmp(str1.c_str(), str2.c_str(), max); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
int compare_ncase(const std::string & str1, off_t offset, size_t size, const char* cstr2){ |
| 60 |
return strncasecmp(str1.c_str(), cstr2+ offset, size); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
} |