cmStringAlgorithms.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cctype>
  6. #include <cstring>
  7. #include <initializer_list>
  8. #include <sstream>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/string_view>
  13. #include "cmRange.h"
  14. #include "cmValue.h"
  15. /** String range type. */
  16. using cmStringRange = cmRange<std::vector<std::string>::const_iterator>;
  17. /** Returns length of a literal string. */
  18. template <size_t N>
  19. constexpr size_t cmStrLen(const char (&/*str*/)[N])
  20. {
  21. return N - 1;
  22. }
  23. /** Callable string comparison struct. */
  24. struct cmStrCmp
  25. {
  26. cmStrCmp(std::string str)
  27. : Test_(std::move(str))
  28. {
  29. }
  30. bool operator()(cm::string_view sv) const { return this->Test_ == sv; }
  31. private:
  32. std::string const Test_;
  33. };
  34. /** Returns true if the character @a ch is a whitespace character. **/
  35. inline bool cmIsSpace(char ch)
  36. {
  37. return ((ch & 0x80) == 0) && std::isspace(ch);
  38. }
  39. /** Returns a string that has whitespace removed from the start and the end. */
  40. std::string cmTrimWhitespace(cm::string_view str);
  41. /** Returns a string that has quotes removed from the start and the end. */
  42. std::string cmRemoveQuotes(cm::string_view str);
  43. /** Escape quotes in a string. */
  44. std::string cmEscapeQuotes(cm::string_view str);
  45. /** Joins elements of a range with separator into a single string. */
  46. template <typename Range>
  47. std::string cmJoin(Range const& rng, cm::string_view separator)
  48. {
  49. if (rng.empty()) {
  50. return std::string();
  51. }
  52. std::ostringstream os;
  53. auto it = rng.begin();
  54. auto const end = rng.end();
  55. os << *it;
  56. while (++it != end) {
  57. os << separator << *it;
  58. }
  59. return os.str();
  60. }
  61. /**
  62. * Faster overloads for std::string ranges.
  63. * If @a initial is provided, it prepends the resulted string without
  64. * @a separator between them.
  65. */
  66. std::string cmJoin(std::vector<std::string> const& rng,
  67. cm::string_view separator, cm::string_view initial = {});
  68. std::string cmJoin(cmStringRange const& rng, cm::string_view separator,
  69. cm::string_view initial = {});
  70. /** Extract tokens that are separated by any of the characters in @a sep. */
  71. std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep);
  72. /** Concatenate string pieces into a single string. */
  73. std::string cmCatViews(
  74. std::initializer_list<std::pair<cm::string_view, std::string*>> views);
  75. /** Utility class for cmStrCat. */
  76. class cmAlphaNum
  77. {
  78. public:
  79. cmAlphaNum(cm::string_view view)
  80. : View_(view)
  81. {
  82. }
  83. cmAlphaNum(std::string const& str)
  84. : View_(str)
  85. {
  86. }
  87. cmAlphaNum(std::string&& str)
  88. : RValueString_(&str)
  89. {
  90. }
  91. cmAlphaNum(const char* str)
  92. : View_(str)
  93. {
  94. }
  95. cmAlphaNum(char ch)
  96. : View_(this->Digits_, 1)
  97. {
  98. this->Digits_[0] = ch;
  99. }
  100. cmAlphaNum(int val);
  101. cmAlphaNum(unsigned int val);
  102. cmAlphaNum(long int val);
  103. cmAlphaNum(unsigned long int val);
  104. cmAlphaNum(long long int val);
  105. cmAlphaNum(unsigned long long int val);
  106. cmAlphaNum(float val);
  107. cmAlphaNum(double val);
  108. cmAlphaNum(cmValue value)
  109. : View_(*value)
  110. {
  111. }
  112. cm::string_view View() const
  113. {
  114. if (this->RValueString_) {
  115. return *this->RValueString_;
  116. }
  117. return this->View_;
  118. }
  119. std::string* RValueString() const { return this->RValueString_; }
  120. private:
  121. std::string* RValueString_ = nullptr;
  122. cm::string_view View_;
  123. char Digits_[32];
  124. };
  125. /** Concatenate string pieces and numbers into a single string. */
  126. template <typename A, typename B, typename... AV>
  127. inline std::string cmStrCat(A&& a, B&& b, AV&&... args)
  128. {
  129. static auto const makePair =
  130. [](const cmAlphaNum& arg) -> std::pair<cm::string_view, std::string*> {
  131. return { arg.View(), arg.RValueString() };
  132. };
  133. return cmCatViews({ makePair(std::forward<A>(a)),
  134. makePair(std::forward<B>(b)),
  135. makePair(std::forward<AV>(args))... });
  136. }
  137. /** Joins wrapped elements of a range with separator into a single string. */
  138. template <typename Range>
  139. std::string cmWrap(cm::string_view prefix, Range const& rng,
  140. cm::string_view suffix, cm::string_view sep)
  141. {
  142. if (rng.empty()) {
  143. return std::string();
  144. }
  145. return cmCatViews({ { prefix, nullptr },
  146. { cmJoin(rng,
  147. cmCatViews({ { suffix, nullptr },
  148. { sep, nullptr },
  149. { prefix, nullptr } })),
  150. nullptr },
  151. { suffix, nullptr } });
  152. }
  153. /** Joins wrapped elements of a range with separator into a single string. */
  154. template <typename Range>
  155. std::string cmWrap(char prefix, Range const& rng, char suffix,
  156. cm::string_view sep)
  157. {
  158. return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
  159. sep);
  160. }
  161. /** Returns true if string @a str starts with the character @a prefix. */
  162. inline bool cmHasPrefix(cm::string_view str, char prefix)
  163. {
  164. return !str.empty() && (str.front() == prefix);
  165. }
  166. /** Returns true if string @a str starts with string @a prefix. */
  167. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  168. {
  169. return str.compare(0, prefix.size(), prefix) == 0;
  170. }
  171. /** Returns true if string @a str starts with string @a prefix. */
  172. inline bool cmHasPrefix(cm::string_view str, cmValue prefix)
  173. {
  174. if (!prefix) {
  175. return false;
  176. }
  177. return str.compare(0, prefix->size(), *prefix) == 0;
  178. }
  179. /** Returns true if string @a str starts with string @a prefix. */
  180. template <size_t N>
  181. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  182. {
  183. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  184. }
  185. /** Returns true if string @a str ends with the character @a suffix. */
  186. inline bool cmHasSuffix(cm::string_view str, char suffix)
  187. {
  188. return !str.empty() && (str.back() == suffix);
  189. }
  190. /** Returns true if string @a str ends with string @a suffix. */
  191. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  192. {
  193. return str.size() >= suffix.size() &&
  194. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  195. }
  196. /** Returns true if string @a str ends with string @a suffix. */
  197. inline bool cmHasSuffix(cm::string_view str, cmValue suffix)
  198. {
  199. if (!suffix) {
  200. return false;
  201. }
  202. return str.size() >= suffix->size() &&
  203. str.compare(str.size() - suffix->size(), suffix->size(), *suffix) == 0;
  204. }
  205. /** Returns true if string @a str ends with string @a suffix. */
  206. template <size_t N>
  207. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  208. {
  209. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  210. }
  211. /** Removes an existing suffix character of from the string @a str. */
  212. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  213. {
  214. if (cmHasSuffix(str, suffix)) {
  215. str.pop_back();
  216. }
  217. }
  218. /** Removes an existing suffix string of from the string @a str. */
  219. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  220. {
  221. if (cmHasSuffix(str, suffix)) {
  222. str.resize(str.size() - suffix.size());
  223. }
  224. }
  225. /** Converts a string to long. Expects that the whole string is an integer. */
  226. bool cmStrToLong(const char* str, long* value);
  227. bool cmStrToLong(std::string const& str, long* value);
  228. /** Converts a string to unsigned long. Expects that the whole string is an
  229. * integer */
  230. bool cmStrToULong(const char* str, unsigned long* value);
  231. bool cmStrToULong(std::string const& str, unsigned long* value);
  232. /** Converts a string to long long. Expects that the whole string
  233. * is an integer */
  234. bool cmStrToLongLong(const char* str, long long* value);
  235. bool cmStrToLongLong(std::string const& str, long long* value);
  236. /** Converts a string to unsigned long long. Expects that the whole string
  237. * is an integer */
  238. bool cmStrToULongLong(const char* str, unsigned long long* value);
  239. bool cmStrToULongLong(std::string const& str, unsigned long long* value);