cmStringAlgorithms.h 3.6 KB

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