| 1 |
#include <cstdio> |
|---|
| 2 |
#include "Strings.h" |
|---|
| 3 |
|
|---|
| 4 |
namespace str { |
|---|
| 5 |
|
|---|
| 6 |
void wraptext(ostream &out, string const &in, unsigned indent, unsigned initial, unsigned width) { |
|---|
| 7 |
char const *p = in.c_str(); |
|---|
| 8 |
int ind = indent - initial, |
|---|
| 9 |
wid = width - initial; |
|---|
| 10 |
|
|---|
| 11 |
if (initial < indent) wid = width - indent; |
|---|
| 12 |
|
|---|
| 13 |
while (*p) { |
|---|
| 14 |
int count; |
|---|
| 15 |
bool newlines = false; |
|---|
| 16 |
|
|---|
| 17 |
if (*p && ind > 0) { |
|---|
| 18 |
out << string(ind, ' '); |
|---|
| 19 |
ind = indent; |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
for (count = 0; p[count] != 0 && p[count] != '\n' && count < wid; count++) ; |
|---|
| 23 |
if (p[count] == '\n') newlines = true; |
|---|
| 24 |
// move back to last space seperation |
|---|
| 25 |
while (p[count] && !isspace(p[count]) && count) count--; |
|---|
| 26 |
if (count == 0) |
|---|
| 27 |
for (count = 0; p[count] != 0 && p[count] != '\n' && count < wid; count++) ; |
|---|
| 28 |
for (int i = 0; i < count; i++) out << *p++; |
|---|
| 29 |
// skip spaces |
|---|
| 30 |
while (*p != 0 && isspace(*p)) { |
|---|
| 31 |
if (*p == '\n') out << endl; |
|---|
| 32 |
p++; |
|---|
| 33 |
} |
|---|
| 34 |
if (*p && !newlines) out << endl; |
|---|
| 35 |
initial = 0; |
|---|
| 36 |
ind = indent; |
|---|
| 37 |
wid = width - ind; |
|---|
| 38 |
} |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
string ucfirst(string const &str) { |
|---|
| 42 |
string out; |
|---|
| 43 |
bool newword = true; |
|---|
| 44 |
|
|---|
| 45 |
for (string::const_iterator i = str.begin(); i != str.end(); i++) |
|---|
| 46 |
if (newword && isalpha(*i)) { |
|---|
| 47 |
out += toupper(*i); |
|---|
| 48 |
newword = false; |
|---|
| 49 |
} else { |
|---|
| 50 |
if (isspace(*i) || ispunct(*i)) { |
|---|
| 51 |
newword = true; |
|---|
| 52 |
} |
|---|
| 53 |
out += *i; |
|---|
| 54 |
} |
|---|
| 55 |
return out; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
string exec(string const &command) { |
|---|
| 59 |
FILE *pipe = popen(command.c_str(), "r"); |
|---|
| 60 |
char buffer[1024]; |
|---|
| 61 |
string out; |
|---|
| 62 |
|
|---|
| 63 |
if (!pipe) throw runtime_error("exec(): failed, " + string(strerror(errno))); |
|---|
| 64 |
while (fgets(buffer, 1024, pipe)) |
|---|
| 65 |
out += buffer; |
|---|
| 66 |
fclose(pipe); |
|---|
| 67 |
return out; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
string join(string const &delim, vector<string> const &components) { |
|---|
| 71 |
string out; |
|---|
| 72 |
vector<string>::const_iterator end = components.end(); |
|---|
| 73 |
|
|---|
| 74 |
end--; |
|---|
| 75 |
for (vector<string>::const_iterator i = components.begin(); i != components.end(); i++) { |
|---|
| 76 |
out += (*i); |
|---|
| 77 |
if (i != end) |
|---|
| 78 |
out += delim; |
|---|
| 79 |
} |
|---|
| 80 |
return out; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
vector<string> split(string const &delim, string const &text) { |
|---|
| 84 |
unsigned start = 0; |
|---|
| 85 |
vector<string> out; |
|---|
| 86 |
|
|---|
| 87 |
for (unsigned i = start; i < text.size(); i++) |
|---|
| 88 |
if (!strncmp(text.c_str() + i, delim.c_str(), delim.size())) { |
|---|
| 89 |
out.push_back(text.substr(start, i - start)); |
|---|
| 90 |
start = i + delim.size(); |
|---|
| 91 |
i += delim.size() - 1; |
|---|
| 92 |
} |
|---|
| 93 |
out.push_back(text.substr(start)); |
|---|
| 94 |
return out; |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
string replace(string const &match, string const &repl, string const &in) { |
|---|
| 98 |
string out; |
|---|
| 99 |
string::size_type found = 0, lastfound = 0; |
|---|
| 100 |
|
|---|
| 101 |
while ((found = in.find(match, found)) != string::npos) { |
|---|
| 102 |
out += in.substr(lastfound, found - lastfound); |
|---|
| 103 |
out += repl; |
|---|
| 104 |
found += match.size(); |
|---|
| 105 |
lastfound = found; |
|---|
| 106 |
} |
|---|
| 107 |
out += in.substr(lastfound); |
|---|
| 108 |
|
|---|
| 109 |
return out; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
string htmlify(string const &_str) { |
|---|
| 113 |
string out; |
|---|
| 114 |
char const *str = _str.c_str(); |
|---|
| 115 |
|
|---|
| 116 |
for (unsigned i = 0; i < _str.size(); i++) |
|---|
| 117 |
switch (str[i]) { |
|---|
| 118 |
case '<' : out += "<"; break; |
|---|
| 119 |
case '>' : out += ">"; break; |
|---|
| 120 |
case '&' : out += "&"; break; |
|---|
| 121 |
default : out += str[i]; break; |
|---|
| 122 |
} |
|---|
| 123 |
return out; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
string unhtmlify(string const &_str) { |
|---|
| 127 |
string out; |
|---|
| 128 |
char const *str = _str.c_str(); |
|---|
| 129 |
|
|---|
| 130 |
for (unsigned i = 0; i < _str.size(); i++) |
|---|
| 131 |
if (str[i] == '&') { |
|---|
| 132 |
++i; |
|---|
| 133 |
if (!strncmp(str + i, "amp;", 4)) { i += 3; out += '&'; } |
|---|
| 134 |
else if (!strncmp(str + i, "gt;", 3)) { i += 3; out += '>'; } |
|---|
| 135 |
else if (!strncmp(str + i, "lt;", 3)) { i += 3; out += '<'; } |
|---|
| 136 |
else { |
|---|
| 137 |
out += '&'; |
|---|
| 138 |
out += str[i]; |
|---|
| 139 |
} |
|---|
| 140 |
} else |
|---|
| 141 |
out += str[i]; |
|---|
| 142 |
return out; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
string addcslashes(string const &_str) { |
|---|
| 146 |
string out; |
|---|
| 147 |
char const *str = _str.c_str(); |
|---|
| 148 |
|
|---|
| 149 |
for (unsigned i = 0; i < _str.size(); ++i) |
|---|
| 150 |
switch (str[i]) { |
|---|
| 151 |
case '\n' : out += "\\n"; break; |
|---|
| 152 |
case '\r' : out += "\\r"; break; |
|---|
| 153 |
case '\t' : out += "\\t"; break; |
|---|
| 154 |
case '"' : out += "\\\""; break; |
|---|
| 155 |
case '\\' : out += "\\\\"; break; |
|---|
| 156 |
default : out += str[i]; break; |
|---|
| 157 |
} |
|---|
| 158 |
return out; |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
string stripcslashes(string const &_str) { |
|---|
| 162 |
string out; |
|---|
| 163 |
char const *str = _str.c_str(); |
|---|
| 164 |
|
|---|
| 165 |
for (unsigned i = 0; i < _str.size(); ++i) |
|---|
| 166 |
if (str[i] == '\\') { |
|---|
| 167 |
++i; |
|---|
| 168 |
switch (str[i]) { |
|---|
| 169 |
case 'n' : out += '\n'; break; |
|---|
| 170 |
case 'r' : out += '\r'; break; |
|---|
| 171 |
case 't' : out += '\t'; break; |
|---|
| 172 |
case '"' : out += '"'; break; |
|---|
| 173 |
case '\\' : out += '\\'; break; |
|---|
| 174 |
default : out += str[i]; break; |
|---|
| 175 |
} |
|---|
| 176 |
} else |
|---|
| 177 |
out += str[i]; |
|---|
| 178 |
return out; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
} |
|---|