| [1505] | 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Fabian 'x3n' Landau |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * Benjamin Grauer |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| [1791] | 29 | /** |
|---|
| [2171] | 30 | @file |
|---|
| [1791] | 31 | @brief Implementation of several string manipulation functions. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| [3250] | 34 | #include "StringUtils.h" |
|---|
| [1505] | 35 | |
|---|
| 36 | #include <cctype> |
|---|
| [8232] | 37 | #include <ctime> |
|---|
| [7284] | 38 | #include <boost/scoped_array.hpp> |
|---|
| [2087] | 39 | #include "Convert.h" |
|---|
| 40 | #include "Math.h" |
|---|
| [1625] | 41 | |
|---|
| [2171] | 42 | namespace orxonox |
|---|
| [2087] | 43 | { |
|---|
| [7401] | 44 | /// A blank string (""). Used to return a blank string by reference. |
|---|
| [6417] | 45 | std::string BLANKSTRING; |
|---|
| [2087] | 46 | |
|---|
| [7401] | 47 | /// Returns a string of a unique number. This function is guaranteed to never return the same string twice. |
|---|
| [2171] | 48 | std::string getUniqueNumberString() |
|---|
| 49 | { |
|---|
| [3280] | 50 | return multi_cast<std::string>(getUniqueNumber()); |
|---|
| [2171] | 51 | } |
|---|
| [1505] | 52 | |
|---|
| [7401] | 53 | /// Removes all whitespaces from a string. |
|---|
| [2171] | 54 | void strip(std::string* str) |
|---|
| 55 | { |
|---|
| 56 | size_t pos; |
|---|
| [6417] | 57 | while ((pos = str->find(' ')) < str->length()) |
|---|
| 58 | str->erase(pos, 1); |
|---|
| 59 | while ((pos = str->find('\t')) < str->length()) |
|---|
| 60 | str->erase(pos, 1); |
|---|
| 61 | while ((pos = str->find('\n')) < str->length()) |
|---|
| 62 | str->erase(pos, 1); |
|---|
| [2171] | 63 | } |
|---|
| [1505] | 64 | |
|---|
| [7401] | 65 | /// Returns a copy of a string without whitespaces. |
|---|
| [2171] | 66 | std::string getStripped(const std::string& str) |
|---|
| 67 | { |
|---|
| [6417] | 68 | std::string output(str); |
|---|
| [2171] | 69 | strip(&output); |
|---|
| 70 | return output; |
|---|
| 71 | } |
|---|
| [1505] | 72 | |
|---|
| [7401] | 73 | /// Returns a copy of a string without trailing whitespaces. |
|---|
| [2171] | 74 | std::string removeTrailingWhitespaces(const std::string& str) |
|---|
| 75 | { |
|---|
| 76 | size_t pos1 = 0; |
|---|
| [3300] | 77 | int pos2 = static_cast<int>(str.size() - 1); |
|---|
| [2171] | 78 | for (; pos1 < str.size() && (str[pos1] == ' ' || str[pos1] == '\t' || str[pos1] == '\n'); pos1++); |
|---|
| 79 | for (; pos2 > 0 && (str[pos2] == ' ' || str[pos2] == '\t' || str[pos2] == '\n'); pos2--); |
|---|
| 80 | return str.substr(pos1, pos2 - pos1 + 1); |
|---|
| 81 | } |
|---|
| [1505] | 82 | |
|---|
| [2171] | 83 | /** |
|---|
| [7401] | 84 | @brief Returns the position of the next quotation mark in the string, starting with start. |
|---|
| [2171] | 85 | @param str The string |
|---|
| [7401] | 86 | @param start The first position to look at |
|---|
| 87 | @return The position of the next quotation mark (@c std::string::npos if there is none) |
|---|
| [2171] | 88 | */ |
|---|
| 89 | size_t getNextQuote(const std::string& str, size_t start) |
|---|
| [1505] | 90 | { |
|---|
| [2171] | 91 | size_t quote = start - 1; |
|---|
| 92 | |
|---|
| [6417] | 93 | while ((quote = str.find('"', quote + 1)) != std::string::npos) |
|---|
| [2171] | 94 | { |
|---|
| 95 | size_t backslash = quote; |
|---|
| 96 | size_t numbackslashes = 0; |
|---|
| 97 | for (; backslash > 0; backslash--, numbackslashes++) |
|---|
| 98 | if (str[backslash - 1] != '\\') |
|---|
| 99 | break; |
|---|
| 100 | |
|---|
| 101 | if (numbackslashes % 2 == 0) |
|---|
| [1505] | 102 | break; |
|---|
| [2171] | 103 | } |
|---|
| [1505] | 104 | |
|---|
| [2171] | 105 | return quote; |
|---|
| [1505] | 106 | } |
|---|
| 107 | |
|---|
| [2171] | 108 | /** |
|---|
| [7401] | 109 | @brief Returns true if pos is between two quotation marks. |
|---|
| [2171] | 110 | @param str The string |
|---|
| 111 | @param pos The position to check |
|---|
| [7401] | 112 | @return True if pos is between two quotation marks |
|---|
| [2171] | 113 | */ |
|---|
| 114 | bool isBetweenQuotes(const std::string& str, size_t pos) |
|---|
| [1505] | 115 | { |
|---|
| [2171] | 116 | if (pos == std::string::npos) |
|---|
| [1830] | 117 | return false; |
|---|
| [1505] | 118 | |
|---|
| [2171] | 119 | size_t quotecount = 0; |
|---|
| [3301] | 120 | size_t quote = static_cast<size_t>(-1); |
|---|
| [2171] | 121 | while ((quote = getNextQuote(str, quote + 1)) < pos) |
|---|
| 122 | { |
|---|
| 123 | if (quote == pos) |
|---|
| 124 | return false; |
|---|
| 125 | quotecount++; |
|---|
| 126 | } |
|---|
| [1505] | 127 | |
|---|
| [2171] | 128 | if (quote == std::string::npos) |
|---|
| 129 | return false; |
|---|
| [1505] | 130 | |
|---|
| [2171] | 131 | return ((quotecount % 2) == 1); |
|---|
| 132 | } |
|---|
| [1505] | 133 | |
|---|
| [7401] | 134 | /// Returns true if the string contains something like '..."between quotaton marks"...'. |
|---|
| [2171] | 135 | bool hasStringBetweenQuotes(const std::string& str) |
|---|
| 136 | { |
|---|
| 137 | size_t pos1 = getNextQuote(str, 0); |
|---|
| 138 | size_t pos2 = getNextQuote(str, pos1 + 1); |
|---|
| 139 | return (pos1 != std::string::npos && pos2 != std::string::npos && pos2 > pos1 + 1); |
|---|
| 140 | } |
|---|
| [1505] | 141 | |
|---|
| [7401] | 142 | /// If the string contains something like '..."between quotaton marks"...' then 'between quotaton marks' gets returned, otherwise "". |
|---|
| [2171] | 143 | std::string getStringBetweenQuotes(const std::string& str) |
|---|
| 144 | { |
|---|
| 145 | size_t pos1 = getNextQuote(str, 0); |
|---|
| 146 | size_t pos2 = getNextQuote(str, pos1 + 1); |
|---|
| 147 | if (pos1 != std::string::npos && pos2 != std::string::npos) |
|---|
| 148 | return str.substr(pos1, pos2 - pos1 + 1); |
|---|
| 149 | else |
|---|
| 150 | return ""; |
|---|
| 151 | } |
|---|
| [1505] | 152 | |
|---|
| [2171] | 153 | /** |
|---|
| [7401] | 154 | @brief Removes enclosing quotation marks if available (including whitespaces at the outside of the quotation marks). |
|---|
| 155 | @return The striped string without quotation marks |
|---|
| [2171] | 156 | */ |
|---|
| 157 | std::string stripEnclosingQuotes(const std::string& str) |
|---|
| [1505] | 158 | { |
|---|
| [2171] | 159 | size_t start = std::string::npos; |
|---|
| 160 | size_t end = 0; |
|---|
| 161 | |
|---|
| 162 | for (size_t pos = 0; (pos < str.size()) && (pos < std::string::npos); pos++) |
|---|
| [1505] | 163 | { |
|---|
| [2171] | 164 | if (str[pos] == '"') |
|---|
| 165 | { |
|---|
| 166 | start = pos; |
|---|
| 167 | break; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) |
|---|
| 171 | return str; |
|---|
| [1505] | 172 | } |
|---|
| 173 | |
|---|
| [2171] | 174 | for (size_t pos = str.size() - 1; pos < std::string::npos; pos--) |
|---|
| 175 | { |
|---|
| 176 | if (str[pos] == '"') |
|---|
| 177 | { |
|---|
| 178 | end = pos; |
|---|
| 179 | break; |
|---|
| 180 | } |
|---|
| [1505] | 181 | |
|---|
| [2171] | 182 | if ((str[pos] != ' ') && (str[pos] != '\t') && (str[pos] != '\n')) |
|---|
| 183 | return str; |
|---|
| [1505] | 184 | } |
|---|
| 185 | |
|---|
| [2171] | 186 | if ((start != std::string::npos) && (end != 0)) |
|---|
| 187 | return str.substr(start + 1, end - start - 1); |
|---|
| 188 | else |
|---|
| [1505] | 189 | return str; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| [2171] | 192 | /** |
|---|
| [7401] | 193 | @brief Removes enclosing braces '{' and '}' (the braces must be exactly on the beginning and the end of the string). |
|---|
| 194 | @return The striped string without braces |
|---|
| [2171] | 195 | */ |
|---|
| 196 | std::string stripEnclosingBraces(const std::string& str) |
|---|
| 197 | { |
|---|
| 198 | std::string output = str; |
|---|
| [1505] | 199 | |
|---|
| [2171] | 200 | while (output.size() >= 2 && output[0] == '{' && output[output.size() - 1] == '}') |
|---|
| 201 | output = output.substr(1, output.size() - 2); |
|---|
| [1505] | 202 | |
|---|
| [2171] | 203 | return output; |
|---|
| 204 | } |
|---|
| [1505] | 205 | |
|---|
| [2171] | 206 | /** |
|---|
| 207 | @brief Determines if a string is a comment (starts with a comment-symbol). |
|---|
| [1505] | 208 | |
|---|
| [2171] | 209 | A comment is defined by a leading '#', '%', ';' or '//'. |
|---|
| 210 | */ |
|---|
| 211 | bool isComment(const std::string& str) |
|---|
| 212 | { |
|---|
| 213 | // Strip the line, whitespaces are disturbing |
|---|
| [6417] | 214 | const std::string& teststring = getStripped(str); |
|---|
| [1505] | 215 | |
|---|
| [2171] | 216 | // There are four possible comment-symbols: |
|---|
| 217 | // 1) #comment in script-language style |
|---|
| 218 | // 2) %comment in matlab style |
|---|
| 219 | // 3) ;comment in unreal tournament config-file style |
|---|
| 220 | // 4) //comment in code style |
|---|
| 221 | if (teststring.size() >= 2) |
|---|
| 222 | { |
|---|
| 223 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';' || (teststring[0] == '/' && teststring[1] == '/')) |
|---|
| 224 | return true; |
|---|
| 225 | } |
|---|
| 226 | else if (teststring.size() == 1) |
|---|
| 227 | { |
|---|
| 228 | if (teststring[0] == '#' || teststring[0] == '%' || teststring[0] == ';') |
|---|
| 229 | return true; |
|---|
| 230 | } |
|---|
| [1505] | 231 | |
|---|
| [2171] | 232 | return false; |
|---|
| [1505] | 233 | } |
|---|
| [2171] | 234 | |
|---|
| [7401] | 235 | /// Determines if a string is empty (contains only whitespaces). |
|---|
| [2171] | 236 | bool isEmpty(const std::string& str) |
|---|
| [1505] | 237 | { |
|---|
| [6417] | 238 | return getStripped(str).empty(); |
|---|
| [1505] | 239 | } |
|---|
| 240 | |
|---|
| [7401] | 241 | /// Determines if a string contains only numbers and maximal one '.'. |
|---|
| [2171] | 242 | bool isNumeric(const std::string& str) |
|---|
| 243 | { |
|---|
| 244 | bool foundPoint = false; |
|---|
| [1505] | 245 | |
|---|
| [2171] | 246 | for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) |
|---|
| [1505] | 247 | { |
|---|
| [2171] | 248 | if (((*it) < '0' || (*it) > '9')) |
|---|
| 249 | { |
|---|
| 250 | if ((*it) != '.' && !foundPoint) |
|---|
| 251 | foundPoint = true; |
|---|
| 252 | else |
|---|
| 253 | return false; |
|---|
| 254 | } |
|---|
| [1505] | 255 | } |
|---|
| [2171] | 256 | |
|---|
| 257 | return true; |
|---|
| [1505] | 258 | } |
|---|
| 259 | |
|---|
| [2171] | 260 | /** |
|---|
| 261 | @brief Adds backslashes to the given string which makes special chars visible. Existing slashes will be doubled. |
|---|
| [7401] | 262 | |
|---|
| 263 | This function converts all special chars like line breaks, tabs, quotation marks etc. into |
|---|
| 264 | a human readable format by adding a backslash. So for example "\n" will be converted to |
|---|
| 265 | "\\" + "n". |
|---|
| 266 | |
|---|
| 267 | This is usually used when a string is written to a file. |
|---|
| 268 | |
|---|
| 269 | @see removeSlashes |
|---|
| [2171] | 270 | */ |
|---|
| 271 | std::string addSlashes(const std::string& str) |
|---|
| 272 | { |
|---|
| [6424] | 273 | std::string output(str.size() * 2, ' '); |
|---|
| 274 | size_t i = 0; |
|---|
| 275 | for (size_t pos = 0; pos < str.size(); ++pos) |
|---|
| 276 | { |
|---|
| 277 | switch (str[pos]) |
|---|
| 278 | { |
|---|
| 279 | case '\\': output[i] = '\\'; output[i + 1] = '\\'; break; |
|---|
| 280 | case '\n': output[i] = '\\'; output[i + 1] = 'n'; break; |
|---|
| 281 | case '\t': output[i] = '\\'; output[i + 1] = 't'; break; |
|---|
| 282 | case '\v': output[i] = '\\'; output[i + 1] = 'v'; break; |
|---|
| 283 | case '\b': output[i] = '\\'; output[i + 1] = 'b'; break; |
|---|
| 284 | case '\r': output[i] = '\\'; output[i + 1] = 'r'; break; |
|---|
| 285 | case '\f': output[i] = '\\'; output[i + 1] = 'f'; break; |
|---|
| 286 | case '\a': output[i] = '\\'; output[i + 1] = 'a'; break; |
|---|
| 287 | case '"': output[i] = '\\'; output[i + 1] = '"'; break; |
|---|
| 288 | case '\0': output[i] = '\\'; output[i + 1] = '0'; break; |
|---|
| 289 | default : output[i] = str[pos]; ++i; continue; |
|---|
| 290 | } |
|---|
| 291 | i += 2; |
|---|
| 292 | } |
|---|
| 293 | output.resize(i); |
|---|
| [1505] | 294 | |
|---|
| [2171] | 295 | return output; |
|---|
| 296 | } |
|---|
| [1505] | 297 | |
|---|
| [2171] | 298 | /** |
|---|
| 299 | @brief Removes backslashes from the given string. Double backslashes are interpreted as one backslash. |
|---|
| [7401] | 300 | |
|---|
| 301 | This function removes all backslashes and converts the human readable equivalents of |
|---|
| 302 | special chars like "\\" + "n" into their real meaning (in this case a line break or "\n"). |
|---|
| 303 | |
|---|
| 304 | This is usually used when reading a string from a file. |
|---|
| 305 | |
|---|
| 306 | @see addSlashes |
|---|
| [2171] | 307 | */ |
|---|
| 308 | std::string removeSlashes(const std::string& str) |
|---|
| 309 | { |
|---|
| 310 | if (str.size() <= 1) |
|---|
| 311 | return str; |
|---|
| [1505] | 312 | |
|---|
| [6424] | 313 | std::string output(str.size(), ' '); |
|---|
| 314 | size_t i = 0; |
|---|
| 315 | size_t pos = 0; |
|---|
| 316 | while (pos < str.size() - 1) |
|---|
| [2171] | 317 | { |
|---|
| 318 | if (str[pos] == '\\') |
|---|
| 319 | { |
|---|
| [6424] | 320 | switch (str[pos + 1]) |
|---|
| 321 | { |
|---|
| 322 | case '\\': output[i] = '\\'; break; |
|---|
| 323 | case 'n': output[i] = '\n'; break; |
|---|
| 324 | case 't': output[i] = '\t'; break; |
|---|
| 325 | case 'v': output[i] = '\v'; break; |
|---|
| 326 | case 'b': output[i] = '\b'; break; |
|---|
| 327 | case 'r': output[i] = '\r'; break; |
|---|
| 328 | case 'f': output[i] = '\f'; break; |
|---|
| 329 | case 'a': output[i] = '\a'; break; |
|---|
| 330 | case '"': output[i] = '"'; break; |
|---|
| 331 | case '0': output[i] = '\0'; break; |
|---|
| 332 | default: ++pos; continue; |
|---|
| 333 | } |
|---|
| 334 | pos += 2; ++i; |
|---|
| [2171] | 335 | } |
|---|
| [6424] | 336 | else |
|---|
| 337 | output[i++] = str[pos++]; |
|---|
| [2171] | 338 | } |
|---|
| [6424] | 339 | if (pos < str.size()) |
|---|
| 340 | output[i++] = str[pos]; |
|---|
| 341 | output.resize(i); |
|---|
| [1505] | 342 | |
|---|
| [2171] | 343 | return output; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| [7401] | 346 | /// Replaces each char between A and Z with its lowercase equivalent. |
|---|
| [2171] | 347 | void lowercase(std::string* str) |
|---|
| [1505] | 348 | { |
|---|
| [2171] | 349 | for (size_t i = 0; i < str->size(); ++i) |
|---|
| [1505] | 350 | { |
|---|
| [3300] | 351 | (*str)[i] = static_cast<char>(tolower((*str)[i])); |
|---|
| [1505] | 352 | } |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| [7401] | 355 | /// Returns a copy of the given string where all chars are converted to lowercase. |
|---|
| [2171] | 356 | std::string getLowercase(const std::string& str) |
|---|
| 357 | { |
|---|
| [6417] | 358 | std::string output(str); |
|---|
| [2171] | 359 | lowercase(&output); |
|---|
| 360 | return output; |
|---|
| 361 | } |
|---|
| [1505] | 362 | |
|---|
| [7401] | 363 | /// Replaces each char between a and z with its uppercase equivalent. |
|---|
| [2171] | 364 | void uppercase(std::string* str) |
|---|
| [1505] | 365 | { |
|---|
| [2171] | 366 | for (size_t i = 0; i < str->size(); ++i) |
|---|
| 367 | { |
|---|
| [3300] | 368 | (*str)[i] = static_cast<char>(toupper((*str)[i])); |
|---|
| [2171] | 369 | } |
|---|
| [1505] | 370 | } |
|---|
| 371 | |
|---|
| [7401] | 372 | /// Returns a copy of the given string where all chars are converted to uppercase. |
|---|
| [2171] | 373 | std::string getUppercase(const std::string& str) |
|---|
| 374 | { |
|---|
| [6417] | 375 | std::string output(str); |
|---|
| [2171] | 376 | uppercase(&output); |
|---|
| 377 | return output; |
|---|
| 378 | } |
|---|
| [1505] | 379 | |
|---|
| [2171] | 380 | /** |
|---|
| 381 | @brief Compares two strings ignoring different casing. |
|---|
| [7401] | 382 | @return s1 == s1 -> returns 0 / s1 < s2 -> returns -1 / s1 >= s2 -> returns 1 |
|---|
| [2171] | 383 | */ |
|---|
| 384 | int nocaseCmp(const std::string& s1, const std::string& s2) |
|---|
| [1505] | 385 | { |
|---|
| [2171] | 386 | std::string::const_iterator it1=s1.begin(); |
|---|
| 387 | std::string::const_iterator it2=s2.begin(); |
|---|
| 388 | |
|---|
| 389 | //stop when either string's end has been reached |
|---|
| 390 | while ( (it1!=s1.end()) && (it2!=s2.end()) ) |
|---|
| 391 | { |
|---|
| 392 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
|---|
| 393 | // return -1 to indicate smaller than, 1 otherwise |
|---|
| 394 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
|---|
| 395 | //proceed to the next character in each string |
|---|
| 396 | ++it1; |
|---|
| 397 | ++it2; |
|---|
| 398 | } |
|---|
| 399 | size_t size1=s1.size(), size2=s2.size();// cache lengths |
|---|
| 400 | //return -1,0 or 1 according to strings' lengths |
|---|
| 401 | if (size1==size2) |
|---|
| 402 | return 0; |
|---|
| 403 | return (size1<size2) ? -1 : 1; |
|---|
| [1505] | 404 | } |
|---|
| 405 | |
|---|
| 406 | |
|---|
| [2171] | 407 | /** |
|---|
| [7401] | 408 | @brief Compares the first @a len chars of two strings ignoring different casing. |
|---|
| [2171] | 409 | @param s1 First string |
|---|
| 410 | @param s2 Second string |
|---|
| 411 | @param len Maximal number of chars to compare |
|---|
| 412 | */ |
|---|
| 413 | int nocaseCmp(const std::string& s1, const std::string& s2, size_t len) |
|---|
| [1505] | 414 | { |
|---|
| [2171] | 415 | if (len == 0) |
|---|
| 416 | return 0; |
|---|
| 417 | std::string::const_iterator it1=s1.begin(); |
|---|
| 418 | std::string::const_iterator it2=s2.begin(); |
|---|
| [1505] | 419 | |
|---|
| [2171] | 420 | //stop when either string's end has been reached |
|---|
| 421 | while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0) |
|---|
| 422 | { |
|---|
| 423 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
|---|
| 424 | // return -1 to indicate smaller than, 1 otherwise |
|---|
| 425 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
|---|
| 426 | //proceed to the next character in each string |
|---|
| 427 | ++it1; |
|---|
| 428 | ++it2; |
|---|
| 429 | } |
|---|
| [1505] | 430 | return 0; |
|---|
| [2171] | 431 | } |
|---|
| [1505] | 432 | |
|---|
| [7401] | 433 | /// Returns true if the string contains a comment, introduced by #, %, ; or //. |
|---|
| [2171] | 434 | bool hasComment(const std::string& str) |
|---|
| [1505] | 435 | { |
|---|
| [2171] | 436 | return (getCommentPosition(str) != std::string::npos); |
|---|
| [1505] | 437 | } |
|---|
| 438 | |
|---|
| [7401] | 439 | /// If the string contains a comment, the comment gets returned (including the comment symbol), an empty string otherwise. |
|---|
| [2171] | 440 | std::string getComment(const std::string& str) |
|---|
| 441 | { |
|---|
| 442 | return str.substr(getCommentPosition(str)); |
|---|
| 443 | } |
|---|
| [1505] | 444 | |
|---|
| [7401] | 445 | /// If the string contains a comment, the position of the comment-symbol gets returned, @c std::string::npos otherwise. |
|---|
| [2171] | 446 | size_t getCommentPosition(const std::string& str) |
|---|
| 447 | { |
|---|
| 448 | return getNextCommentPosition(str, 0); |
|---|
| 449 | } |
|---|
| [1505] | 450 | |
|---|
| [2171] | 451 | /** |
|---|
| [7401] | 452 | @brief Returns the position of the next comment-symbol, starting with @a start. |
|---|
| [2171] | 453 | @param str The string |
|---|
| [7401] | 454 | @param start The first position to look at |
|---|
| [2171] | 455 | */ |
|---|
| 456 | size_t getNextCommentPosition(const std::string& str, size_t start) |
|---|
| 457 | { |
|---|
| 458 | for (size_t i = start; i < str.size(); i++) |
|---|
| 459 | if (isComment(str.substr(i))) |
|---|
| 460 | return i; |
|---|
| [1505] | 461 | |
|---|
| [2171] | 462 | return std::string::npos; |
|---|
| 463 | } |
|---|
| [3327] | 464 | |
|---|
| 465 | /** |
|---|
| 466 | @brief Replaces individual charaters |
|---|
| 467 | @param str String to be manipulated |
|---|
| 468 | @param target Character to be replaced |
|---|
| 469 | @param replacement Replacement character |
|---|
| 470 | @return Number of replacements |
|---|
| 471 | */ |
|---|
| [7284] | 472 | size_t replaceCharacters(std::string& str, char target, char replacement) |
|---|
| [3327] | 473 | { |
|---|
| 474 | size_t j = 0; |
|---|
| 475 | for (size_t i = 0; i < str.size(); ++i) |
|---|
| 476 | { |
|---|
| 477 | if (str[i] == target) |
|---|
| 478 | { |
|---|
| 479 | str[i] = replacement; |
|---|
| 480 | ++j; |
|---|
| 481 | } |
|---|
| 482 | } |
|---|
| 483 | return j; |
|---|
| 484 | } |
|---|
| [7284] | 485 | |
|---|
| 486 | /** |
|---|
| 487 | @brief Calculates the Levenshtein distance between two strings. |
|---|
| 488 | |
|---|
| 489 | The Levenshtein distance is defined by the number of transformations needed to convert str1 |
|---|
| 490 | into str2. Possible transformations are substituted, added, or removed characters. |
|---|
| 491 | */ |
|---|
| 492 | unsigned int getLevenshteinDistance(const std::string& str1, const std::string& str2) |
|---|
| 493 | { |
|---|
| 494 | size_t cols = str1.size() + 1; |
|---|
| 495 | size_t rows = str2.size() + 1; |
|---|
| 496 | boost::scoped_array<int> matrix(new int[rows * cols]); |
|---|
| 497 | |
|---|
| 498 | for (size_t r = 0; r < rows; ++r) |
|---|
| 499 | for (size_t c = 0; c < cols; ++c) |
|---|
| 500 | matrix[r*cols + c] = 0; |
|---|
| 501 | |
|---|
| 502 | for (size_t i = 1; i < cols; ++i) |
|---|
| 503 | matrix[0*cols + i] = i; |
|---|
| 504 | for (size_t i = 1; i < rows; ++i) |
|---|
| 505 | matrix[i*cols + 0] = i; |
|---|
| 506 | |
|---|
| 507 | for (size_t r = 1; r < rows; ++r) |
|---|
| 508 | for (size_t c = 1; c < cols; ++c) |
|---|
| 509 | matrix[r*cols + c] = (str1[c-1] != str2[r-1]); |
|---|
| 510 | |
|---|
| 511 | for (size_t r = 1; r < rows; ++r) |
|---|
| 512 | for (size_t c = 1; c < cols; ++c) |
|---|
| 513 | matrix[r*cols + c] = std::min(std::min(matrix[(r-1)*cols + c] + 1, |
|---|
| 514 | matrix[r*cols + c-1] + 1), |
|---|
| 515 | matrix[(r-1)*cols + c-1] + (str1[c-1] != str2[r-1])); |
|---|
| 516 | |
|---|
| 517 | return matrix[(rows-1)*cols + cols-1]; |
|---|
| 518 | } |
|---|
| [8232] | 519 | |
|---|
| 520 | /** |
|---|
| 521 | @brief |
|---|
| 522 | Get a timestamp for the curent time instant. |
|---|
| 523 | @return |
|---|
| 524 | Returns a string with the timestamp. |
|---|
| 525 | */ |
|---|
| 526 | std::string getTimestamp(void) |
|---|
| 527 | { |
|---|
| 528 | struct tm *pTime; |
|---|
| 529 | time_t ctTime; std::time(&ctTime); |
|---|
| 530 | pTime = std::localtime( &ctTime ); |
|---|
| 531 | std::ostringstream oss; |
|---|
| 532 | oss << std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1) |
|---|
| 533 | << std::setw(2) << std::setfill('0') << pTime->tm_mday |
|---|
| 534 | << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900) |
|---|
| 535 | << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour |
|---|
| 536 | << std::setw(2) << std::setfill('0') << pTime->tm_min |
|---|
| 537 | << std::setw(2) << std::setfill('0') << pTime->tm_sec; |
|---|
| 538 | return oss.str(); |
|---|
| 539 | } |
|---|
| [1505] | 540 | } |
|---|