-
Notifications
You must be signed in to change notification settings - Fork 218
Implement operator<<
for cuda::std::string_view
#4736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davebayer
wants to merge
6
commits into
NVIDIA:main
Choose a base branch
from
davebayer:string_view_ostream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
275c887
Implement `operator<<` for `cuda::std::string_view`
davebayer 545c4d9
add interoperability with `std::string_view`
davebayer 576a28c
fix review, simplify implementation
davebayer 12ebede
fix typo
davebayer d8346ae
add missing `typename`
davebayer 2974a4f
try to fix compilation
davebayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
libcudacxx/test/libcudacxx/cuda/strings/std_interop/string.view.cons.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// <cuda/std/string_view> | ||
|
||
// constexpr basic_string_view(std::basic_string_view sv); | ||
|
||
#include <cuda/std/cassert> | ||
#include <cuda/std/string_view> | ||
#include <cuda/std/type_traits> | ||
|
||
#include <string_view> | ||
|
||
#include "literal.h" | ||
|
||
template <class CharT> | ||
constexpr void test_with_default_type_traits() | ||
{ | ||
using HostSV = std::basic_string_view<CharT>; | ||
using CudaSV = cuda::std::basic_string_view<CharT>; | ||
|
||
// check that cuda::std::char_traits are mapped to std::char_traits | ||
static_assert(cuda::std::is_same_v<typename HostSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename HostSV::traits_type, std::char_traits<CharT>>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::traits_type, cuda::std::char_traits<CharT>>); | ||
|
||
const CharT* str = TEST_STRLIT(CharT, "some text"); | ||
|
||
// test construction from std::basic_string_view<CharT, std::char_traits<CharT>> | ||
{ | ||
static_assert(cuda::std::is_constructible_v<CudaSV, HostSV>); | ||
static_assert(noexcept(CudaSV{HostSV{}})); | ||
|
||
HostSV host_sv{str}; | ||
CudaSV cuda_sv{host_sv}; | ||
assert(cuda_sv.data() == host_sv.data()); | ||
assert(cuda_sv.size() == host_sv.size()); | ||
} | ||
} | ||
|
||
template <class CharT> | ||
struct custom_type_traits | ||
: private std::char_traits<CharT> | ||
, private cuda::std::char_traits<CharT> | ||
{ | ||
using cuda::std::char_traits<CharT>::char_type; | ||
using cuda::std::char_traits<CharT>::int_type; | ||
using std::char_traits<CharT>::pos_type; | ||
using std::char_traits<CharT>::off_type; | ||
using std::char_traits<CharT>::state_type; | ||
|
||
using cuda::std::char_traits<CharT>::assign; | ||
using cuda::std::char_traits<CharT>::eq; | ||
using cuda::std::char_traits<CharT>::lt; | ||
using cuda::std::char_traits<CharT>::compare; | ||
using cuda::std::char_traits<CharT>::length; | ||
using cuda::std::char_traits<CharT>::find; | ||
using cuda::std::char_traits<CharT>::move; | ||
using cuda::std::char_traits<CharT>::copy; | ||
using cuda::std::char_traits<CharT>::to_char_type; | ||
using cuda::std::char_traits<CharT>::to_int_type; | ||
using cuda::std::char_traits<CharT>::eq_int_type; | ||
using std::char_traits<CharT>::eof; | ||
using std::char_traits<CharT>::not_eof; | ||
}; | ||
|
||
template <class CharT> | ||
constexpr void test_with_custom_type_traits() | ||
{ | ||
using HostSV = std::basic_string_view<CharT, custom_type_traits<CharT>>; | ||
using CudaSV = cuda::std::basic_string_view<CharT, custom_type_traits<CharT>>; | ||
|
||
// check that cuda::std::char_traits are mapped to std::char_traits | ||
static_assert(cuda::std::is_same_v<typename HostSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename HostSV::traits_type, custom_type_traits<CharT>>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::traits_type, custom_type_traits<CharT>>); | ||
|
||
const CharT* str = TEST_STRLIT(CharT, "some text"); | ||
|
||
// test construction from std::basic_string_view<CharT, custom_type_traits<CharT>> | ||
{ | ||
static_assert(cuda::std::is_constructible_v<CudaSV, HostSV>); | ||
static_assert(noexcept(CudaSV{HostSV{}})); | ||
|
||
HostSV host_sv{str}; | ||
CudaSV cuda_sv{host_sv}; | ||
assert(cuda_sv.data() == host_sv.data()); | ||
assert(cuda_sv.size() == host_sv.size()); | ||
} | ||
} | ||
|
||
template <class CharT> | ||
constexpr void test_type() | ||
{ | ||
test_with_default_type_traits<CharT>(); | ||
test_with_custom_type_traits<CharT>(); | ||
} | ||
|
||
constexpr bool test() | ||
{ | ||
test_type<char>(); | ||
#if _CCCL_HAS_CHAR8_T() | ||
test_type<char8_t>(); | ||
#endif // _CCCL_HAS_CHAR8_T() | ||
test_type<char16_t>(); | ||
test_type<char32_t>(); | ||
#if _CCCL_HAS_WCHAR_T() | ||
test_type<wchar_t>(); | ||
#endif // _CCCL_HAS_WCHAR_T() | ||
|
||
return true; | ||
} | ||
|
||
static_assert(test()); | ||
|
||
int main(int, char**) | ||
{ | ||
NV_IF_TARGET(NV_IS_HOST, (test();)) | ||
return 0; | ||
} |
119 changes: 119 additions & 0 deletions
119
libcudacxx/test/libcudacxx/cuda/strings/std_interop/string.view.conversion.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// <cuda/std/string_view> | ||
|
||
// constexpr operator std::basic_string_view() const noexcept; | ||
|
||
#include <cuda/std/cassert> | ||
#include <cuda/std/string_view> | ||
#include <cuda/std/type_traits> | ||
|
||
#include <string_view> | ||
|
||
#include "literal.h" | ||
|
||
template <class CharT> | ||
constexpr void test_with_default_type_traits() | ||
{ | ||
using HostSV = std::basic_string_view<CharT>; | ||
using CudaSV = cuda::std::basic_string_view<CharT>; | ||
|
||
// check that cuda::std::char_traits are mapped to std::char_traits | ||
static_assert(cuda::std::is_same_v<typename HostSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename HostSV::traits_type, std::char_traits<CharT>>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::traits_type, cuda::std::char_traits<CharT>>); | ||
|
||
const CharT* str = TEST_STRLIT(CharT, "some text"); | ||
|
||
// test conversion to std::basic_string_view<CharT, std::char_traits<CharT>> | ||
{ | ||
static_assert(cuda::std::is_convertible_v<CudaSV, HostSV>); | ||
static_assert(noexcept(CudaSV{}.operator HostSV())); | ||
|
||
CudaSV cuda_sv{str}; | ||
HostSV host_sv{cuda_sv}; | ||
assert(host_sv.data() == cuda_sv.data()); | ||
assert(host_sv.size() == cuda_sv.size()); | ||
} | ||
} | ||
|
||
template <class CharT> | ||
struct custom_type_traits | ||
: private std::char_traits<CharT> | ||
, private cuda::std::char_traits<CharT> | ||
{ | ||
using cuda::std::char_traits<CharT>::char_type; | ||
using cuda::std::char_traits<CharT>::int_type; | ||
using std::char_traits<CharT>::pos_type; | ||
using std::char_traits<CharT>::off_type; | ||
using std::char_traits<CharT>::state_type; | ||
|
||
using cuda::std::char_traits<CharT>::assign; | ||
using cuda::std::char_traits<CharT>::eq; | ||
using cuda::std::char_traits<CharT>::lt; | ||
using cuda::std::char_traits<CharT>::compare; | ||
using cuda::std::char_traits<CharT>::length; | ||
using cuda::std::char_traits<CharT>::find; | ||
using cuda::std::char_traits<CharT>::move; | ||
using cuda::std::char_traits<CharT>::copy; | ||
using cuda::std::char_traits<CharT>::to_char_type; | ||
using cuda::std::char_traits<CharT>::to_int_type; | ||
using cuda::std::char_traits<CharT>::eq_int_type; | ||
using std::char_traits<CharT>::eof; | ||
using std::char_traits<CharT>::not_eof; | ||
}; | ||
|
||
template <class CharT> | ||
constexpr void test_with_custom_type_traits() | ||
{ | ||
using HostSV = std::basic_string_view<CharT, custom_type_traits<CharT>>; | ||
using CudaSV = cuda::std::basic_string_view<CharT, custom_type_traits<CharT>>; | ||
|
||
// check that cuda::std::char_traits are mapped to std::char_traits | ||
static_assert(cuda::std::is_same_v<typename HostSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename HostSV::traits_type, custom_type_traits<CharT>>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename CudaSV::traits_type, custom_type_traits<CharT>>); | ||
|
||
const CharT* str = TEST_STRLIT(CharT, "some text"); | ||
|
||
// test conversion to std::basic_string_view<CharT, custom_type_traits<CharT>> | ||
{ | ||
static_assert(cuda::std::is_convertible_v<CudaSV, HostSV>); | ||
static_assert(noexcept(CudaSV{}.operator HostSV())); | ||
|
||
CudaSV cuda_sv{str}; | ||
HostSV host_sv{cuda_sv}; | ||
assert(host_sv.data() == cuda_sv.data()); | ||
assert(host_sv.size() == cuda_sv.size()); | ||
} | ||
} | ||
|
||
template <class CharT> | ||
constexpr void test_type() | ||
{ | ||
test_with_default_type_traits<CharT>(); | ||
test_with_custom_type_traits<CharT>(); | ||
} | ||
|
||
constexpr bool test() | ||
{ | ||
test_type<char>(); | ||
#if _CCCL_HAS_CHAR8_T() | ||
test_type<char8_t>(); | ||
#endif // _CCCL_HAS_CHAR8_T() | ||
test_type<char16_t>(); | ||
test_type<char32_t>(); | ||
#if _CCCL_HAS_WCHAR_T() | ||
test_type<wchar_t>(); | ||
#endif // _CCCL_HAS_WCHAR_T() | ||
|
||
return true; | ||
} | ||
|
||
static_assert(test()); | ||
|
||
int main(int, char**) | ||
{ | ||
NV_IF_TARGET(NV_IS_HOST, (test();)) | ||
return 0; | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This forces use of the same char_traits than this string_view is using. We should templatize this so that we can construct from a
::std::string_view
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional, the idea is that both char traits types must match with an exception of
std::char_traits
that are mapped tocuda::std::char_traits
. That means that:cuda::std::basic_string_view<CharT, cuda::std::char_traits<CharT>>
can be constructed (converted) from (to) bothstd::basic_string_view<CharT, std::char_traits<CharT>>
andstd::basic_string_view<CharT, cuda::std::char_traits<CharT>>
cuda::std::basic_string_view<CharT, Traits>
can be constructed (converted) from (to)std::basic_string_view<CharT, Traits>
Do you think this is wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe then should have 2 constructors,
We can then omit the constraint on the second constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what we have now. But I don't think we can omit the constraint, because we must ensure that the current type traits are
cuda::std::char_traits
, otherwise we would allow something like:which is wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think so, the incoming type traits are correct and its the users responsibility to ensure that their char_traits are conforming.
Also no one writes their own char_traits, so I believe its more of a theoretical question