1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
9 #include <initializer_list>
15 #include <cm/string_view>
20 /** String range type. */
21 using cmStringRange = cmRange<std::vector<std::string>::const_iterator>;
23 /** Returns length of a literal string. */
25 constexpr size_t cmStrLen(const char (&/*str*/)[N])
30 /** Callable string comparison struct. */
33 cmStrCmp(std::string str)
34 : Test_(std::move(str))
38 bool operator()(cm::string_view sv) const { return this->Test_ == sv; }
41 std::string const Test_;
44 /** Returns true if the character @a ch is a whitespace character. **/
45 inline bool cmIsSpace(char ch)
47 return ((ch & 0x80) == 0) && std::isspace(ch);
50 /** Returns a string that has whitespace removed from the start and the end. */
51 std::string cmTrimWhitespace(cm::string_view str);
53 /** Returns a string that has quotes removed from the start and the end. */
54 std::string cmRemoveQuotes(cm::string_view str);
56 /** Escape quotes in a string. */
57 std::string cmEscapeQuotes(cm::string_view str);
59 /** Joins elements of a range with separator into a single string. */
60 template <typename Range>
61 std::string cmJoin(Range const& rng, cm::string_view separator)
67 std::ostringstream os;
68 auto it = rng.begin();
69 auto const end = rng.end();
72 os << separator << *it;
78 * Faster overloads for std::string ranges.
79 * If @a initial is provided, it prepends the resulted string without
80 * @a separator between them.
82 std::string cmJoin(std::vector<std::string> const& rng,
83 cm::string_view separator, cm::string_view initial = {});
85 std::string cmJoin(cmStringRange const& rng, cm::string_view separator,
86 cm::string_view initial = {});
88 /** Extract tokens that are separated by any of the characters in @a sep. */
89 std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep);
92 * Expand the ; separated string @a arg into multiple arguments.
93 * All found arguments are appended to @a argsOut.
95 void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut,
96 bool emptyArgs = false);
97 inline void cmExpandList(cmValue arg, std::vector<std::string>& argsOut,
98 bool emptyArgs = false)
101 cmExpandList(*arg, argsOut, emptyArgs);
106 * Expand out any arguments in the string range [@a first, @a last) that have
107 * ; separated strings into multiple arguments. All found arguments are
108 * appended to @a argsOut.
110 template <class InputIt>
111 void cmExpandLists(InputIt first, InputIt last,
112 std::vector<std::string>& argsOut)
114 for (; first != last; ++first) {
115 cmExpandList(*first, argsOut);
120 * Same as cmExpandList but a new vector is created containing
121 * the expanded arguments from the string @a arg.
123 std::vector<std::string> cmExpandedList(cm::string_view arg,
124 bool emptyArgs = false);
125 inline std::vector<std::string> cmExpandedList(cmValue arg,
126 bool emptyArgs = false)
131 return cmExpandedList(*arg, emptyArgs);
135 * Same as cmExpandList but a new vector is created containing the expanded
136 * versions of all arguments in the string range [@a first, @a last).
138 template <class InputIt>
139 std::vector<std::string> cmExpandedLists(InputIt first, InputIt last)
141 std::vector<std::string> argsOut;
142 for (; first != last; ++first) {
143 cmExpandList(*first, argsOut);
148 /** Concatenate string pieces into a single string. */
149 std::string cmCatViews(std::initializer_list<cm::string_view> views);
151 /** Utility class for cmStrCat. */
155 cmAlphaNum(cm::string_view view)
159 cmAlphaNum(std::string const& str)
163 cmAlphaNum(const char* str)
168 : View_(this->Digits_, 1)
170 this->Digits_[0] = ch;
173 cmAlphaNum(unsigned int val);
174 cmAlphaNum(long int val);
175 cmAlphaNum(unsigned long int val);
176 cmAlphaNum(long long int val);
177 cmAlphaNum(unsigned long long int val);
178 cmAlphaNum(float val);
179 cmAlphaNum(double val);
180 cmAlphaNum(cmValue value)
185 cm::string_view View() const { return this->View_; }
188 cm::string_view View_;
192 /** Concatenate string pieces and numbers into a single string. */
193 template <typename... AV>
194 inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b,
198 { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... });
201 /** Joins wrapped elements of a range with separator into a single string. */
202 template <typename Range>
203 std::string cmWrap(cm::string_view prefix, Range const& rng,
204 cm::string_view suffix, cm::string_view sep)
207 return std::string();
210 { prefix, cmJoin(rng, cmCatViews({ suffix, sep, prefix })), suffix });
213 /** Joins wrapped elements of a range with separator into a single string. */
214 template <typename Range>
215 std::string cmWrap(char prefix, Range const& rng, char suffix,
218 return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
222 /** Returns true if string @a str starts with the character @a prefix. */
223 inline bool cmHasPrefix(cm::string_view str, char prefix)
225 return !str.empty() && (str.front() == prefix);
228 /** Returns true if string @a str starts with string @a prefix. */
229 inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
231 return str.compare(0, prefix.size(), prefix) == 0;
234 /** Returns true if string @a str starts with string @a prefix. */
235 inline bool cmHasPrefix(cm::string_view str, cmValue prefix)
241 return str.compare(0, prefix->size(), *prefix) == 0;
244 /** Returns true if string @a str starts with string @a prefix. */
246 inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
248 return cmHasPrefix(str, cm::string_view(prefix, N - 1));
251 /** Returns true if string @a str ends with the character @a suffix. */
252 inline bool cmHasSuffix(cm::string_view str, char suffix)
254 return !str.empty() && (str.back() == suffix);
257 /** Returns true if string @a str ends with string @a suffix. */
258 inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
260 return str.size() >= suffix.size() &&
261 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
264 /** Returns true if string @a str ends with string @a suffix. */
265 inline bool cmHasSuffix(cm::string_view str, cmValue suffix)
271 return str.size() >= suffix->size() &&
272 str.compare(str.size() - suffix->size(), suffix->size(), *suffix) == 0;
275 /** Returns true if string @a str ends with string @a suffix. */
277 inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
279 return cmHasSuffix(str, cm::string_view(suffix, N - 1));
282 /** Removes an existing suffix character of from the string @a str. */
283 inline void cmStripSuffixIfExists(std::string& str, char suffix)
285 if (cmHasSuffix(str, suffix)) {
290 /** Removes an existing suffix string of from the string @a str. */
291 inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
293 if (cmHasSuffix(str, suffix)) {
294 str.resize(str.size() - suffix.size());
298 /** Converts a string to long. Expects that the whole string is an integer. */
299 bool cmStrToLong(const char* str, long* value);
300 bool cmStrToLong(std::string const& str, long* value);
302 /** Converts a string to unsigned long. Expects that the whole string is an
304 bool cmStrToULong(const char* str, unsigned long* value);
305 bool cmStrToULong(std::string const& str, unsigned long* value);
307 /** Converts a string to long long. Expects that the whole string
309 bool cmStrToLongLong(const char* str, long long* value);
310 bool cmStrToLongLong(std::string const& str, long long* value);
312 /** Converts a string to unsigned long long. Expects that the whole string
314 bool cmStrToULongLong(const char* str, unsigned long long* value);
315 bool cmStrToULongLong(std::string const& str, unsigned long long* value);