diff options
author | Sebastian Holtermann <sebholt@xwmw.org> | 2019-07-29 15:34:54 (GMT) |
---|---|---|
committer | Sebastian Holtermann <sebholt@xwmw.org> | 2019-08-01 09:45:04 (GMT) |
commit | e5d3ea22d49e6d85082306d1f73d0689a3fbca81 (patch) | |
tree | 49cb6cfcea887454bddab8e6cc79883a3351139d | |
parent | a7d0fe9c24e111bc4f325d608b31fdf394c24602 (diff) | |
download | CMake-e5d3ea22d49e6d85082306d1f73d0689a3fbca81.zip CMake-e5d3ea22d49e6d85082306d1f73d0689a3fbca81.tar.gz CMake-e5d3ea22d49e6d85082306d1f73d0689a3fbca81.tar.bz2 |
cmStringAlgorithms: Add cmCatViews and cmStrCat functions
-rw-r--r-- | Source/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Source/cmStringAlgorithms.cxx | 73 | ||||
-rw-r--r-- | Source/cmStringAlgorithms.h | 50 | ||||
-rwxr-xr-x | bootstrap | 1 |
4 files changed, 125 insertions, 0 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 8d6aec6..3037bef 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -7,6 +7,7 @@ #include "cmRange.h" #include "cm_string_view.hxx" +#include <initializer_list> #include <sstream> #include <string.h> #include <string> @@ -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<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 cmCatViews( + { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... }); +} + template <typename Range> std::string cmWrap(std::string const& prefix, Range const& r, std::string const& suffix, std::string const& sep) @@ -422,6 +422,7 @@ CMAKE_CXX_SOURCES="\ cmState \ cmStateDirectory \ cmStateSnapshot \ + cmStringAlgorithms \ cmStringReplaceHelper \ cmStringCommand \ cmSubdirCommand \ |