cmStringAlgorithms.h 5.1 KB

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