diff options
author | Brad King <brad.king@kitware.com> | 2019-08-02 14:55:11 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2019-08-02 14:55:39 (GMT) |
commit | 94f7d19a983a4d5d9efc5e21d167a0a6a4ba18fa (patch) | |
tree | 9c45537aee148518c6fe7e281a59bbd1dfe8ba33 /Source | |
parent | 97d9876c78803879f3cd08a65dec9420873fcc92 (diff) | |
parent | 4aa555f9da5541d6b1f13b0cd3ec3e7387c75c89 (diff) | |
download | CMake-94f7d19a983a4d5d9efc5e21d167a0a6a4ba18fa.zip CMake-94f7d19a983a4d5d9efc5e21d167a0a6a4ba18fa.tar.gz CMake-94f7d19a983a4d5d9efc5e21d167a0a6a4ba18fa.tar.bz2 |
Merge topic 'cmStringAlgorithms_modernize'
4aa555f9da Tests: Add CMakeLib.testStringAlgorithms test
75cf7ec263 cmStringAlgorithms: Modernize cmWrap using cm::string_view
e5d3ea22d4 cmStringAlgorithms: Add cmCatViews and cmStrCat functions
a7d0fe9c24 cmStringAlgorithms: Simplify cmJoin using cm::string_view
541361a58b cmStringAlgorithms: Simplify cmStrCmp using cm::string_view
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !3628
Diffstat (limited to 'Source')
-rw-r--r-- | Source/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Source/cmStringAlgorithms.cxx | 73 | ||||
-rw-r--r-- | Source/cmStringAlgorithms.h | 126 |
3 files changed, 156 insertions, 44 deletions
diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 041c606..fe40af3 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -404,6 +404,7 @@ set(SRCS cmStateSnapshot.cxx cmStateSnapshot.h cmStateTypes.h + cmStringAlgorithms.cxx cmStringAlgorithms.h cmSystemTools.cxx cmSystemTools.h diff --git a/Source/cmStringAlgorithms.cxx b/Source/cmStringAlgorithms.cxx new file mode 100644 index 0000000..5deb9b0 --- /dev/null +++ b/Source/cmStringAlgorithms.cxx @@ -0,0 +1,73 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "cmStringAlgorithms.h" + +#include <algorithm> +#include <cstdio> + +namespace { +template <std::size_t N, typename T> +inline void MakeDigits(cm::string_view& view, char (&digits)[N], + const char* pattern, T value) +{ + int res = std::snprintf(digits, N, pattern, value); + if (res > 0 && res < static_cast<int>(N)) { + view = cm::string_view(digits, static_cast<std::size_t>(res)); + } +} +} // unnamed namespace + +cmAlphaNum::cmAlphaNum(int val) +{ + MakeDigits(View_, Digits_, "%i", val); +} + +cmAlphaNum::cmAlphaNum(unsigned int val) +{ + MakeDigits(View_, Digits_, "%u", val); +} + +cmAlphaNum::cmAlphaNum(long int val) +{ + MakeDigits(View_, Digits_, "%li", val); +} + +cmAlphaNum::cmAlphaNum(unsigned long int val) +{ + MakeDigits(View_, Digits_, "%lu", val); +} + +cmAlphaNum::cmAlphaNum(long long int val) +{ + MakeDigits(View_, Digits_, "%lli", val); +} + +cmAlphaNum::cmAlphaNum(unsigned long long int val) +{ + MakeDigits(View_, Digits_, "%llu", val); +} + +cmAlphaNum::cmAlphaNum(float val) +{ + MakeDigits(View_, Digits_, "%g", static_cast<double>(val)); +} + +cmAlphaNum::cmAlphaNum(double val) +{ + MakeDigits(View_, Digits_, "%g", val); +} + +std::string cmCatViews(std::initializer_list<cm::string_view> views) +{ + std::size_t total_size = 0; + for (cm::string_view const& view : views) { + total_size += view.size(); + } + + std::string result(total_size, '\0'); + std::string::iterator sit = result.begin(); + for (cm::string_view const& view : views) { + sit = std::copy_n(view.data(), view.size(), sit); + } + return result; +} diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index 149e0ad..cdb494f 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -7,120 +7,158 @@ #include "cmRange.h" #include "cm_string_view.hxx" -#include <algorithm> -#include <iterator> +#include <initializer_list> #include <sstream> #include <string.h> #include <string> #include <utility> #include <vector> +/** String range type. */ typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange; +/** Callable string comparison struct. */ struct cmStrCmp { - cmStrCmp(const char* test) - : m_test(test) - { - } - cmStrCmp(std::string test) - : m_test(std::move(test)) + cmStrCmp(std::string str) + : Test_(std::move(str)) { } - bool operator()(const std::string& input) const { return m_test == input; } - - bool operator()(const char* input) const - { - return strcmp(input, m_test.c_str()) == 0; - } + bool operator()(cm::string_view sv) const { return Test_ == sv; } private: - const std::string m_test; + std::string const Test_; }; +/** Joins elements of a range with separator into a single string. */ template <typename Range> -std::string cmJoin(Range const& r, const char* delimiter) +std::string cmJoin(Range const& rng, cm::string_view separator) { - if (r.empty()) { + if (rng.empty()) { return std::string(); } - std::ostringstream os; - typedef typename Range::value_type ValueType; - typedef typename Range::const_iterator InputIt; - const InputIt first = r.begin(); - InputIt last = r.end(); - --last; - std::copy(first, last, std::ostream_iterator<ValueType>(os, delimiter)); - - os << *last; + std::ostringstream os; + auto it = rng.begin(); + auto const end = rng.end(); + os << *it; + while (++it != end) { + os << separator << *it; + } return os.str(); } -template <typename Range> -std::string cmJoin(Range const& r, std::string const& delimiter) +/** Concatenate string pieces into a single string. */ +std::string cmCatViews(std::initializer_list<cm::string_view> views); + +/** Utility class for cmStrCat. */ +class cmAlphaNum +{ +public: + cmAlphaNum(cm::string_view view) + : View_(view) + { + } + cmAlphaNum(std::string const& str) + : View_(str) + { + } + cmAlphaNum(const char* str) + : View_(str) + { + } + cmAlphaNum(char ch) + : View_(Digits_, 1) + { + Digits_[0] = ch; + } + cmAlphaNum(int val); + cmAlphaNum(unsigned int val); + cmAlphaNum(long int val); + cmAlphaNum(unsigned long int val); + cmAlphaNum(long long int val); + cmAlphaNum(unsigned long long int val); + cmAlphaNum(float val); + cmAlphaNum(double val); + + cm::string_view View() const { return View_; } + +private: + cm::string_view View_; + char Digits_[32]; +}; + +/** Concatenate string pieces and numbers into a single string. */ +template <typename... AV> +inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b, + AV const&... args) { - return cmJoin(r, delimiter.c_str()); + return cmCatViews( + { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... }); } +/** Joins wrapped elements of a range with separator into a single string. */ template <typename Range> -std::string cmWrap(std::string const& prefix, Range const& r, - std::string const& suffix, std::string const& sep) +std::string cmWrap(cm::string_view prefix, Range const& rng, + cm::string_view suffix, cm::string_view sep) { - if (r.empty()) { + if (rng.empty()) { return std::string(); } - return prefix + cmJoin(r, suffix + sep + prefix) + suffix; + return cmCatViews( + { prefix, cmJoin(rng, cmCatViews({ suffix, sep, prefix })), suffix }); } +/** Joins wrapped elements of a range with separator into a single string. */ template <typename Range> -std::string cmWrap(char prefix, Range const& r, char suffix, - std::string const& sep) +std::string cmWrap(char prefix, Range const& rng, char suffix, + cm::string_view sep) { - return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep); + return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1), + sep); } -/** Returns true if string @a str starts with the character @a prefix. **/ +/** Returns true if string @a str starts with the character @a prefix. */ inline bool cmHasPrefix(cm::string_view str, char prefix) { return !str.empty() && (str.front() == prefix); } -/** Returns true if string @a str starts with string @a prefix. **/ +/** Returns true if string @a str starts with string @a prefix. */ inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix) { return str.compare(0, prefix.size(), prefix) == 0; } -/** Returns true if string @a str starts with string @a prefix. **/ +/** Returns true if string @a str starts with string @a prefix. */ template <size_t N> inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N]) { return cmHasPrefix(str, cm::string_view(prefix, N - 1)); } -/** Returns true if string @a str ends with the character @a suffix. **/ +/** Returns true if string @a str ends with the character @a suffix. */ inline bool cmHasSuffix(cm::string_view str, char suffix) { return !str.empty() && (str.back() == suffix); } -/** Returns true if string @a str ends with string @a suffix. **/ +/** Returns true if string @a str ends with string @a suffix. */ inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; } -/** Returns true if string @a str ends with string @a suffix. **/ +/** Returns true if string @a str ends with string @a suffix. */ template <size_t N> inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N]) { return cmHasSuffix(str, cm::string_view(suffix, N - 1)); } -/** Removes an existing suffix character of from the string @a str. **/ +/** Removes an existing suffix character of from the string @a str. */ inline void cmStripSuffixIfExists(std::string& str, char suffix) { if (cmHasSuffix(str, suffix)) { @@ -128,7 +166,7 @@ inline void cmStripSuffixIfExists(std::string& str, char suffix) } } -/** Removes an existing suffix string of from the string @a str. **/ +/** Removes an existing suffix string of from the string @a str. */ inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix) { if (cmHasSuffix(str, suffix)) { |