cmStringAlgorithms.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmStringAlgorithms_h
  4. #define cmStringAlgorithms_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <cctype>
  7. #include <cstring>
  8. #include <initializer_list>
  9. #include <sstream>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include <cm/string_view>
  14. #include "cmRange.h"
  15. /** String range type. */
  16. using cmStringRange = cmRange<std::vector<std::string>::const_iterator>;
  17. /** Callable string comparison struct. */
  18. struct cmStrCmp
  19. {
  20. cmStrCmp(std::string str)
  21. : Test_(std::move(str))
  22. {
  23. }
  24. bool operator()(cm::string_view sv) const { return Test_ == sv; }
  25. private:
  26. std::string const Test_;
  27. };
  28. /** Returns true if the character @a ch is a whitespace character. **/
  29. inline bool cmIsSpace(char ch)
  30. {
  31. return ((ch & 0x80) == 0) && std::isspace(ch);
  32. }
  33. /** Returns a string that has whitespace removed from the start and the end. */
  34. std::string cmTrimWhitespace(cm::string_view str);
  35. /** Returns a string that has quotes removed from the start and the end. */
  36. std::string cmRemoveQuotes(cm::string_view str);
  37. /** Escape quotes in a string. */
  38. std::string cmEscapeQuotes(cm::string_view str);
  39. /** Joins elements of a range with separator into a single string. */
  40. template <typename Range>
  41. std::string cmJoin(Range const& rng, cm::string_view separator)
  42. {
  43. if (rng.empty()) {
  44. return std::string();
  45. }
  46. std::ostringstream os;
  47. auto it = rng.begin();
  48. auto const end = rng.end();
  49. os << *it;
  50. while (++it != end) {
  51. os << separator << *it;
  52. }
  53. return os.str();
  54. }
  55. /** Extract tokens that are separated by any of the characters in @a sep. */
  56. std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep);
  57. /**
  58. * Expand the ; separated string @a arg into multiple arguments.
  59. * All found arguments are appended to @a argsOut.
  60. */
  61. void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut,
  62. bool emptyArgs = false);
  63. /**
  64. * Expand out any arguments in the string range [@a first, @a last) that have
  65. * ; separated strings into multiple arguments. All found arguments are
  66. * appended to @a argsOut.
  67. */
  68. template <class InputIt>
  69. void cmExpandLists(InputIt first, InputIt last,
  70. std::vector<std::string>& argsOut)
  71. {
  72. for (; first != last; ++first) {
  73. cmExpandList(*first, argsOut);
  74. }
  75. }
  76. /**
  77. * Same as cmExpandList but a new vector is created containing
  78. * the expanded arguments from the string @a arg.
  79. */
  80. std::vector<std::string> cmExpandedList(cm::string_view arg,
  81. bool emptyArgs = false);
  82. /**
  83. * Same as cmExpandList but a new vector is created containing the expanded
  84. * versions of all arguments in the string range [@a first, @a last).
  85. */
  86. template <class InputIt>
  87. std::vector<std::string> cmExpandedLists(InputIt first, InputIt last)
  88. {
  89. std::vector<std::string> argsOut;
  90. for (; first != last; ++first) {
  91. cmExpandList(*first, argsOut);
  92. }
  93. return argsOut;
  94. }
  95. /** Concatenate string pieces into a single string. */
  96. std::string cmCatViews(std::initializer_list<cm::string_view> views);
  97. /** Utility class for cmStrCat. */
  98. class cmAlphaNum
  99. {
  100. public:
  101. cmAlphaNum(cm::string_view view)
  102. : View_(view)
  103. {
  104. }
  105. cmAlphaNum(std::string const& str)
  106. : View_(str)
  107. {
  108. }
  109. cmAlphaNum(const char* str)
  110. : View_(str)
  111. {
  112. }
  113. cmAlphaNum(char ch)
  114. : View_(Digits_, 1)
  115. {
  116. Digits_[0] = ch;
  117. }
  118. cmAlphaNum(int val);
  119. cmAlphaNum(unsigned int val);
  120. cmAlphaNum(long int val);
  121. cmAlphaNum(unsigned long int val);
  122. cmAlphaNum(long long int val);
  123. cmAlphaNum(unsigned long long int val);
  124. cmAlphaNum(float val);
  125. cmAlphaNum(double val);
  126. cm::string_view View() const { return View_; }
  127. private:
  128. cm::string_view View_;
  129. char Digits_[32];
  130. };
  131. /** Concatenate string pieces and numbers into a single string. */
  132. template <typename... AV>
  133. inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b,
  134. AV const&... args)
  135. {
  136. return cmCatViews(
  137. { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... });
  138. }
  139. /** Joins wrapped elements of a range with separator into a single string. */
  140. template <typename Range>
  141. std::string cmWrap(cm::string_view prefix, Range const& rng,
  142. cm::string_view suffix, cm::string_view sep)
  143. {
  144. if (rng.empty()) {
  145. return std::string();
  146. }
  147. return cmCatViews(
  148. { prefix, cmJoin(rng, cmCatViews({ suffix, sep, prefix })), suffix });
  149. }
  150. /** Joins wrapped elements of a range with separator into a single string. */
  151. template <typename Range>
  152. std::string cmWrap(char prefix, Range const& rng, char suffix,
  153. cm::string_view sep)
  154. {
  155. return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
  156. sep);
  157. }
  158. /**
  159. * Does a string indicates that CMake/CPack/CTest internally
  160. * forced this value. This is not the same as On, but this
  161. * may be considered as "internally switched on".
  162. */
  163. bool cmIsInternallyOn(cm::string_view val);
  164. inline bool cmIsInternallyOn(const char* val)
  165. {
  166. if (!val) {
  167. return false;
  168. }
  169. return cmIsInternallyOn(cm::string_view(val));
  170. }
  171. /** Return true if value is NOTFOUND or ends in -NOTFOUND. */
  172. bool cmIsNOTFOUND(cm::string_view val);
  173. /**
  174. * Does a string indicate a true or ON value? This is not the same as ifdef.
  175. */
  176. bool cmIsOn(cm::string_view val);
  177. inline bool cmIsOn(const char* val)
  178. {
  179. if (!val) {
  180. return false;
  181. }
  182. return cmIsOn(cm::string_view(val));
  183. }
  184. /**
  185. * Does a string indicate a false or off value ? Note that this is
  186. * not the same as !IsOn(...) because there are a number of
  187. * ambiguous values such as "/usr/local/bin" a path will result in
  188. * IsON and IsOff both returning false. Note that the special path
  189. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  190. */
  191. bool cmIsOff(cm::string_view val);
  192. inline bool cmIsOff(const char* val)
  193. {
  194. if (!val) {
  195. return true;
  196. }
  197. return cmIsOff(cm::string_view(val));
  198. }
  199. /** Returns true if string @a str starts with the character @a prefix. */
  200. inline bool cmHasPrefix(cm::string_view str, char prefix)
  201. {
  202. return !str.empty() && (str.front() == prefix);
  203. }
  204. /** Returns true if string @a str starts with string @a prefix. */
  205. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  206. {
  207. return str.compare(0, prefix.size(), prefix) == 0;
  208. }
  209. /** Returns true if string @a str starts with string @a prefix. */
  210. template <size_t N>
  211. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  212. {
  213. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  214. }
  215. /** Returns true if string @a str ends with the character @a suffix. */
  216. inline bool cmHasSuffix(cm::string_view str, char suffix)
  217. {
  218. return !str.empty() && (str.back() == suffix);
  219. }
  220. /** Returns true if string @a str ends with string @a suffix. */
  221. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  222. {
  223. return str.size() >= suffix.size() &&
  224. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  225. }
  226. /** Returns true if string @a str ends with string @a suffix. */
  227. template <size_t N>
  228. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  229. {
  230. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  231. }
  232. /** Removes an existing suffix character of from the string @a str. */
  233. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  234. {
  235. if (cmHasSuffix(str, suffix)) {
  236. str.pop_back();
  237. }
  238. }
  239. /** Removes an existing suffix string of from the string @a str. */
  240. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  241. {
  242. if (cmHasSuffix(str, suffix)) {
  243. str.resize(str.size() - suffix.size());
  244. }
  245. }
  246. /** Converts a string to long. Expects that the whole string is an integer. */
  247. bool cmStrToLong(const char* str, long* value);
  248. bool cmStrToLong(std::string const& str, long* value);
  249. /** Converts a string to unsigned long. Expects that the whole string is an
  250. * integer */
  251. bool cmStrToULong(const char* str, unsigned long* value);
  252. bool cmStrToULong(std::string const& str, unsigned long* value);
  253. #endif