From 541361a58bae487956c5b50d475edc4fcb453030 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Mon, 29 Jul 2019 14:47:10 +0200 Subject: cmStringAlgorithms: Simplify cmStrCmp using cm::string_view --- Source/cmStringAlgorithms.h | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index 149e0ad..3f88692 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -15,28 +15,21 @@ #include #include +/** String range type. */ typedef cmRange::const_iterator> cmStringRange; +/** Callable string comparison struct. */ struct cmStrCmp { - cmStrCmp(const char* test) - : m_test(test) + cmStrCmp(std::string str) + : Test_(std::move(str)) { } - cmStrCmp(std::string test) - : m_test(std::move(test)) - { - } - - 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_; }; template @@ -81,46 +74,46 @@ std::string cmWrap(char prefix, Range const& r, char suffix, return cmWrap(std::string(1, prefix), r, std::string(1, suffix), 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 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 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 +121,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)) { -- cgit v0.12 From a7d0fe9c24e111bc4f325d608b31fdf394c24602 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Mon, 29 Jul 2019 15:15:00 +0200 Subject: cmStringAlgorithms: Simplify cmJoin using cm::string_view --- Source/cmStringAlgorithms.h | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index 3f88692..8d6aec6 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -7,8 +7,6 @@ #include "cmRange.h" #include "cm_string_view.hxx" -#include -#include #include #include #include @@ -32,32 +30,25 @@ private: std::string const Test_; }; +/** Joins elements of a range with separator into a single string. */ template -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(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 -std::string cmJoin(Range const& r, std::string const& delimiter) -{ - return cmJoin(r, delimiter.c_str()); -} - -template std::string cmWrap(std::string const& prefix, Range const& r, std::string const& suffix, std::string const& sep) { -- cgit v0.12 From e5d3ea22d49e6d85082306d1f73d0689a3fbca81 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Mon, 29 Jul 2019 17:34:54 +0200 Subject: cmStringAlgorithms: Add cmCatViews and cmStrCat functions --- Source/CMakeLists.txt | 1 + Source/cmStringAlgorithms.cxx | 73 +++++++++++++++++++++++++++++++++++++++++++ Source/cmStringAlgorithms.h | 50 +++++++++++++++++++++++++++++ bootstrap | 1 + 4 files changed, 125 insertions(+) create mode 100644 Source/cmStringAlgorithms.cxx 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 +#include + +namespace { +template +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(N)) { + view = cm::string_view(digits, static_cast(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(val)); +} + +cmAlphaNum::cmAlphaNum(double val) +{ + MakeDigits(View_, Digits_, "%g", val); +} + +std::string cmCatViews(std::initializer_list 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 8d6aec6..3037bef 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -7,6 +7,7 @@ #include "cmRange.h" #include "cm_string_view.hxx" +#include #include #include #include @@ -48,6 +49,55 @@ std::string cmJoin(Range const& rng, cm::string_view separator) return os.str(); } +/** Concatenate string pieces into a single string. */ +std::string cmCatViews(std::initializer_list 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 +inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b, + AV const&... args) +{ + return cmCatViews( + { a.View(), b.View(), static_cast(args).View()... }); +} + template std::string cmWrap(std::string const& prefix, Range const& r, std::string const& suffix, std::string const& sep) diff --git a/bootstrap b/bootstrap index 7d9631c..ac5adc8 100755 --- a/bootstrap +++ b/bootstrap @@ -422,6 +422,7 @@ CMAKE_CXX_SOURCES="\ cmState \ cmStateDirectory \ cmStateSnapshot \ + cmStringAlgorithms \ cmStringReplaceHelper \ cmStringCommand \ cmSubdirCommand \ -- cgit v0.12 From 75cf7ec263034e0212554f373bb6fd02d351163f Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Wed, 31 Jul 2019 22:07:05 +0200 Subject: cmStringAlgorithms: Modernize cmWrap using cm::string_view --- Source/cmStringAlgorithms.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index 3037bef..cdb494f 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -98,21 +98,25 @@ inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b, { a.View(), b.View(), static_cast(args).View()... }); } +/** Joins wrapped elements of a range with separator into a single string. */ template -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 -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. */ -- cgit v0.12 From 4aa555f9da5541d6b1f13b0cd3ec3e7387c75c89 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Wed, 31 Jul 2019 12:44:51 +0200 Subject: Tests: Add CMakeLib.testStringAlgorithms test --- Tests/CMakeLib/CMakeLists.txt | 1 + Tests/CMakeLib/testStringAlgorithms.cxx | 134 ++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 Tests/CMakeLib/testStringAlgorithms.cxx diff --git a/Tests/CMakeLib/CMakeLists.txt b/Tests/CMakeLib/CMakeLists.txt index a25f25a..cd4dbc8 100644 --- a/Tests/CMakeLib/CMakeLists.txt +++ b/Tests/CMakeLib/CMakeLists.txt @@ -10,6 +10,7 @@ set(CMakeLib_TESTS testRST.cxx testRange.cxx testString.cxx + testStringAlgorithms.cxx testSystemTools.cxx testUTF8.cxx testXMLParser.cxx diff --git a/Tests/CMakeLib/testStringAlgorithms.cxx b/Tests/CMakeLib/testStringAlgorithms.cxx new file mode 100644 index 0000000..95616ff --- /dev/null +++ b/Tests/CMakeLib/testStringAlgorithms.cxx @@ -0,0 +1,134 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ + +#include // IWYU pragma: keep + +#include "cm_string_view.hxx" +#include +#include +#include +#include + +#include "cmStringAlgorithms.h" + +int testStringAlgorithms(int /*unused*/, char* /*unused*/ []) +{ + int failed = 0; + + auto assert_ok = [&failed](bool test, cm::string_view title) { + if (test) { + std::cout << "Passed: " << title << "\n"; + } else { + std::cout << "Failed: " << title << "\n"; + ++failed; + } + }; + + auto assert_string = [&failed](cm::string_view generated, + cm::string_view expected, + cm::string_view title) { + if (generated == expected) { + std::cout << "Passed: " << title << "\n"; + } else { + std::cout << "Failed: " << title << "\n"; + std::cout << "Expected: " << expected << "\n"; + std::cout << "Got: " << generated << "\n"; + ++failed; + } + }; + + // ---------------------------------------------------------------------- + // Test cmJoin + { + typedef std::string ST; + typedef std::vector VT; + assert_string(cmJoin(ST("abc"), ";"), "a;b;c", "cmJoin std::string"); + assert_string(cmJoin(VT{}, ";"), "", "cmJoin std::vector empty"); + assert_string(cmJoin(VT{ "a" }, ";"), "a", "cmJoin std::vector single"); + assert_string(cmJoin(VT{ "a", "b", "c" }, ";"), "a;b;c", + "cmJoin std::vector multiple"); + assert_string(cmJoin(VT{ "a", "b", "c" }, "<=>"), "a<=>b<=>c", + "cmJoin std::vector long sep"); + } + + // ---------------------------------------------------------------------- + // Test cmStrCat + { + int ni = -1100; + unsigned int nui = 1100u; + long int nli = -12000l; + unsigned long int nuli = 12000ul; + long long int nlli = -130000ll; + unsigned long long int nulli = 130000ull; + std::string val = + cmStrCat("", ni, ',', nui, ',', nli, ",", nuli, ", ", nlli, + std::string(", "), nulli, cm::string_view("")); + std::string expect = + "-1100,1100,-12000,12000, -130000, 130000"; + assert_string(val, expect, "cmStrCat strings and integers"); + } + { + float const val = 1.5f; + float const div = 0.00001f; + float f = 0.0f; + std::istringstream(cmStrCat("", val)) >> f; + f -= val; + assert_ok((f < div) && (f > -div), "cmStrCat float"); + } + { + double const val = 1.5; + double const div = 0.00001; + double d = 0.0; + std::istringstream(cmStrCat("", val)) >> d; + d -= val; + assert_ok((d < div) && (d > -div), "cmStrCat double"); + } + + // ---------------------------------------------------------------------- + // Test cmWrap + { + typedef std::vector VT; + assert_string(cmWrap("<", VT{}, ">", "; "), // + "", // + "cmWrap empty, string prefix and suffix"); + assert_string(cmWrap("<", VT{ "abc" }, ">", "; "), // + "", // + "cmWrap single, string prefix and suffix"); + assert_string(cmWrap("<", VT{ "a1", "a2", "a3" }, ">", "; "), // + "; ; ", // + "cmWrap multiple, string prefix and suffix"); + + assert_string(cmWrap('<', VT{}, '>', "; "), // + "", // + "cmWrap empty, char prefix and suffix"); + assert_string(cmWrap('<', VT{ "abc" }, '>', "; "), // + "", // + "cmWrap single, char prefix and suffix"); + assert_string(cmWrap('<', VT{ "a1", "a2", "a3" }, '>', "; "), // + "; ; ", // + "cmWrap multiple, char prefix and suffix"); + } + + // ---------------------------------------------------------------------- + // Test cmHas(Literal)Prefix and cmHas(Literal)Suffix + { + std::string str("abc"); + assert_ok(cmHasPrefix(str, 'a'), "cmHasPrefix char"); + assert_ok(!cmHasPrefix(str, 'c'), "cmHasPrefix char not"); + assert_ok(cmHasPrefix(str, "ab"), "cmHasPrefix string"); + assert_ok(!cmHasPrefix(str, "bc"), "cmHasPrefix string not"); + assert_ok(cmHasPrefix(str, str), "cmHasPrefix complete string"); + assert_ok(cmHasLiteralPrefix(str, "ab"), "cmHasLiteralPrefix string"); + assert_ok(!cmHasLiteralPrefix(str, "bc"), "cmHasLiteralPrefix string not"); + + assert_ok(cmHasSuffix(str, 'c'), "cmHasSuffix char"); + assert_ok(!cmHasSuffix(str, 'a'), "cmHasSuffix char not"); + assert_ok(cmHasSuffix(str, "bc"), "cmHasSuffix string"); + assert_ok(!cmHasSuffix(str, "ab"), "cmHasSuffix string not"); + assert_ok(cmHasSuffix(str, str), "cmHasSuffix complete string"); + assert_ok(cmHasLiteralSuffix(str, "bc"), "cmHasLiteralSuffix string"); + assert_ok(!cmHasLiteralSuffix(str, "ab"), "cmHasLiteralPrefix string not"); + } + + return failed; +} -- cgit v0.12