Fix #247: add to_basic_string().

Add to_basic_string
This commit is contained in:
Neil MacIntosh 2016-09-04 14:12:40 -07:00 committed by GitHub
commit a360b81805
2 changed files with 22 additions and 0 deletions

View File

@ -486,6 +486,16 @@ inline std::wstring to_string(wstring_span<> view)
#endif
template <typename CharT,
typename Traits = typename std::char_traits<CharT>,
typename Allocator = std::allocator<CharT>,
typename gCharT,
std::ptrdiff_t Extent>
std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
{
return {view.data(), static_cast<size_t>(view.length())};
}
// zero-terminated string span, used to convert
// zero-terminated spans to legacy strings
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>

View File

@ -114,6 +114,18 @@ SUITE(string_span_tests)
CHECK(s2.length() == 5);
}
TEST(TestToBasicString)
{
auto s = gsl::to_basic_string<char,std::char_traits<char>,::std::allocator<char>>(cstring_span<>{});
CHECK(s.length() == 0);
char stack_string[] = "Hello";
cstring_span<> v = ensure_z(stack_string);
auto s2 = gsl::to_basic_string<char,std::char_traits<char>,::std::allocator<char>>(v);
CHECK(static_cast<cstring_span<>::index_type>(s2.length()) == v.length());
CHECK(s2.length() == 5);
}
TEST(EqualityAndImplicitConstructors)
{
{