From ec109d2374a6fe949b9760c5f55869119072f083 Mon Sep 17 00:00:00 2001 From: Gary Furnish Date: Sun, 4 Sep 2016 00:29:10 -0600 Subject: [PATCH] Add to_basic_string --- gsl/string_span | 10 ++++++++++ tests/string_span_tests.cpp | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/gsl/string_span b/gsl/string_span index 8e60183..cd49998 100644 --- a/gsl/string_span +++ b/gsl/string_span @@ -486,6 +486,16 @@ inline std::wstring to_string(wstring_span<> view) #endif +template , + typename Allocator = std::allocator, + typename gCharT, + std::ptrdiff_t Extent> +std::basic_string to_basic_string(basic_string_span view) +{ + return {view.data(), static_cast(view.length())}; +} + // 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 7e623f3..a1cfe79 100644 --- a/tests/string_span_tests.cpp +++ b/tests/string_span_tests.cpp @@ -114,6 +114,18 @@ SUITE(string_span_tests) CHECK(s2.length() == 5); } + TEST(TestToBasicString) + { + auto s = gsl::to_basic_string,::std::allocator>(cstring_span<>{}); + CHECK(s.length() == 0); + + char stack_string[] = "Hello"; + cstring_span<> v = ensure_z(stack_string); + auto s2 = gsl::to_basic_string,::std::allocator>(v); + CHECK(static_cast::index_type>(s2.length()) == v.length()); + CHECK(s2.length() == 5); + } + TEST(EqualityAndImplicitConstructors) { {