cmStringAlgorithms.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/optional>
  13. #include <cm/string_view>
  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. void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut,
  78. bool emptyArgs = false);
  79. inline void cmExpandList(cmValue arg, std::vector<std::string>& argsOut,
  80. bool emptyArgs = false)
  81. {
  82. if (arg) {
  83. cmExpandList(*arg, argsOut, emptyArgs);
  84. }
  85. }
  86. /**
  87. * Expand out any arguments in the string range [@a first, @a last) that have
  88. * ; separated strings into multiple arguments. All found arguments are
  89. * appended to @a argsOut.
  90. */
  91. template <class InputIt>
  92. void cmExpandLists(InputIt first, InputIt last,
  93. std::vector<std::string>& argsOut)
  94. {
  95. for (; first != last; ++first) {
  96. cmExpandList(*first, argsOut);
  97. }
  98. }
  99. /**
  100. * Same as cmExpandList but a new vector is created containing
  101. * the expanded arguments from the string @a arg.
  102. */
  103. std::vector<std::string> cmExpandedList(cm::string_view arg,
  104. bool emptyArgs = false);
  105. inline std::vector<std::string> cmExpandedList(cmValue arg,
  106. bool emptyArgs = false)
  107. {
  108. if (!arg) {
  109. return {};
  110. }
  111. return cmExpandedList(*arg, emptyArgs);
  112. }
  113. /**
  114. * Same as cmExpandList but a new vector is created containing the expanded
  115. * versions of all arguments in the string range [@a first, @a last).
  116. */
  117. template <class InputIt>
  118. std::vector<std::string> cmExpandedLists(InputIt first, InputIt last)
  119. {
  120. std::vector<std::string> argsOut;
  121. for (; first != last; ++first) {
  122. cmExpandList(*first, argsOut);
  123. }
  124. return argsOut;
  125. }
  126. /** Concatenate string pieces into a single string. */
  127. std::string cmCatViews(cm::optional<std::string>&& first,
  128. std::initializer_list<cm::string_view> views);
  129. /** Utility class for cmStrCat. */
  130. class cmAlphaNum
  131. {
  132. public:
  133. cmAlphaNum(cm::string_view view)
  134. : View_(view)
  135. {
  136. }
  137. cmAlphaNum(std::string const& str)
  138. : View_(str)
  139. {
  140. }
  141. cmAlphaNum(const char* str)
  142. : View_(str)
  143. {
  144. }
  145. cmAlphaNum(char ch)
  146. : View_(this->Digits_, 1)
  147. {
  148. this->Digits_[0] = ch;
  149. }
  150. cmAlphaNum(int val);
  151. cmAlphaNum(unsigned int val);
  152. cmAlphaNum(long int val);
  153. cmAlphaNum(unsigned long int val);
  154. cmAlphaNum(long long int val);
  155. cmAlphaNum(unsigned long long int val);
  156. cmAlphaNum(float val);
  157. cmAlphaNum(double val);
  158. cmAlphaNum(cmValue value)
  159. : View_(*value)
  160. {
  161. }
  162. cm::string_view View() const { return this->View_; }
  163. private:
  164. cm::string_view View_;
  165. char Digits_[32];
  166. };
  167. template <typename A, typename B, typename... AV>
  168. class cmStrCatHelper
  169. {
  170. public:
  171. static std::string Compute(cmAlphaNum const& a, cmAlphaNum const& b,
  172. AV const&... args)
  173. {
  174. return cmCatViews(
  175. cm::nullopt,
  176. { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... });
  177. }
  178. };
  179. template <typename B, typename... AV>
  180. class cmStrCatHelper<std::string, B, AV...>
  181. {
  182. public:
  183. static std::string Compute(std::string&& a, cmAlphaNum const& b,
  184. AV const&... args)
  185. {
  186. return cmCatViews(
  187. std::move(a),
  188. { b.View(), static_cast<cmAlphaNum const&>(args).View()... });
  189. }
  190. };
  191. /** Concatenate string pieces and numbers into a single string. */
  192. template <typename A, typename B, typename... AV>
  193. inline std::string cmStrCat(A&& a, B&& b, AV&&... args)
  194. {
  195. return cmStrCatHelper<A, B, AV...>::Compute(
  196. std::forward<A>(a), std::forward<B>(b), std::forward<AV>(args)...);
  197. }
  198. /** Joins wrapped elements of a range with separator into a single string. */
  199. template <typename Range>
  200. std::string cmWrap(cm::string_view prefix, Range const& rng,
  201. cm::string_view suffix, cm::string_view sep)
  202. {
  203. if (rng.empty()) {
  204. return std::string();
  205. }
  206. return cmCatViews(
  207. cm::nullopt,
  208. { prefix, cmJoin(rng, cmCatViews(cm::nullopt, { suffix, sep, prefix })),
  209. suffix });
  210. }
  211. /** Joins wrapped elements of a range with separator into a single string. */
  212. template <typename Range>
  213. std::string cmWrap(char prefix, Range const& rng, char suffix,
  214. cm::string_view sep)
  215. {
  216. return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
  217. sep);
  218. }
  219. /** Returns true if string @a str starts with the character @a prefix. */
  220. inline bool cmHasPrefix(cm::string_view str, char prefix)
  221. {
  222. return !str.empty() && (str.front() == prefix);
  223. }
  224. /** Returns true if string @a str starts with string @a prefix. */
  225. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  226. {
  227. return str.compare(0, prefix.size(), prefix) == 0;
  228. }
  229. /** Returns true if string @a str starts with string @a prefix. */
  230. inline bool cmHasPrefix(cm::string_view str, cmValue prefix)
  231. {
  232. if (!prefix) {
  233. return false;
  234. }
  235. return str.compare(0, prefix->size(), *prefix) == 0;
  236. }
  237. /** Returns true if string @a str starts with string @a prefix. */
  238. template <size_t N>
  239. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  240. {
  241. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  242. }
  243. /** Returns true if string @a str ends with the character @a suffix. */
  244. inline bool cmHasSuffix(cm::string_view str, char suffix)
  245. {
  246. return !str.empty() && (str.back() == suffix);
  247. }
  248. /** Returns true if string @a str ends with string @a suffix. */
  249. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  250. {
  251. return str.size() >= suffix.size() &&
  252. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  253. }
  254. /** Returns true if string @a str ends with string @a suffix. */
  255. inline bool cmHasSuffix(cm::string_view str, cmValue suffix)
  256. {
  257. if (!suffix) {
  258. return false;
  259. }
  260. return str.size() >= suffix->size() &&
  261. str.compare(str.size() - suffix->size(), suffix->size(), *suffix) == 0;
  262. }
  263. /** Returns true if string @a str ends with string @a suffix. */
  264. template <size_t N>
  265. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  266. {
  267. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  268. }
  269. /** Removes an existing suffix character of from the string @a str. */
  270. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  271. {
  272. if (cmHasSuffix(str, suffix)) {
  273. str.pop_back();
  274. }
  275. }
  276. /** Removes an existing suffix string of from the string @a str. */
  277. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  278. {
  279. if (cmHasSuffix(str, suffix)) {
  280. str.resize(str.size() - suffix.size());
  281. }
  282. }
  283. /** Converts a string to long. Expects that the whole string is an integer. */
  284. bool cmStrToLong(const char* str, long* value);
  285. bool cmStrToLong(std::string const& str, long* value);
  286. /** Converts a string to unsigned long. Expects that the whole string is an
  287. * integer */
  288. bool cmStrToULong(const char* str, unsigned long* value);
  289. bool cmStrToULong(std::string const& str, unsigned long* value);
  290. /** Converts a string to long long. Expects that the whole string
  291. * is an integer */
  292. bool cmStrToLongLong(const char* str, long long* value);
  293. bool cmStrToLongLong(std::string const& str, long long* value);
  294. /** Converts a string to unsigned long long. Expects that the whole string
  295. * is an integer */
  296. bool cmStrToULongLong(const char* str, unsigned long long* value);
  297. bool cmStrToULongLong(std::string const& str, unsigned long long* value);