1
0

cmStringAlgorithms.h 8.9 KB

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