| 1 |
#ifndef CRASH_STRINGS |
|---|
| 2 |
#define CRASH_STRINGS |
|---|
| 3 |
|
|---|
| 4 |
#include <cstdio> |
|---|
| 5 |
#include <cstring> |
|---|
| 6 |
#include <string> |
|---|
| 7 |
#include <cerrno> |
|---|
| 8 |
#include <iostream> |
|---|
| 9 |
#include <vector> |
|---|
| 10 |
#include <sstream> |
|---|
| 11 |
#include <stdexcept> |
|---|
| 12 |
|
|---|
| 13 |
using namespace std; |
|---|
| 14 |
|
|---|
| 15 |
namespace str { |
|---|
| 16 |
|
|---|
| 17 |
string join(string const &delim, vector<string> const &components); |
|---|
| 18 |
|
|---|
| 19 |
vector<string> split(string const &delim, string const &text); |
|---|
| 20 |
|
|---|
| 21 |
string replace(string const &match, string const &repl, string const &in); |
|---|
| 22 |
|
|---|
| 23 |
inline int count(string const &in, string const &delim = " \t\n\r") { |
|---|
| 24 |
int count = 1; |
|---|
| 25 |
|
|---|
| 26 |
for (unsigned i = 0; i < in.size(); i++) |
|---|
| 27 |
if (delim.find(in[i]) != string::npos) { |
|---|
| 28 |
count++; |
|---|
| 29 |
i += delim.size() - 1; |
|---|
| 30 |
} |
|---|
| 31 |
return count; |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
inline string dirname(string const &filename) { |
|---|
| 35 |
int slash = filename.rfind('/'); |
|---|
| 36 |
|
|---|
| 37 |
if (slash == -1) return "."; |
|---|
| 38 |
return filename.substr(0, slash); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
string exec(string const &command); |
|---|
| 42 |
|
|---|
| 43 |
string ucfirst(string const &str); |
|---|
| 44 |
|
|---|
| 45 |
inline string reverse(string const &str) { |
|---|
| 46 |
string out; |
|---|
| 47 |
|
|---|
| 48 |
for (string::const_reverse_iterator i = str.rbegin(); i != str.rend(); i++) |
|---|
| 49 |
out += *i; |
|---|
| 50 |
return out; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
inline string uppercase(string const &str) { |
|---|
| 54 |
string out; |
|---|
| 55 |
|
|---|
| 56 |
for (string::const_iterator i = str.begin(); i != str.end(); i++) |
|---|
| 57 |
out += toupper(*i); |
|---|
| 58 |
return out; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
inline string lowercase(string const &str) { |
|---|
| 62 |
string out; |
|---|
| 63 |
|
|---|
| 64 |
for (string::const_iterator i = str.begin(); i != str.end(); i++) |
|---|
| 65 |
out += tolower(*i); |
|---|
| 66 |
return out; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
inline string invertcase(string const &str) { |
|---|
| 70 |
string out; |
|---|
| 71 |
|
|---|
| 72 |
for (string::const_iterator i = str.begin(); i != str.end(); i++) |
|---|
| 73 |
out += islower(*i) ? toupper(*i) : tolower(*i); |
|---|
| 74 |
return out; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
inline string ltrim(string const &str) { |
|---|
| 78 |
for (unsigned i = 0; i < str.size(); i++) |
|---|
| 79 |
if (!isspace(str[i])) |
|---|
| 80 |
return str.substr(i); |
|---|
| 81 |
return ""; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
inline string rtrim(string const &str) { |
|---|
| 85 |
for (int i = str.size() - 1; i >= 0; i--) |
|---|
| 86 |
if (!isspace(str[i])) |
|---|
| 87 |
return str.substr(0, i + 1); |
|---|
| 88 |
return ""; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
inline string trim(string const &str) { |
|---|
| 92 |
return rtrim(ltrim(str)); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
/// Convert all HTML-able characters. ie. >, < and & |
|---|
| 96 |
string htmlify(string const &str); |
|---|
| 97 |
|
|---|
| 98 |
/// Convert HTML digraphs to their character forms. |
|---|
| 99 |
string unhtmlify(string const &str); |
|---|
| 100 |
|
|---|
| 101 |
/// Convert all C escapable characters to their escaped form. |
|---|
| 102 |
string addcslashes(string const &str); |
|---|
| 103 |
|
|---|
| 104 |
/// Convert escaped characters to their original forms. |
|---|
| 105 |
string stripcslashes(string const &str); |
|---|
| 106 |
|
|---|
| 107 |
void wraptext(ostream &out, string const &in, unsigned indent = 0, unsigned initialindent = 0, unsigned width = 80); |
|---|
| 108 |
|
|---|
| 109 |
inline string basename(string const &filename) { |
|---|
| 110 |
return filename.substr(filename.rfind('/') + 1); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
/** |
|---|
| 114 |
Convert a type to a string. A simple, but convenient, wrapper around |
|---|
| 115 |
ostrstream. If a type has a ostream << operator associated with it, |
|---|
| 116 |
this function will work. |
|---|
| 117 |
|
|---|
| 118 |
@parameter var Variable to convert. |
|---|
| 119 |
*/ |
|---|
| 120 |
template <typename T> string stringify(T const &t) { |
|---|
| 121 |
ostringstream os; |
|---|
| 122 |
os << t; |
|---|
| 123 |
return os.str(); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
/** |
|---|
| 127 |
Convert a string to a type. Requires that the type have an istream >> |
|---|
| 128 |
operator assoicated with it. |
|---|
| 129 |
|
|---|
| 130 |
eg. |
|---|
| 131 |
string s = "10"; |
|---|
| 132 |
int i; |
|---|
| 133 |
i = destringify<int>(s); |
|---|
| 134 |
|
|---|
| 135 |
@parameter str Strings to convert to type T. |
|---|
| 136 |
*/ |
|---|
| 137 |
template <typename T> T destringify(string const &str) { |
|---|
| 138 |
istringstream os(str); |
|---|
| 139 |
T t; |
|---|
| 140 |
|
|---|
| 141 |
os >> t; |
|---|
| 142 |
if (!os.good() && !os.eof()) throw runtime_error("can't destringify '" + str + "'"); |
|---|
| 143 |
return t; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
} |
|---|
| 147 |
#endif |
|---|