mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Add to_basic_string
This commit is contained in:
parent
f8ef4e2355
commit
ec109d2374
@ -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>
|
||||
|
@ -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)
|
||||
{
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user