Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

strings.cpp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. * \file src/utils/strings.cpp
  3. * \brief String handling utilities
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include "global.h"
  9. #include "utils/filesystem.h"
  10. #include "utils/strings.h"
  11. std::string findAndReplace(std::string s, std::string find, std::string replace) {
  12. size_t p = s.find(find);
  13. while (p != std::string::npos) {
  14. s.erase(p, find.length());
  15. s.insert(p, replace);
  16. p = s.find(find);
  17. }
  18. return s;
  19. }
  20. std::string expandHomeDirectory(std::string s) {
  21. if ((s.length() > 0) && (s[0] == '~')) {
  22. s.erase(0, 1);
  23. s.insert(0, getHomeDirectory());
  24. }
  25. return s;
  26. }
  27. bool stringEndsWith(std::string s, std::string suffix, bool casesensitive) {
  28. if (!casesensitive) {
  29. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  30. std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
  31. }
  32. if (s.length() >= suffix.length()) {
  33. std::string end = s.substr(s.length() - suffix.length());
  34. return (end == suffix);
  35. }
  36. return false;
  37. }
  38. std::string removeLastPathElement(std::string s) {
  39. auto pos = s.find_last_of("/\\");
  40. if (pos == std::string::npos)
  41. return s;
  42. return s.erase(pos);
  43. }
  44. std::string getLastPathElement(std::string s) {
  45. auto pos = s.find_last_of("/\\");
  46. if (pos == std::string::npos)
  47. return s;
  48. return s.erase(0, pos + 1);
  49. }
  50. std::string convertPathDelimiter(std::string s) {
  51. std::string::size_type pos;
  52. #ifdef _WIN32
  53. while ((pos = s.find('/')) != std::string::npos) {
  54. s.replace(pos, 1, 1, '\\');
  55. }
  56. #else
  57. while ((pos = s.find('\\')) != std::string::npos) {
  58. s.replace(pos, 1, 1, '/');
  59. }
  60. #endif
  61. return s;
  62. }