cmStringAlgorithms.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /**
  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(cmValue 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(cmValue 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(
  127. std::initializer_list<std::pair<cm::string_view, std::string*>> views);
  128. /** Utility class for cmStrCat. */
  129. class cmAlphaNum
  130. {
  131. public:
  132. cmAlphaNum(cm::string_view view)
  133. : View_(view)
  134. {
  135. }
  136. cmAlphaNum(std::string const& str)
  137. : View_(str)
  138. {
  139. }
  140. cmAlphaNum(std::string&& str)
  141. : RValueString_(&str)
  142. {
  143. }
  144. cmAlphaNum(const char* str)
  145. : View_(str)
  146. {
  147. }
  148. cmAlphaNum(char ch)
  149. : View_(this->Digits_, 1)
  150. {
  151. this->Digits_[0] = ch;
  152. }
  153. cmAlphaNum(int val);
  154. cmAlphaNum(unsigned int val);
  155. cmAlphaNum(long int val);
  156. cmAlphaNum(unsigned long int val);
  157. cmAlphaNum(long long int val);
  158. cmAlphaNum(unsigned long long int val);
  159. cmAlphaNum(float val);
  160. cmAlphaNum(double val);
  161. cmAlphaNum(cmValue value)
  162. : View_(*value)
  163. {
  164. }
  165. cm::string_view View() const
  166. {
  167. if (this->RValueString_) {
  168. return *this->RValueString_;
  169. }
  170. return this->View_;
  171. }
  172. std::string* RValueString() const { return this->RValueString_; }
  173. private:
  174. std::string* RValueString_ = nullptr;
  175. cm::string_view View_;
  176. char Digits_[32];
  177. };
  178. /** Concatenate string pieces and numbers into a single string. */
  179. template <typename A, typename B, typename... AV>
  180. inline std::string cmStrCat(A&& a, B&& b, AV&&... args)
  181. {
  182. static auto const makePair =
  183. [](const cmAlphaNum& arg) -> std::pair<cm::string_view, std::string*> {
  184. return { arg.View(), arg.RValueString() };
  185. };
  186. return cmCatViews({ makePair(std::forward<A>(a)),
  187. makePair(std::forward<B>(b)),
  188. makePair(std::forward<AV>(args))... });
  189. }
  190. /** Joins wrapped elements of a range with separator into a single string. */
  191. template <typename Range>
  192. std::string cmWrap(cm::string_view prefix, Range const& rng,
  193. cm::string_view suffix, cm::string_view sep)
  194. {
  195. if (rng.empty()) {
  196. return std::string();
  197. }
  198. return cmCatViews({ { prefix, nullptr },
  199. { cmJoin(rng,
  200. cmCatViews({ { suffix, nullptr },
  201. { sep, nullptr },
  202. { prefix, nullptr } })),
  203. nullptr },
  204. { suffix, nullptr } });
  205. }
  206. /** Joins wrapped elements of a range with separator into a single string. */
  207. template <typename Range>
  208. std::string cmWrap(char prefix, Range const& rng, char suffix,
  209. cm::string_view sep)
  210. {
  211. return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
  212. sep);
  213. }
  214. /** Returns true if string @a str starts with the character @a prefix. */
  215. inline bool cmHasPrefix(cm::string_view str, char prefix)
  216. {
  217. return !str.empty() && (str.front() == prefix);
  218. }
  219. /** Returns true if string @a str starts with string @a prefix. */
  220. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  221. {
  222. return str.compare(0, prefix.size(), prefix) == 0;
  223. }
  224. /** Returns true if string @a str starts with string @a prefix. */
  225. inline bool cmHasPrefix(cm::string_view str, cmValue prefix)
  226. {
  227. if (!prefix) {
  228. return false;
  229. }
  230. return str.compare(0, prefix->size(), *prefix) == 0;
  231. }
  232. /** Returns true if string @a str starts with string @a prefix. */
  233. template <size_t N>
  234. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  235. {
  236. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  237. }
  238. /** Returns true if string @a str ends with the character @a suffix. */
  239. inline bool cmHasSuffix(cm::string_view str, char suffix)
  240. {
  241. return !str.empty() && (str.back() == suffix);
  242. }
  243. /** Returns true if string @a str ends with string @a suffix. */
  244. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  245. {
  246. return str.size() >= suffix.size() &&
  247. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  248. }
  249. /** Returns true if string @a str ends with string @a suffix. */
  250. inline bool cmHasSuffix(cm::string_view str, cmValue suffix)
  251. {
  252. if (!suffix) {
  253. return false;
  254. }
  255. return str.size() >= suffix->size() &&
  256. str.compare(str.size() - suffix->size(), suffix->size(), *suffix) == 0;
  257. }
  258. /** Returns true if string @a str ends with string @a suffix. */
  259. template <size_t N>
  260. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  261. {
  262. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  263. }
  264. /** Removes an existing suffix character of from the string @a str. */
  265. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  266. {
  267. if (cmHasSuffix(str, suffix)) {
  268. str.pop_back();
  269. }
  270. }
  271. /** Removes an existing suffix string of from the string @a str. */
  272. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  273. {
  274. if (cmHasSuffix(str, suffix)) {
  275. str.resize(str.size() - suffix.size());
  276. }
  277. }
  278. /** Converts a string to long. Expects that the whole string is an integer. */
  279. bool cmStrToLong(const char* str, long* value);
  280. bool cmStrToLong(std::string const& str, long* value);
  281. /** Converts a string to unsigned long. Expects that the whole string is an
  282. * integer */
  283. bool cmStrToULong(const char* str, unsigned long* value);
  284. bool cmStrToULong(std::string const& str, unsigned long* value);
  285. /** Converts a string to long long. Expects that the whole string
  286. * is an integer */
  287. bool cmStrToLongLong(const char* str, long long* value);
  288. bool cmStrToLongLong(std::string const& str, long long* value);
  289. /** Converts a string to unsigned long long. Expects that the whole string
  290. * is an integer */
  291. bool cmStrToULongLong(const char* str, unsigned long long* value);
  292. bool cmStrToULongLong(std::string const& str, unsigned long long* value);