| 1 |
#ifndef STRINGUTILS_HPP |
| 2 |
#define STRINGUTILS_HPP |
| 3 |
|
| 4 |
#include <vector> |
| 5 |
#include <string> |
| 6 |
#include <sstream> |
| 7 |
#include <algorithm> |
| 8 |
#include <functional> |
| 9 |
#include <cctype> |
| 10 |
#include <locale> |
| 11 |
#include <cstring> |
| 12 |
|
| 13 |
|
| 14 |
typedef std::vector<std::string> stringVec; |
| 15 |
|
| 16 |
namespace StrUtil{ |
| 17 |
|
| 18 |
|
| 19 |
struct isSlash{ |
| 20 |
|
| 21 |
bool operator()(char c) const{ |
| 22 |
return ( c == '/'); |
| 23 |
} |
| 24 |
|
| 25 |
typedef char argument_type; |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
struct isCrLf{ |
| 30 |
bool operator()(char c) const{ |
| 31 |
return (( c == '\n') || (c == '\r')); |
| 32 |
} |
| 33 |
|
| 34 |
typedef char argument_type; |
| 35 |
}; |
| 36 |
|
| 37 |
struct isSpace{ |
| 38 |
bool operator()(char c) const{ |
| 39 |
return ::isspace(c); |
| 40 |
} |
| 41 |
|
| 42 |
typedef char argument_type; |
| 43 |
|
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
struct isHexa{ |
| 48 |
bool operator()(char c) const{ |
| 49 |
return (isdigit(c) || (isalpha(c) && ( tolower(c) >= 'a' && tolower(c) <= 'f'))); |
| 50 |
} |
| 51 |
|
| 52 |
typedef char argument_type; |
| 53 |
}; |
| 54 |
|
| 55 |
// predicates |
| 56 |
inline bool charEqCase(char c1, char c2){ |
| 57 |
return (c1 == c2 || ::tolower(c1) == ::tolower(c2)); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
///////////////////////////// |
| 65 |
// Parsing |
| 66 |
///////////////////////////// |
| 67 |
//split a string following an array of delimiter ( strtok like ) |
| 68 |
std::vector<std::string> tokenSplit(const std::string & str, const std::string & delimiter); |
| 69 |
|
| 70 |
inline std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &res) { |
| 71 |
std::stringstream ss(s); |
| 72 |
std::string item; |
| 73 |
item.reserve(s.size()); |
| 74 |
while (std::getline(ss, item, delim)) { |
| 75 |
res.push_back(item); |
| 76 |
} |
| 77 |
return res; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
inline std::string & remove(std::string & str, char c){ |
| 82 |
std::string::iterator it = std::remove(str.begin(), str.end(), c); |
| 83 |
str.erase(it, str.end()); |
| 84 |
return str; |
| 85 |
} |
| 86 |
|
| 87 |
std::string stringReplace(std::string str, const std::string & search, const std::string & replace); |
| 88 |
|
| 89 |
// Case-insensitive find |
| 90 |
size_t findInsensitive(const std::string& str, const std::string& search); |
| 91 |
|
| 92 |
/////////////////////////// |
| 93 |
/// compare utils |
| 94 |
////////////////////// |
| 95 |
// compare two C++ string case insensitive |
| 96 |
int compare_ncase(const std::string & str1, const std::string & str2); |
| 97 |
|
| 98 |
int compare_ncase(const std::string &str1, const std::string &str2, size_t max); |
| 99 |
|
| 100 |
int compare_ncase(const std::string & str1, off_t offset, size_t size, const char* cstr2); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
inline size_t copy_std_string_to_buff(char* buffer, size_t max_size, const std::string & str){ |
| 105 |
const size_t str_size = str.copy(buffer, max_size-1); |
| 106 |
buffer[str_size]= '\0'; |
| 107 |
return str_size; |
| 108 |
} |
| 109 |
|
| 110 |
/////////////////////////////////// |
| 111 |
///// String utilities |
| 112 |
////////////////////////////////// |
| 113 |
|
| 114 |
// trim from start |
| 115 |
template <typename Func> |
| 116 |
inline std::string <rim(std::string &s, const Func & pred) { |
| 117 |
s.erase(s.begin(), static_cast<std::string::iterator>(std::find_if(s.begin(), s.end(), std::not1(pred)))); |
| 118 |
return s; |
| 119 |
} |
| 120 |
|
| 121 |
inline std::string <rim(std::string &s) { |
| 122 |
return ltrim(s, isSpace()); |
| 123 |
} |
| 124 |
|
| 125 |
// trim from end |
| 126 |
template <typename Func> |
| 127 |
inline std::string &rtrim(std::string &s, const Func & pred) { |
| 128 |
s.erase(static_cast<std::string::iterator>(std::find_if(s.rbegin(), s.rend(), std::not1(pred)).base()), s.end()); |
| 129 |
return s; |
| 130 |
} |
| 131 |
|
| 132 |
inline std::string &rtrim(std::string &s) { |
| 133 |
return rtrim(s, isSpace()); |
| 134 |
} |
| 135 |
|
| 136 |
// trim from both |
| 137 |
template <typename Func> |
| 138 |
inline std::string &trim(std::string &s, const Func & pred) { |
| 139 |
return ltrim<Func>(rtrim<Func>(s, pred), pred); |
| 140 |
} |
| 141 |
|
| 142 |
inline std::string &trim(std::string &s) { |
| 143 |
return ltrim(rtrim(s)); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
inline std::string & toLower(std::string & str){ |
| 148 |
std::transform(str.begin(), str.end(), str.begin(), ::tolower); |
| 149 |
return str; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
#endif // STRINGUTILS_HPP |