--- /dev/null
+// 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 <util/Bytes.hpp>
+
+#include <third_party/nonstd/span.hpp>
+
+#include <cstdint>
+#include <string>
+#include <string_view>
+
+namespace util {
+
+// --- Interface ---
+
+// Convert `data` to a `nonstd::span<const uint8_t>`.
+nonstd::span<const uint8_t> to_span(const void* data, size_t size);
+
+// Convert `value` to a `nonstd::span<const uint8_t>`.
+nonstd::span<const uint8_t> to_span(std::string_view value);
+
+// Convert `value` to a string. This function is used when joining
+// `std::string`s with `util::join`.
+template<typename T> std::string to_string(const T& value);
+
+// Convert `data` to a `std::string_view`.
+std::string_view to_string_view(nonstd::span<const uint8_t> data);
+
+// --- Inline implementations ---
+
+inline nonstd::span<const uint8_t>
+to_span(const void* data, size_t size)
+{
+ return {reinterpret_cast<const uint8_t*>(data), size};
+}
+
+inline nonstd::span<const uint8_t>
+to_span(std::string_view data)
+{
+ return to_span(data.data(), data.size());
+}
+
+template<typename T>
+inline std::string
+to_string(const T& t)
+{
+ using std::to_string;
+ return to_string(std::forward<T>(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<const uint8_t>& 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<const uint8_t> data)
+{
+ return std::string_view(reinterpret_cast<const char*>(data.data()),
+ data.size());
+}
+
+} // namespace util
#pragma once
#include <util/Bytes.hpp>
+#include <util/conversion.hpp>
#include <third_party/nonstd/expected.hpp>
#include <third_party/nonstd/span.hpp>
// 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<const uint8_t>`.
-nonstd::span<const uint8_t> to_span(const void* data, size_t size);
-
-// Convert `value` to a `nonstd::span<const uint8_t>`.
-nonstd::span<const uint8_t> to_span(std::string_view value);
-
-// Convert `value` to a string. This function is used when joining
-// `std::string`s with `util::join`.
-template<typename T> std::string to_string(const T& value);
-
-// Convert `data` to a `std::string_view`.
-std::string_view to_string_view(nonstd::span<const uint8_t> data);
-
// --- Inline implementations ---
inline bool
return string.substr(0, prefix.size()) == prefix;
}
-inline nonstd::span<const uint8_t>
-to_span(const void* data, size_t size)
-{
- return {reinterpret_cast<const uint8_t*>(data), size};
-}
-
-inline nonstd::span<const uint8_t>
-to_span(std::string_view data)
-{
- return to_span(data.data(), data.size());
-}
-
-template<typename T>
-inline std::string
-to_string(const T& t)
-{
- using std::to_string;
- return to_string(std::forward<T>(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<const uint8_t>& 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<const uint8_t> data)
-{
- return std::string_view(reinterpret_cast<const char*>(data.data()),
- data.size());
-}
-
} // namespace util
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
--- /dev/null
+// 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 <util/conversion.hpp>
+
+#include <third_party/doctest.h>
+
+#include <ostream> // https://github.com/doctest/doctest/issues/618
+#include <vector>
+
+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<const uint8_t>(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();
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<const uint8_t>(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();