Improve support for printing std::basic_string_view #4624
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Extend printing capabilities for std::basic_string_view
What is std::basic_string_view?
std::basic_string_view (spec) is a relatively new (since C++17) form of manipulating string objects in C++.
The language also provides a suffix for it, so the following line will create and initiate a new
std::string_view
(synonym forstd::basic_string_view<char>
) object:Does GTest support it?
Currently, GTest has some means of outputting these objects. It will be handled by the
ContainerPrinter
with one exception.It will look a bit ugly though. Printing
L"w"sv
will produce{ L'w' (119, 0x77) }
.Exception
GTest
declares its ownStringView
which depending on the build parameters will be eitherabsl::string_view
orstd::string_view
.Besides that
GTest
implements printing capability for thatStringView
type. Unfortunately, in the best case scenario it covers only one of the subtypes of thestd::basic_string_view
family. That is why in the #4295 author mentioned that the issue appears only for non-char basic_string_view's.Solution
To improve the way
std::basic_string_view
is printed,PrintTo
function was overloaded with all the available subtypes of thestd::basic_string_view
(just the way it's done for thestd::basic_string
):std::string_view
std::u8string_view
std::u16string_view
std::u32string_view
std::wstring_view
This should fix #4295.
Notes
std::string_view
will be added only in case the language level supports it andGTest
is built withabsl::string_view
. Otherwise it skips the implementation, because it should be already done forinternal::StringView
type.std::u8string_view
is implemented only when the flag__cpp_lib_char8_t
is defined.std::wstring_view
is implemented if the flagGTEST_HAS_STD_WSTRING
is defined.I followed the way it's done for similar
std::basic_string<T>
types, but I'd like to ask to double check it.Tests
I added a few tests too.
If more needed, please let me know.