cmStringAlgorithms.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "cmProperty.h"
  14. #include "cmRange.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. /**
  73. * Expand the ; separated string @a arg into multiple arguments.
  74. * All found arguments are appended to @a argsOut.
  75. */
  76. void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut,
  77. bool emptyArgs = false);
  78. inline void cmExpandList(cmProp arg, std::vector<std::string>& argsOut,
  79. bool emptyArgs = false)
  80. {
  81. if (arg) {
  82. cmExpandList(*arg, argsOut, emptyArgs);
  83. }
  84. }
  85. /**
  86. * Expand out any arguments in the string range [@a first, @a last) that have
  87. * ; separated strings into multiple arguments. All found arguments are
  88. * appended to @a argsOut.
  89. */
  90. template <class InputIt>
  91. void cmExpandLists(InputIt first, InputIt last,
  92. std::vector<std::string>& argsOut)
  93. {
  94. for (; first != last; ++first) {
  95. cmExpandList(*first, argsOut);
  96. }
  97. }
  98. /**
  99. * Same as cmExpandList but a new vector is created containing
  100. * the expanded arguments from the string @a arg.
  101. */
  102. std::vector<std::string> cmExpandedList(cm::string_view arg,
  103. bool emptyArgs = false);
  104. inline std::vector<std::string> cmExpandedList(cmProp arg,
  105. bool emptyArgs = false)
  106. {
  107. if (!arg) {
  108. return {};
  109. }
  110. return cmExpandedList(*arg, emptyArgs);
  111. }
  112. /**
  113. * Same as cmExpandList but a new vector is created containing the expanded
  114. * versions of all arguments in the string range [@a first, @a last).
  115. */
  116. template <class InputIt>
  117. std::vector<std::string> cmExpandedLists(InputIt first, InputIt last)
  118. {
  119. std::vector<std::string> argsOut;
  120. for (; first != last; ++first) {
  121. cmExpandList(*first, argsOut);
  122. }
  123. return argsOut;
  124. }
  125. /** Concatenate string pieces into a single string. */
  126. std::string cmCatViews(std::initializer_list<cm::string_view> views);
  127. /** Utility class for cmStrCat. */
  128. class cmAlphaNum
  129. {
  130. public:
  131. cmAlphaNum(cm::string_view view)
  132. : View_(view)
  133. {
  134. }
  135. cmAlphaNum(std::string const& str)
  136. : View_(str)
  137. {
  138. }
  139. cmAlphaNum(const char* str)
  140. : View_(str)
  141. {
  142. }
  143. cmAlphaNum(char ch)
  144. : View_(this->Digits_, 1)
  145. {
  146. this->Digits_[0] = ch;
  147. }
  148. cmAlphaNum(int val);
  149. cmAlphaNum(unsigned int val);
  150. cmAlphaNum(long int val);
  151. cmAlphaNum(unsigned long int val);
  152. cmAlphaNum(long long int val);
  153. cmAlphaNum(unsigned long long int val);
  154. cmAlphaNum(float val);
  155. cmAlphaNum(double val);
  156. cmAlphaNum(cmProp value)
  157. : View_(*value)
  158. {
  159. }
  160. cm::string_view View() const { return this->View_; }
  161. private:
  162. cm::string_view View_;
  163. char Digits_[32];
  164. };
  165. /** Concatenate string pieces and numbers into a single string. */
  166. template <typename... AV>
  167. inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b,
  168. AV const&... args)
  169. {
  170. return cmCatViews(
  171. { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... });
  172. }
  173. /** Joins wrapped elements of a range with separator into a single string. */
  174. template <typename Range>
  175. std::string cmWrap(cm::string_view prefix, Range const& rng,
  176. cm::string_view suffix, cm::string_view sep)
  177. {
  178. if (rng.empty()) {
  179. return std::string();
  180. }
  181. return cmCatViews(
  182. { prefix, cmJoin(rng, cmCatViews({ suffix, sep, prefix })), suffix });
  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. /**
  193. * Does a string indicates that CMake/CPack/CTest internally
  194. * forced this value. This is not the same as On, but this
  195. * may be considered as "internally switched on".
  196. */
  197. bool cmIsInternallyOn(cm::string_view val);
  198. inline bool cmIsInternallyOn(const char* val)
  199. {
  200. if (!val) {
  201. return false;
  202. }
  203. return cmIsInternallyOn(cm::string_view(val));
  204. }
  205. inline bool cmIsInternallyOn(cmProp val)
  206. {
  207. if (!val) {
  208. return false;
  209. }
  210. return cmIsInternallyOn(*val);
  211. }
  212. /** Check for non-empty Property/Variable value. */
  213. inline bool cmNonempty(cm::string_view val)
  214. {
  215. return !cmProp::IsEmpty(val);
  216. }
  217. inline bool cmNonempty(const char* val)
  218. {
  219. return !cmProp::IsEmpty(val);
  220. }
  221. inline bool cmNonempty(cmProp val)
  222. {
  223. return !val.IsEmpty();
  224. }
  225. /** Return true if value is NOTFOUND or ends in -NOTFOUND. */
  226. inline bool cmIsNOTFOUND(cm::string_view val)
  227. {
  228. return cmProp::IsNOTFOUND(val);
  229. }
  230. inline bool cmIsNOTFOUND(cmProp val)
  231. {
  232. return val.IsNOTFOUND();
  233. }
  234. /**
  235. * Does a string indicate a true or ON value? This is not the same as ifdef.
  236. */
  237. inline bool cmIsOn(cm::string_view val)
  238. {
  239. return cmProp::IsOn(val);
  240. }
  241. inline bool cmIsOn(const char* val)
  242. {
  243. return cmProp::IsOn(val);
  244. }
  245. inline bool cmIsOn(cmProp val)
  246. {
  247. return val.IsOn();
  248. }
  249. /**
  250. * Does a string indicate a false or off value ? Note that this is
  251. * not the same as !IsOn(...) because there are a number of
  252. * ambiguous values such as "/usr/local/bin" a path will result in
  253. * IsON and IsOff both returning false. Note that the special path
  254. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  255. */
  256. inline bool cmIsOff(cm::string_view val)
  257. {
  258. return cmProp::IsOff(val);
  259. }
  260. inline bool cmIsOff(const char* val)
  261. {
  262. return cmProp::IsOff(val);
  263. }
  264. inline bool cmIsOff(cmProp val)
  265. {
  266. return val.IsOff();
  267. }
  268. /** Returns true if string @a str starts with the character @a prefix. */
  269. inline bool cmHasPrefix(cm::string_view str, char prefix)
  270. {
  271. return !str.empty() && (str.front() == prefix);
  272. }
  273. /** Returns true if string @a str starts with string @a prefix. */
  274. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  275. {
  276. return str.compare(0, prefix.size(), prefix) == 0;
  277. }
  278. /** Returns true if string @a str starts with string @a prefix. */
  279. inline bool cmHasPrefix(cm::string_view str, cmProp prefix)
  280. {
  281. if (!prefix) {
  282. return false;
  283. }
  284. return str.compare(0, prefix->size(), *prefix) == 0;
  285. }
  286. /** Returns true if string @a str starts with string @a prefix. */
  287. template <size_t N>
  288. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  289. {
  290. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  291. }
  292. /** Returns true if string @a str ends with the character @a suffix. */
  293. inline bool cmHasSuffix(cm::string_view str, char suffix)
  294. {
  295. return !str.empty() && (str.back() == suffix);
  296. }
  297. /** Returns true if string @a str ends with string @a suffix. */
  298. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  299. {
  300. return str.size() >= suffix.size() &&
  301. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  302. }
  303. /** Returns true if string @a str ends with string @a suffix. */
  304. inline bool cmHasSuffix(cm::string_view str, cmProp suffix)
  305. {
  306. if (!suffix) {
  307. return false;
  308. }
  309. return str.size() >= suffix->size() &&
  310. str.compare(str.size() - suffix->size(), suffix->size(), *suffix) == 0;
  311. }
  312. /** Returns true if string @a str ends with string @a suffix. */
  313. template <size_t N>
  314. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  315. {
  316. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  317. }
  318. /** Removes an existing suffix character of from the string @a str. */
  319. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  320. {
  321. if (cmHasSuffix(str, suffix)) {
  322. str.pop_back();
  323. }
  324. }
  325. /** Removes an existing suffix string of from the string @a str. */
  326. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  327. {
  328. if (cmHasSuffix(str, suffix)) {
  329. str.resize(str.size() - suffix.size());
  330. }
  331. }
  332. /** Converts a string to long. Expects that the whole string is an integer. */
  333. bool cmStrToLong(const char* str, long* value);
  334. bool cmStrToLong(std::string const& str, long* value);
  335. /** Converts a string to unsigned long. Expects that the whole string is an
  336. * integer */
  337. bool cmStrToULong(const char* str, unsigned long* value);
  338. bool cmStrToULong(std::string const& str, unsigned long* value);