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