| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#ifndef OVStringHelper_h |
| 33 |
#define OVStringHelper_h |
| 34 |
|
| 35 |
#include <string> |
| 36 |
|
| 37 |
namespace OpenVanilla { |
| 38 |
using namespace std; |
| 39 |
|
| 40 |
class OVStringHelper { |
| 41 |
public: |
| 42 |
static const vector<string> SplitBySpacesOrTabsWithDoubleQuoteSupport(const string& text) |
| 43 |
{ |
| 44 |
vector<string> result; |
| 45 |
size_t index = 0, last = 0, length = text.length(); |
| 46 |
while (index < length) { |
| 47 |
if (text[index] == '\"') { |
| 48 |
index++; |
| 49 |
string tmp; |
| 50 |
while (index < length) { |
| 51 |
if (text[index] == '\"') { |
| 52 |
index++; |
| 53 |
break; |
| 54 |
} |
| 55 |
|
| 56 |
if (text[index] == '\\' && index + 1 < length) { |
| 57 |
index++; |
| 58 |
char c = text[index]; |
| 59 |
switch (c) { |
| 60 |
case 'r': |
| 61 |
tmp += '\r'; |
| 62 |
break; |
| 63 |
case 'n': |
| 64 |
tmp += '\n'; |
| 65 |
break; |
| 66 |
case '\"': |
| 67 |
tmp += '\"'; |
| 68 |
break; |
| 69 |
case '\\': |
| 70 |
tmp += '\\'; |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
else { |
| 75 |
tmp += text[index]; |
| 76 |
} |
| 77 |
|
| 78 |
index++; |
| 79 |
} |
| 80 |
result.push_back(tmp); |
| 81 |
} |
| 82 |
|
| 83 |
if (text[index] != ' ' && text[index] != '\t') { |
| 84 |
last = index; |
| 85 |
while (index < length) { |
| 86 |
if (text[index] == ' ' || text[index] == '\t') { |
| 87 |
if (index - last) |
| 88 |
result.push_back(text.substr(last, index - last)); |
| 89 |
break; |
| 90 |
} |
| 91 |
index++; |
| 92 |
} |
| 93 |
|
| 94 |
if (index == length && index - last) |
| 95 |
result.push_back(text.substr(last, index - last)); |
| 96 |
} |
| 97 |
|
| 98 |
index++; |
| 99 |
} |
| 100 |
|
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
static const vector<string> SplitBySpacesOrTabs(const string& text) |
| 105 |
{ |
| 106 |
vector<string> result; |
| 107 |
size_t index = 0, last = 0, length = text.length(); |
| 108 |
while (index < length) { |
| 109 |
if (text[index] != ' ' && text[index] != '\t') { |
| 110 |
last = index; |
| 111 |
while (index < length) { |
| 112 |
if (text[index] == ' ' || text[index] == '\t') { |
| 113 |
if (index - last) |
| 114 |
result.push_back(text.substr(last, index - last)); |
| 115 |
break; |
| 116 |
} |
| 117 |
index++; |
| 118 |
} |
| 119 |
|
| 120 |
if (index == length && index - last) |
| 121 |
result.push_back(text.substr(last, index - last)); |
| 122 |
} |
| 123 |
|
| 124 |
index++; |
| 125 |
} |
| 126 |
|
| 127 |
return result; |
| 128 |
} |
| 129 |
|
| 130 |
static const vector<string> Split(const string& text, char c) |
| 131 |
{ |
| 132 |
vector<string> result; |
| 133 |
size_t index = 0, last = 0, length = text.length(); |
| 134 |
while (index < length) { |
| 135 |
last = index; |
| 136 |
while (index < length) { |
| 137 |
if (text[index] == c) { |
| 138 |
result.push_back(text.substr(last, index - last)); |
| 139 |
break; |
| 140 |
} |
| 141 |
index++; |
| 142 |
|
| 143 |
if (index == length && index - last) |
| 144 |
result.push_back(text.substr(last, index - last)); |
| 145 |
} |
| 146 |
|
| 147 |
index++; |
| 148 |
} |
| 149 |
|
| 150 |
return result; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
static const string StringByReplacingOccurrencesOfStringWithString(const string& source, const string& substr, const string& replacement) |
| 155 |
{ |
| 156 |
if (!substr.length()) |
| 157 |
return source; |
| 158 |
|
| 159 |
size_t pos; |
| 160 |
if ((pos = source.find(substr)) >= source.length()) |
| 161 |
return source; |
| 162 |
|
| 163 |
return source.substr(0, pos) + replacement + StringByReplacingOccurrencesOfStringWithString(source.substr(pos + substr.length()), substr, replacement); |
| 164 |
} |
| 165 |
|
| 166 |
static const string Join(const vector<string>& vec, const string& separator) |
| 167 |
{ |
| 168 |
return Join(vec.begin(), vec.end(), separator); |
| 169 |
} |
| 170 |
|
| 171 |
static const string Join(const vector<string>& vec, size_t from, size_t size, const string& separator) |
| 172 |
{ |
| 173 |
return Join(vec.begin() + from, vec.begin() + from + size, separator); |
| 174 |
} |
| 175 |
|
| 176 |
static const string Join(vector<string>::const_iterator begin, vector<string>::const_iterator end, const string& separator) |
| 177 |
{ |
| 178 |
string result; |
| 179 |
for (vector<string>::const_iterator iter = begin ; iter != end ; ) { |
| 180 |
result += *iter; |
| 181 |
if (++iter != end) |
| 182 |
result += separator; |
| 183 |
} |
| 184 |
return result; |
| 185 |
} |
| 186 |
}; |
| 187 |
}; |
| 188 |
|
| 189 |
#endif |