1
0

cmStringAlgorithms.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <cctype>
  9. #include <initializer_list>
  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. /** Returns true if the character @a ch is a whitespace character. **/
  29. inline bool cmIsSpace(char ch)
  30. {
  31. return ((ch & 0x80) == 0) && std::isspace(ch);
  32. }
  33. /** Returns a string that has whitespace removed from the start and the end. */
  34. std::string cmTrimWhitespace(cm::string_view str);
  35. /** Returns a string that has quotes removed from the start and the end. */
  36. std::string cmRemoveQuotes(cm::string_view str);
  37. /** Escape quotes in a string. */
  38. std::string cmEscapeQuotes(cm::string_view str);
  39. /** Joins elements of a range with separator into a single string. */
  40. template <typename Range>
  41. std::string cmJoin(Range const& rng, cm::string_view separator)
  42. {
  43. if (rng.empty()) {
  44. return std::string();
  45. }
  46. std::ostringstream os;
  47. auto it = rng.begin();
  48. auto const end = rng.end();
  49. os << *it;
  50. while (++it != end) {
  51. os << separator << *it;
  52. }
  53. return os.str();
  54. }
  55. /** Extract tokens that are separated by any of the characters in @a sep. */
  56. std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep);
  57. /** Concatenate string pieces into a single string. */
  58. std::string cmCatViews(std::initializer_list<cm::string_view> views);
  59. /** Utility class for cmStrCat. */
  60. class cmAlphaNum
  61. {
  62. public:
  63. cmAlphaNum(cm::string_view view)
  64. : View_(view)
  65. {
  66. }
  67. cmAlphaNum(std::string const& str)
  68. : View_(str)
  69. {
  70. }
  71. cmAlphaNum(const char* str)
  72. : View_(str)
  73. {
  74. }
  75. cmAlphaNum(char ch)
  76. : View_(Digits_, 1)
  77. {
  78. Digits_[0] = ch;
  79. }
  80. cmAlphaNum(int val);
  81. cmAlphaNum(unsigned int val);
  82. cmAlphaNum(long int val);
  83. cmAlphaNum(unsigned long int val);
  84. cmAlphaNum(long long int val);
  85. cmAlphaNum(unsigned long long int val);
  86. cmAlphaNum(float val);
  87. cmAlphaNum(double val);
  88. cm::string_view View() const { return View_; }
  89. private:
  90. cm::string_view View_;
  91. char Digits_[32];
  92. };
  93. /** Concatenate string pieces and numbers into a single string. */
  94. template <typename... AV>
  95. inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b,
  96. AV const&... args)
  97. {
  98. return cmCatViews(
  99. { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... });
  100. }
  101. /** Joins wrapped elements of a range with separator into a single string. */
  102. template <typename Range>
  103. std::string cmWrap(cm::string_view prefix, Range const& rng,
  104. cm::string_view suffix, cm::string_view sep)
  105. {
  106. if (rng.empty()) {
  107. return std::string();
  108. }
  109. return cmCatViews(
  110. { prefix, cmJoin(rng, cmCatViews({ suffix, sep, prefix })), suffix });
  111. }
  112. /** Joins wrapped elements of a range with separator into a single string. */
  113. template <typename Range>
  114. std::string cmWrap(char prefix, Range const& rng, char suffix,
  115. cm::string_view sep)
  116. {
  117. return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
  118. sep);
  119. }
  120. /** Returns true if string @a str starts with the character @a prefix. */
  121. inline bool cmHasPrefix(cm::string_view str, char prefix)
  122. {
  123. return !str.empty() && (str.front() == prefix);
  124. }
  125. /** Returns true if string @a str starts with string @a prefix. */
  126. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  127. {
  128. return str.compare(0, prefix.size(), prefix) == 0;
  129. }
  130. /** Returns true if string @a str starts with string @a prefix. */
  131. template <size_t N>
  132. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  133. {
  134. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  135. }
  136. /** Returns true if string @a str ends with the character @a suffix. */
  137. inline bool cmHasSuffix(cm::string_view str, char suffix)
  138. {
  139. return !str.empty() && (str.back() == suffix);
  140. }
  141. /** Returns true if string @a str ends with string @a suffix. */
  142. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  143. {
  144. return str.size() >= suffix.size() &&
  145. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  146. }
  147. /** Returns true if string @a str ends with string @a suffix. */
  148. template <size_t N>
  149. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  150. {
  151. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  152. }
  153. /** Removes an existing suffix character of from the string @a str. */
  154. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  155. {
  156. if (cmHasSuffix(str, suffix)) {
  157. str.pop_back();
  158. }
  159. }
  160. /** Removes an existing suffix string of from the string @a str. */
  161. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  162. {
  163. if (cmHasSuffix(str, suffix)) {
  164. str.resize(str.size() - suffix.size());
  165. }
  166. }
  167. #endif