From: Joel Rosdahl Date: Mon, 5 Sep 2022 11:19:03 +0000 (+0200) Subject: enhance: Add util::to_span and util::to_string_view functions X-Git-Tag: v4.7~78 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d2d8b28531bd2eb29b68f2ec01596978d19d5080;p=thirdparty%2Fccache.git enhance: Add util::to_span and util::to_string_view functions --- diff --git a/src/util/string.hpp b/src/util/string.hpp index 5ee9716d6..d9c7fe595 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -19,9 +19,11 @@ #pragma once #include +#include #include // for mode_t +#include #include #include #include @@ -110,10 +112,16 @@ 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 `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 @@ -158,6 +166,13 @@ starts_with(const std::string_view string, const std::string_view prefix) return string.substr(0, prefix.size()) == prefix; } +inline nonstd::span +to_span(std::string_view data) +{ + return nonstd::span( + reinterpret_cast(data.data()), data.size()); +} + template inline std::string to_string(const T& t) @@ -180,4 +195,11 @@ to_string(const std::string_view& sv) return std::string(sv); } +inline std::string_view +to_string_view(nonstd::span data) +{ + return std::string_view(reinterpret_cast(data.data()), + data.size()); +} + } // namespace util