Changeset 1049 for code/branches/core2/src/util
- Timestamp:
- Apr 14, 2008, 12:48:25 AM (17 years ago)
- Location:
- code/branches/core2/src/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core2/src/util/String.cc
r1020 r1049 27 27 28 28 #include <cctype> 29 29 #include <iostream> 30 30 #include "String.h" 31 31 … … 92 92 93 93 return quote; 94 } 95 96 /** 97 @brief Returns true if pos is between two quotes. 98 @param str The string 99 @param pos The position to check 100 @return True if pos is between two quotes 101 */ 102 bool isBetweenQuotes(const std::string& str, unsigned int pos) 103 { 104 if (pos == std::string::npos) 105 return false; 106 107 unsigned int quotecount = 0; 108 unsigned int quote = 0; 109 while ((quote = getNextQuote(str, quote)) < pos) 110 { 111 quotecount++; 112 } 113 114 if (quote == std::string::npos) 115 return false; 116 117 return ((quotecount % 2) == 1); 94 118 } 95 119 … … 124 148 @brief Removes enclosing quotes if available. 125 149 @brief str The string to strip 126 */ 127 void stripEnclosingQuotes(std::string* str) 150 @return The string with removed quotes 151 */ 152 std::string stripEnclosingQuotes(const std::string& str) 128 153 { 129 154 unsigned int start = std::string::npos; 130 155 unsigned int end = 0; 131 156 132 for (unsigned int pos = 0; (pos < (*str).size()) && (pos < std::string::npos); pos++)133 { 134 if ( (*str)[pos] == '"')157 for (unsigned int pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++) 158 { 159 if (str[pos] == '"') 135 160 { 136 161 start = pos; … … 138 163 } 139 164 140 if (( (*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n'))141 return ;142 } 143 144 for (unsigned int pos = (*str).size() - 1; pos < std::string::npos; pos--)145 { 146 if ( (*str)[pos] == '"')165 if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) 166 return str; 167 } 168 169 for (unsigned int pos = str.size() - 1; pos < std::string::npos; pos--) 170 { 171 if (str[pos] == '"') 147 172 { 148 173 end = pos; … … 150 175 } 151 176 152 if (( (*str)[pos] != ' ') && ((*str)[pos] != '\t') && ((*str)[pos] != '\n'))153 return ;177 if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) 178 return str; 154 179 } 155 180 156 181 if ((start != std::string::npos) && (end != 0)) 157 (*str) = (*str).substr(start + 1, end - start - 1); 158 } 159 160 /** 161 @brief Returns a copy of the string with removed enclosing quotes (if available). 162 @brief str The string to strip 163 @return The striped copy of the string 164 */ 165 std::string getStrippedEnclosingQuotes(const std::string& str) 166 { 167 std::string output = std::string(str); 168 stripEnclosingQuotes(&output); 169 return output; 182 return str.substr(start + 1, end - start - 1); 183 else 184 return str; 170 185 } 171 186 … … 235 250 } 236 251 252 std::string addSlashes(const std::string& str) 253 { 254 std::string output = str; 255 256 for (unsigned int pos = 0; (pos = output.find('\\', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\\\"); } 257 for (unsigned int pos = 0; (pos = output.find('\n', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\n"); } 258 for (unsigned int pos = 0; (pos = output.find('\t', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\t"); } 259 for (unsigned int pos = 0; (pos = output.find('\v', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\v"); } 260 for (unsigned int pos = 0; (pos = output.find('\b', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\b"); } 261 for (unsigned int pos = 0; (pos = output.find('\r', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\r"); } 262 for (unsigned int pos = 0; (pos = output.find('\f', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\f"); } 263 for (unsigned int pos = 0; (pos = output.find('\a', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\a"); } 264 for (unsigned int pos = 0; (pos = output.find('"', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\\""); } 265 for (unsigned int pos = 0; (pos = output.find('\0', pos)) < std::string::npos; pos += 2) { output.replace(pos, 2, "\\0"); } 266 267 return output; 268 } 269 270 std::string removeSlashes(const std::string& str) 271 { 272 if (str.size() == 0) 273 return str; 274 275 std::string output = ""; 276 for (unsigned int pos = 0; pos < str.size() - 1; ) 277 { 278 if (str[pos] == '\\') 279 { 280 if (str[pos + 1] == '\\') { output += '\\'; pos += 2; continue; } 281 else if (str[pos + 1] == 'n') { output += '\n'; pos += 2; continue; } 282 else if (str[pos + 1] == 't') { output += '\t'; pos += 2; continue; } 283 else if (str[pos + 1] == 'v') { output += '\v'; pos += 2; continue; } 284 else if (str[pos + 1] == 'b') { output += '\b'; pos += 2; continue; } 285 else if (str[pos + 1] == 'r') { output += '\r'; pos += 2; continue; } 286 else if (str[pos + 1] == 'f') { output += '\f'; pos += 2; continue; } 287 else if (str[pos + 1] == 'a') { output += '\a'; pos += 2; continue; } 288 else if (str[pos + 1] == '"') { output += '"'; pos += 2; continue; } 289 else if (str[pos + 1] == '0') { output += '\0'; pos += 2; continue; } 290 } 291 output += str[pos]; 292 pos++; 293 } 294 output += str[str.size() - 1]; 295 296 return output; 297 } 298 237 299 /** 238 300 @brief Replaces each char between A and Z with its lowercase equivalent. … … 364 426 unsigned int getCommentPosition(const std::string& str) 365 427 { 366 for (unsigned int i = 0; i < str.size(); i++) 428 return getNextCommentPosition(str, 0); 429 } 430 431 /** 432 @brief Returns the position of the next comment-symbol, starting with start. 433 @param str The string 434 @param start The startposition 435 @return The position 436 */ 437 unsigned int getNextCommentPosition(const std::string& str, unsigned int start) 438 { 439 for (unsigned int i = start; i < str.size(); i++) 367 440 if (isComment(str.substr(i))) 368 441 return i; -
code/branches/core2/src/util/String.h
r1020 r1049 40 40 41 41 _UtilExport unsigned int getNextQuote(const std::string& str, unsigned int start); 42 _UtilExport bool isBetweenQuotes(const std::string& str, unsigned int pos); 42 43 43 44 _UtilExport bool hasStringBetweenQuotes(const std::string& str); 44 45 _UtilExport std::string getStringBetweenQuotes(const std::string& str); 45 46 46 _UtilExport void stripEnclosingQuotes(std::string* str); 47 _UtilExport std::string getStrippedEnclosingQuotes(const std::string& str); 47 _UtilExport std::string stripEnclosingQuotes(const std::string& str); 48 48 49 49 _UtilExport bool isEmpty(const std::string& str); 50 50 _UtilExport bool isComment(const std::string& str); 51 51 _UtilExport bool isNumeric(const std::string& str); 52 53 _UtilExport std::string addSlashes(const std::string& str); 54 _UtilExport std::string removeSlashes(const std::string& str); 52 55 53 56 _UtilExport void lowercase(std::string* str); … … 63 66 _UtilExport std::string getComment(const std::string& str); 64 67 _UtilExport unsigned int getCommentPosition(const std::string& str); 68 _UtilExport unsigned int getNextCommentPosition(const std::string& str, unsigned int start = 0); 65 69 66 70 //! The Convert class has some static member functions to convert strings to values and values to strings.
Note: See TracChangeset
for help on using the changeset viewer.