cmStringAlgorithms.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <initializer_list>
  9. #include <sstream>
  10. #include <string.h>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. /** String range type. */
  15. typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange;
  16. /** Callable string comparison struct. */
  17. struct cmStrCmp
  18. {
  19. cmStrCmp(std::string str)
  20. : Test_(std::move(str))
  21. {
  22. }
  23. bool operator()(cm::string_view sv) const { return Test_ == sv; }
  24. private:
  25. std::string const Test_;
  26. };
  27. /** Joins elements of a range with separator into a single string. */
  28. template <typename Range>
  29. std::string cmJoin(Range const& rng, cm::string_view separator)
  30. {
  31. if (rng.empty()) {
  32. return std::string();
  33. }
  34. std::ostringstream os;
  35. auto it = rng.begin();
  36. auto const end = rng.end();
  37. os << *it;
  38. while (++it != end) {
  39. os << separator << *it;
  40. }
  41. return os.str();
  42. }
  43. /** Concatenate string pieces into a single string. */
  44. std::string cmCatViews(std::initializer_list<cm::string_view> views);
  45. /** Utility class for cmStrCat. */
  46. class cmAlphaNum
  47. {
  48. public:
  49. cmAlphaNum(cm::string_view view)
  50. : View_(view)
  51. {
  52. }
  53. cmAlphaNum(std::string const& str)
  54. : View_(str)
  55. {
  56. }
  57. cmAlphaNum(const char* str)
  58. : View_(str)
  59. {
  60. }
  61. cmAlphaNum(char ch)
  62. : View_(Digits_, 1)
  63. {
  64. Digits_[0] = ch;
  65. }
  66. cmAlphaNum(int val);
  67. cmAlphaNum(unsigned int val);
  68. cmAlphaNum(long int val);
  69. cmAlphaNum(unsigned long int val);
  70. cmAlphaNum(long long int val);
  71. cmAlphaNum(unsigned long long int val);
  72. cmAlphaNum(float val);
  73. cmAlphaNum(double val);
  74. cm::string_view View() const { return View_; }
  75. private:
  76. cm::string_view View_;
  77. char Digits_[32];
  78. };
  79. /** Concatenate string pieces and numbers into a single string. */
  80. template <typename... AV>
  81. inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b,
  82. AV const&... args)
  83. {
  84. return cmCatViews(
  85. { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... });
  86. }
  87. /** Joins wrapped elements of a range with separator into a single string. */
  88. template <typename Range>
  89. std::string cmWrap(cm::string_view prefix, Range const& rng,
  90. cm::string_view suffix, cm::string_view sep)
  91. {
  92. if (rng.empty()) {
  93. return std::string();
  94. }
  95. return cmCatViews(
  96. { prefix, cmJoin(rng, cmCatViews({ suffix, sep, prefix })), suffix });
  97. }
  98. /** Joins wrapped elements of a range with separator into a single string. */
  99. template <typename Range>
  100. std::string cmWrap(char prefix, Range const& rng, char suffix,
  101. cm::string_view sep)
  102. {
  103. return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
  104. sep);
  105. }
  106. /** Returns true if string @a str starts with the character @a prefix. */
  107. inline bool cmHasPrefix(cm::string_view str, char prefix)
  108. {
  109. return !str.empty() && (str.front() == prefix);
  110. }
  111. /** Returns true if string @a str starts with string @a prefix. */
  112. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  113. {
  114. return str.compare(0, prefix.size(), prefix) == 0;
  115. }
  116. /** Returns true if string @a str starts with string @a prefix. */
  117. template <size_t N>
  118. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  119. {
  120. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  121. }
  122. /** Returns true if string @a str ends with the character @a suffix. */
  123. inline bool cmHasSuffix(cm::string_view str, char suffix)
  124. {
  125. return !str.empty() && (str.back() == suffix);
  126. }
  127. /** Returns true if string @a str ends with string @a suffix. */
  128. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  129. {
  130. return str.size() >= suffix.size() &&
  131. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  132. }
  133. /** Returns true if string @a str ends with string @a suffix. */
  134. template <size_t N>
  135. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  136. {
  137. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  138. }
  139. /** Removes an existing suffix character of from the string @a str. */
  140. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  141. {
  142. if (cmHasSuffix(str, suffix)) {
  143. str.pop_back();
  144. }
  145. }
  146. /** Removes an existing suffix string of from the string @a str. */
  147. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  148. {
  149. if (cmHasSuffix(str, suffix)) {
  150. str.resize(str.size() - suffix.size());
  151. }
  152. }
  153. #endif