From 85160108055fd6ab88bf05ae928eddae0b142cf6 Mon Sep 17 00:00:00 2001 From: Bailey Chittle Date: Thu, 5 Jan 2023 10:06:30 -0500 Subject: [PATCH] C++11 backwards compat fixes by replacing std::string_view with built-in string_view_t --- CMakeLists.txt | 2 +- include/spdlog/details/attr_composer.h | 8 ++++--- include/spdlog/details/log_attr.h | 31 ++++++++++++++------------ include/spdlog/fmt/bundled/core.h | 2 ++ 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3594b58..61d21781 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ endif() # Compiler config # --------------------------------------------------------------------------------------- if(NOT CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) endif() diff --git a/include/spdlog/details/attr_composer.h b/include/spdlog/details/attr_composer.h index 4d7ab50e..6a1a32bd 100644 --- a/include/spdlog/details/attr_composer.h +++ b/include/spdlog/details/attr_composer.h @@ -1,11 +1,12 @@ #pragma once #include +#include namespace spdlog { namespace details { -inline void scramble(std::string& dst, std::string_view s) +inline void scramble(std::string& dst, string_view_t s) { if (s.empty()) return; @@ -16,11 +17,12 @@ inline void scramble(std::string& dst, std::string_view s) dst.reserve(dst.size() + s.size()); - auto replace = [&](std::string_view with) { + auto replace = [&](string_view_t with) { dst.append(start, size_t(cursor - start)); ++cursor; start = cursor; - dst.append(with); + // dst.append(with); + dst.append(with.data(), with.size()); }; while (cursor != end) { diff --git a/include/spdlog/details/log_attr.h b/include/spdlog/details/log_attr.h index 5a7330bd..3a59492d 100644 --- a/include/spdlog/details/log_attr.h +++ b/include/spdlog/details/log_attr.h @@ -1,9 +1,9 @@ #pragma once #include -#include #include #include "attr_composer.h" +#include namespace spdlog { namespace details { @@ -17,28 +17,31 @@ struct attr std::string value; public: - attr(std::initializer_list l) { + attr(std::initializer_list l) { if (l.size() != 2) return; // throw exception if not kv pair? scramble(key, *l.begin()); scramble(value, *(l.begin()+1)); } - attr(std::string_view k, bool v) - : key{k} - , value{v ? "true" : "false"} - {} + attr(string_view_t k, bool v) + : value{v ? "true" : "false"} + { + key = std::string{k.data(), k.size()}; + } - attr(std::string_view k, std::string_view v) - : key{k} - , value{v} - {} + attr(string_view_t k, string_view_t v) + { + key = std::string{k.data(), k.size()}; + value = std::string{v.data(), v.size()}; + } template - attr(std::string_view k, T const &v) - : key{k} - , value{std::to_string(v)} - {} + attr(string_view_t k, T const &v) + : value{std::to_string(v)} + { + key = std::string{k.data(), k.size()}; + } }; } // namespace details diff --git a/include/spdlog/fmt/bundled/core.h b/include/spdlog/fmt/bundled/core.h index f6a37af9..a0de87f4 100644 --- a/include/spdlog/fmt/bundled/core.h +++ b/include/spdlog/fmt/bundled/core.h @@ -474,6 +474,8 @@ template class basic_string_view { /** Returns the string size. */ constexpr auto size() const noexcept -> size_t { return size_; } + constexpr auto empty() const noexcept -> bool { return size_ == 0; } + constexpr auto begin() const noexcept -> iterator { return data_; } constexpr auto end() const noexcept -> iterator { return data_ + size_; }