From aead7c080841a76c22fb21a1bd0cb13f8d30962c Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Fri, 7 Jul 2023 16:24:40 +0200 Subject: [PATCH] refactor: Move data type conversion utilities to util --- src/util/conversion.hpp | 103 ++++++++++++++++++++++++++++++ src/util/string.hpp | 69 +------------------- unittest/CMakeLists.txt | 1 + unittest/test_util_conversion.cpp | 48 ++++++++++++++ unittest/test_util_string.cpp | 20 ------ 5 files changed, 153 insertions(+), 88 deletions(-) create mode 100644 src/util/conversion.hpp create mode 100644 unittest/test_util_conversion.cpp diff --git a/src/util/conversion.hpp b/src/util/conversion.hpp new file mode 100644 index 000000000..6592b3526 --- /dev/null +++ b/src/util/conversion.hpp @@ -0,0 +1,103 @@ +// Copyright (C) 2023 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#pragma once + +#include + +#include + +#include +#include +#include + +namespace util { + +// --- Interface --- + +// Convert `data` to a `nonstd::span`. +nonstd::span to_span(const void* data, size_t size); + +// Convert `value` to a `nonstd::span`. +nonstd::span to_span(std::string_view value); + +// Convert `value` to a string. This function is used when joining +// `std::string`s with `util::join`. +template std::string to_string(const T& value); + +// Convert `data` to a `std::string_view`. +std::string_view to_string_view(nonstd::span data); + +// --- Inline implementations --- + +inline nonstd::span +to_span(const void* data, size_t size) +{ + return {reinterpret_cast(data), size}; +} + +inline nonstd::span +to_span(std::string_view data) +{ + return to_span(data.data(), data.size()); +} + +template +inline std::string +to_string(const T& t) +{ + using std::to_string; + return to_string(std::forward(t)); +} + +template<> +inline std::string +to_string(const std::string& string) +{ + return std::string(string); +} + +template<> +inline std::string +to_string(const std::string_view& sv) +{ + return std::string(sv); +} + +template<> +inline std::string +to_string(const nonstd::span& bytes) +{ + return std::string(to_string_view(bytes)); +} + +template<> +inline std::string +to_string(const util::Bytes& bytes) +{ + return std::string(to_string_view(bytes)); +} + +inline std::string_view +to_string_view(nonstd::span data) +{ + return std::string_view(reinterpret_cast(data.data()), + data.size()); +} + +} // namespace util diff --git a/src/util/string.hpp b/src/util/string.hpp index 1ef58a139..300faab85 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -19,6 +19,7 @@ #pragma once #include +#include #include #include @@ -150,19 +151,6 @@ bool starts_with(std::string_view string, std::string_view prefix); // Strip whitespace from left and right side of a string. [[nodiscard]] std::string strip_whitespace(std::string_view string); -// Convert `data` to a `nonstd::span`. -nonstd::span to_span(const void* data, size_t size); - -// Convert `value` to a `nonstd::span`. -nonstd::span to_span(std::string_view value); - -// Convert `value` to a string. This function is used when joining -// `std::string`s with `util::join`. -template std::string to_string(const T& value); - -// Convert `data` to a `std::string_view`. -std::string_view to_string_view(nonstd::span data); - // --- Inline implementations --- inline bool @@ -207,59 +195,4 @@ starts_with(const std::string_view string, const std::string_view prefix) return string.substr(0, prefix.size()) == prefix; } -inline nonstd::span -to_span(const void* data, size_t size) -{ - return {reinterpret_cast(data), size}; -} - -inline nonstd::span -to_span(std::string_view data) -{ - return to_span(data.data(), data.size()); -} - -template -inline std::string -to_string(const T& t) -{ - using std::to_string; - return to_string(std::forward(t)); -} - -template<> -inline std::string -to_string(const std::string& string) -{ - return std::string(string); -} - -template<> -inline std::string -to_string(const std::string_view& sv) -{ - return std::string(sv); -} - -template<> -inline std::string -to_string(const nonstd::span& bytes) -{ - return std::string(to_string_view(bytes)); -} - -template<> -inline std::string -to_string(const util::Bytes& bytes) -{ - return std::string(to_string_view(bytes)); -} - -inline std::string_view -to_string_view(nonstd::span data) -{ - return std::string_view(reinterpret_cast(data.data()), - data.size()); -} - } // namespace util diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 062961f01..208dd8bbb 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -29,6 +29,7 @@ set( test_util_Tokenizer.cpp test_util_XXH3_128.cpp test_util_XXH3_64.cpp + test_util_conversion.cpp test_util_expected.cpp test_util_file.cpp test_util_path.cpp diff --git a/unittest/test_util_conversion.cpp b/unittest/test_util_conversion.cpp new file mode 100644 index 000000000..2daadaa2d --- /dev/null +++ b/unittest/test_util_conversion.cpp @@ -0,0 +1,48 @@ +// Copyright (C) 2021-2023 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include + +#include + +#include // https://github.com/doctest/doctest/issues/618 +#include + +TEST_SUITE_BEGIN("util"); + +TEST_CASE("util::to_string") +{ + const uint8_t bytes[] = {'f', 'o', 'o'}; + const char str[] = "foo"; + + CHECK(util::to_string(std::string(str)) == std::string(str)); + CHECK(util::to_string(std::string_view(str)) == std::string(str)); + CHECK(util::to_string(nonstd::span(bytes)) + == std::string(str)); + CHECK(util::to_string(util::Bytes(bytes, 3)) == std::string(str)); +} + +TEST_CASE("util::to_string_view") +{ + uint8_t bytes[] = {'f', 'o', 'o'}; + char str[] = "foo"; + + CHECK(util::to_string_view(nonstd::span(bytes)) == std::string(str)); +} + +TEST_SUITE_END(); diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index 371020480..12e4822fa 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -522,24 +522,4 @@ TEST_CASE("util::strip_whitespace") CHECK(util::strip_whitespace(" x y ") == "x y"); } -TEST_CASE("util::to_string") -{ - const uint8_t bytes[] = {'f', 'o', 'o'}; - const char str[] = "foo"; - - CHECK(util::to_string(std::string(str)) == std::string(str)); - CHECK(util::to_string(std::string_view(str)) == std::string(str)); - CHECK(util::to_string(nonstd::span(bytes)) - == std::string(str)); - CHECK(util::to_string(util::Bytes(bytes, 3)) == std::string(str)); -} - -TEST_CASE("util::to_string_view") -{ - uint8_t bytes[] = {'f', 'o', 'o'}; - char str[] = "foo"; - - CHECK(util::to_string_view(nonstd::span(bytes)) == std::string(str)); -} - TEST_SUITE_END(); -- 2.47.2