cmStringAlgorithms.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmStringAlgorithms_h
  4. #define cmStringAlgorithms_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmRange.h"
  7. #include "cm_string_view.hxx"
  8. #include <sstream>
  9. #include <string.h>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. /** String range type. */
  14. typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange;
  15. /** Callable string comparison struct. */
  16. struct cmStrCmp
  17. {
  18. cmStrCmp(std::string str)
  19. : Test_(std::move(str))
  20. {
  21. }
  22. bool operator()(cm::string_view sv) const { return Test_ == sv; }
  23. private:
  24. std::string const Test_;
  25. };
  26. /** Joins elements of a range with separator into a single string. */
  27. template <typename Range>
  28. std::string cmJoin(Range const& rng, cm::string_view separator)
  29. {
  30. if (rng.empty()) {
  31. return std::string();
  32. }
  33. std::ostringstream os;
  34. auto it = rng.begin();
  35. auto const end = rng.end();
  36. os << *it;
  37. while (++it != end) {
  38. os << separator << *it;
  39. }
  40. return os.str();
  41. }
  42. template <typename Range>
  43. std::string cmWrap(std::string const& prefix, Range const& r,
  44. std::string const& suffix, std::string const& sep)
  45. {
  46. if (r.empty()) {
  47. return std::string();
  48. }
  49. return prefix + cmJoin(r, suffix + sep + prefix) + suffix;
  50. }
  51. template <typename Range>
  52. std::string cmWrap(char prefix, Range const& r, char suffix,
  53. std::string const& sep)
  54. {
  55. return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep);
  56. }
  57. /** Returns true if string @a str starts with the character @a prefix. */
  58. inline bool cmHasPrefix(cm::string_view str, char prefix)
  59. {
  60. return !str.empty() && (str.front() == prefix);
  61. }
  62. /** Returns true if string @a str starts with string @a prefix. */
  63. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  64. {
  65. return str.compare(0, prefix.size(), prefix) == 0;
  66. }
  67. /** Returns true if string @a str starts with string @a prefix. */
  68. template <size_t N>
  69. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  70. {
  71. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  72. }
  73. /** Returns true if string @a str ends with the character @a suffix. */
  74. inline bool cmHasSuffix(cm::string_view str, char suffix)
  75. {
  76. return !str.empty() && (str.back() == suffix);
  77. }
  78. /** Returns true if string @a str ends with string @a suffix. */
  79. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  80. {
  81. return str.size() >= suffix.size() &&
  82. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  83. }
  84. /** Returns true if string @a str ends with string @a suffix. */
  85. template <size_t N>
  86. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  87. {
  88. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  89. }
  90. /** Removes an existing suffix character of from the string @a str. */
  91. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  92. {
  93. if (cmHasSuffix(str, suffix)) {
  94. str.pop_back();
  95. }
  96. }
  97. /** Removes an existing suffix string of from the string @a str. */
  98. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  99. {
  100. if (cmHasSuffix(str, suffix)) {
  101. str.resize(str.size() - suffix.size());
  102. }
  103. }
  104. #endif