diff --git a/include/gsl/string_span b/include/gsl/string_span index 6b4c839..c08f246 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -376,6 +376,21 @@ std::basic_string to_basic_string(basic_string_span(view.length())}; } +template +basic_string_span::value> +as_bytes(basic_string_span s) noexcept +{ + return { reinterpret_cast(s.data()), s.size_bytes() }; +} + +template ::value>> +basic_string_span::value> +as_writeable_bytes(basic_string_span s) noexcept +{ + return {reinterpret_cast(s.data()), s.size_bytes()}; +} + // zero-terminated string span, used to convert // zero-terminated spans to legacy strings template diff --git a/tests/string_span_tests.cpp b/tests/string_span_tests.cpp index 0032b0f..c0b5b19 100644 --- a/tests/string_span_tests.cpp +++ b/tests/string_span_tests.cpp @@ -1167,3 +1167,22 @@ TEST_CASE("char32_t type") CHECK(ss8 <= ss9); CHECK(ss8 != ss9); } + +TEST_CASE("as_bytes") +{ + cwzstring_span<> v(L"qwerty"); + const auto s = v.as_string_span(); + const auto bs = as_bytes(s); + CHECK(static_cast(bs.data()) == static_cast(s.data())); + CHECK(bs.size() == s.size_bytes()); +} + +TEST_CASE("as_writeable_bytes") +{ + wchar_t buf[]{L"qwerty"}; + wzstring_span<> v(buf); + const auto s = v.as_string_span(); + const auto bs = as_writeable_bytes(s); + CHECK(static_cast(bs.data()) == static_cast(s.data())); + CHECK(bs.size() == s.size_bytes()); +}