1
0

cmStringAlgorithms.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <algorithm>
  9. #include <iterator>
  10. #include <sstream>
  11. #include <string.h>
  12. #include <string>
  13. #include <utility>
  14. #include <vector>
  15. /** String range type. */
  16. typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange;
  17. /** Callable string comparison struct. */
  18. struct cmStrCmp
  19. {
  20. cmStrCmp(std::string str)
  21. : Test_(std::move(str))
  22. {
  23. }
  24. bool operator()(cm::string_view sv) const { return Test_ == sv; }
  25. private:
  26. std::string const Test_;
  27. };
  28. template <typename Range>
  29. std::string cmJoin(Range const& r, const char* delimiter)
  30. {
  31. if (r.empty()) {
  32. return std::string();
  33. }
  34. std::ostringstream os;
  35. typedef typename Range::value_type ValueType;
  36. typedef typename Range::const_iterator InputIt;
  37. const InputIt first = r.begin();
  38. InputIt last = r.end();
  39. --last;
  40. std::copy(first, last, std::ostream_iterator<ValueType>(os, delimiter));
  41. os << *last;
  42. return os.str();
  43. }
  44. template <typename Range>
  45. std::string cmJoin(Range const& r, std::string const& delimiter)
  46. {
  47. return cmJoin(r, delimiter.c_str());
  48. }
  49. template <typename Range>
  50. std::string cmWrap(std::string const& prefix, Range const& r,
  51. std::string const& suffix, std::string const& sep)
  52. {
  53. if (r.empty()) {
  54. return std::string();
  55. }
  56. return prefix + cmJoin(r, suffix + sep + prefix) + suffix;
  57. }
  58. template <typename Range>
  59. std::string cmWrap(char prefix, Range const& r, char suffix,
  60. std::string const& sep)
  61. {
  62. return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep);
  63. }
  64. /** Returns true if string @a str starts with the character @a prefix. */
  65. inline bool cmHasPrefix(cm::string_view str, char prefix)
  66. {
  67. return !str.empty() && (str.front() == prefix);
  68. }
  69. /** Returns true if string @a str starts with string @a prefix. */
  70. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  71. {
  72. return str.compare(0, prefix.size(), prefix) == 0;
  73. }
  74. /** Returns true if string @a str starts with string @a prefix. */
  75. template <size_t N>
  76. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  77. {
  78. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  79. }
  80. /** Returns true if string @a str ends with the character @a suffix. */
  81. inline bool cmHasSuffix(cm::string_view str, char suffix)
  82. {
  83. return !str.empty() && (str.back() == suffix);
  84. }
  85. /** Returns true if string @a str ends with string @a suffix. */
  86. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  87. {
  88. return str.size() >= suffix.size() &&
  89. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  90. }
  91. /** Returns true if string @a str ends with string @a suffix. */
  92. template <size_t N>
  93. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  94. {
  95. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  96. }
  97. /** Removes an existing suffix character of from the string @a str. */
  98. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  99. {
  100. if (cmHasSuffix(str, suffix)) {
  101. str.pop_back();
  102. }
  103. }
  104. /** Removes an existing suffix string of from the string @a str. */
  105. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  106. {
  107. if (cmHasSuffix(str, suffix)) {
  108. str.resize(str.size() - suffix.size());
  109. }
  110. }
  111. #endif