cmStringAlgorithms.h 9.0 KB

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