aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/StringUtil.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/StringUtil.cc')
-rw-r--r--src/FbTk/StringUtil.cc55
1 files changed, 28 insertions, 27 deletions
diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc
index 3066615..1c1597a 100644
--- a/src/FbTk/StringUtil.cc
+++ b/src/FbTk/StringUtil.cc
@@ -50,7 +50,8 @@
50#include <algorithm> 50#include <algorithm>
51#include <string> 51#include <string>
52 52
53using namespace std; 53using std::string;
54using std::transform;
54 55
55namespace FbTk { 56namespace FbTk {
56 57
@@ -92,11 +93,11 @@ const char *strcasestr(const char *str, const char *ptn) {
92 if ~ then expand it to home of user 93 if ~ then expand it to home of user
93 returns expanded filename 94 returns expanded filename
94*/ 95*/
95string expandFilename(const std::string &filename) { 96string expandFilename(const string &filename) {
96 string retval; 97 string retval;
97 size_t pos = filename.find_first_not_of(" \t"); 98 size_t pos = filename.find_first_not_of(" \t");
98 if (pos != std::string::npos && filename[pos] == '~') { 99 if (pos != string::npos && filename[pos] == '~') {
99 retval = getenv("HOME"); 100 retval = getenv("HOME");
100 if (pos != filename.size()) { 101 if (pos != filename.size()) {
101 // copy from the character after '~' 102 // copy from the character after '~'
102 retval += static_cast<const char *>(filename.c_str() + pos + 1); 103 retval += static_cast<const char *>(filename.c_str() + pos + 1);
@@ -110,16 +111,16 @@ string expandFilename(const std::string &filename) {
110/** 111/**
111 @return string from last "." to end of string 112 @return string from last "." to end of string
112*/ 113*/
113string findExtension(const std::string &filename) { 114string findExtension(const string &filename) {
114 //get start of extension 115 //get start of extension
115 std::string::size_type start_pos = filename.find_last_of("."); 116 string::size_type start_pos = filename.find_last_of(".");
116 if (start_pos == std::string::npos && start_pos != filename.size()) 117 if (start_pos == string::npos && start_pos != filename.size())
117 return ""; 118 return "";
118 // return from last . to end of string 119 // return from last . to end of string
119 return filename.substr(start_pos + 1); 120 return filename.substr(start_pos + 1);
120} 121}
121 122
122string replaceString(const std::string &original, 123string replaceString(const string &original,
123 const char *findthis, 124 const char *findthis,
124 const char *replace) { 125 const char *replace) {
125 int i=0; 126 int i=0;
@@ -128,7 +129,7 @@ string replaceString(const std::string &original,
128 string ret_str(original); 129 string ret_str(original);
129 while (i < ret_str.size()) { 130 while (i < ret_str.size()) {
130 i = ret_str.find(findthis, i); 131 i = ret_str.find(findthis, i);
131 if (i == std::string::npos) 132 if (i == string::npos)
132 break; 133 break;
133 // erase old string and insert replacement 134 // erase old string and insert replacement
134 ret_str.erase(i, size_of_find); 135 ret_str.erase(i, size_of_find);
@@ -150,31 +151,31 @@ string replaceString(const std::string &original,
150 for the position + 1 in the in-string where the "last"-char value 151 for the position + 1 in the in-string where the "last"-char value
151 was found. 152 was found.
152*/ 153*/
153int getStringBetween(std::string& out, const char *instr, const char first, const char last, 154int getStringBetween(string& out, const char *instr, const char first, const char last,
154 const char *ok_chars, bool allow_nesting) { 155 const char *ok_chars, bool allow_nesting) {
155 assert(first); 156 assert(first);
156 assert(last); 157 assert(last);
157 assert(instr); 158 assert(instr);
158 159
159 std::string::size_type i = 0, 160 string::size_type i = 0;
160 total_add=0; //used to add extra if there is a \last to skip 161 string::size_type total_add=0; //used to add extra if there is a \last to skip
161 std::string in(instr); 162 string in(instr);
162 163
163 // eat leading whitespace 164 // eat leading whitespace
164 i = in.find_first_not_of(ok_chars); 165 i = in.find_first_not_of(ok_chars);
165 if (i == std::string::npos) 166 if (i == string::npos)
166 return -in.size(); // nothing left but whitespace 167 return -in.size(); // nothing left but whitespace
167 168
168 if (in[i]!=first) 169 if (in[i]!=first)
169 return -i; //return position to error 170 return -i; //return position to error
170 171
171 // find the end of the token 172 // find the end of the token
172 std::string::size_type j = i, k; 173 string::size_type j = i, k;
173 int nesting = 0; 174 int nesting = 0;
174 while (1) { 175 while (1) {
175 k = in.find_first_of(first, j+1); 176 k = in.find_first_of(first, j+1);
176 j = in.find_first_of(last, j+1); 177 j = in.find_first_of(last, j+1);
177 if (j==std::string::npos) 178 if (j==string::npos)
178 return -in.size(); //send negative size 179 return -in.size(); //send negative size
179 180
180 if (allow_nesting && k < j && in[k-1] != '\\') { 181 if (allow_nesting && k < j && in[k-1] != '\\') {
@@ -199,26 +200,26 @@ int getStringBetween(std::string& out, const char *instr, const char first, cons
199 return (j+1+total_add); 200 return (j+1+total_add);
200} 201}
201 202
202std::string toLower(const std::string &conv) { 203string toLower(const string &conv) {
203 std::string ret = conv; 204 string ret = conv;
204 std::transform(ret.begin(), ret.end(), ret.begin(), tolower); 205 transform(ret.begin(), ret.end(), ret.begin(), tolower);
205 return ret; 206 return ret;
206} 207}
207 208
208std::string toUpper(const std::string &conv) { 209string toUpper(const string &conv) {
209 std::string ret = conv; 210 string ret = conv;
210 std::transform(ret.begin(), ret.end(), ret.begin(), toupper); 211 transform(ret.begin(), ret.end(), ret.begin(), toupper);
211 return ret; 212 return ret;
212} 213}
213 214
214std::string basename(const std::string &filename) { 215string basename(const string &filename) {
215 std::string::size_type first_pos = filename.find_last_of("/"); 216 string::size_type first_pos = filename.find_last_of("/");
216 if (first_pos != std::string::npos) 217 if (first_pos != string::npos)
217 return filename.substr(first_pos + 1); 218 return filename.substr(first_pos + 1);
218 return filename; 219 return filename;
219} 220}
220 221
221string::size_type removeFirstWhitespace(std::string &str) { 222string::size_type removeFirstWhitespace(string &str) {
222 string::size_type first_pos = str.find_first_not_of(" \t"); 223 string::size_type first_pos = str.find_first_not_of(" \t");
223 if (first_pos != string::npos) 224 if (first_pos != string::npos)
224 str.erase(0, first_pos); 225 str.erase(0, first_pos);
@@ -226,7 +227,7 @@ string::size_type removeFirstWhitespace(std::string &str) {
226} 227}
227 228
228 229
229string::size_type removeTrailingWhitespace(std::string &str) { 230string::size_type removeTrailingWhitespace(string &str) {
230 // strip trailing whitespace 231 // strip trailing whitespace
231 string::size_type first_pos = str.find_last_not_of(" \t"); 232 string::size_type first_pos = str.find_last_not_of(" \t");
232 if (first_pos != string::npos) { 233 if (first_pos != string::npos) {