-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
275c887
545c4d9
576a28c
12ebede
d8346ae
2974a4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ | |
#include <cuda/std/version> | ||
|
||
#if !_CCCL_COMPILER(NVRTC) | ||
# include <iosfwd> | ||
# include <string_view> | ||
#endif // !_CCCL_COMPILER(NVRTC) | ||
|
||
#include <cuda/std/__cccl/prologue.h> | ||
|
@@ -105,6 +105,7 @@ public: | |
|
||
_CCCL_HIDE_FROM_ABI basic_string_view& operator=(const basic_string_view&) noexcept = default; | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
_LIBCUDACXX_HIDE_FROM_ABI constexpr basic_string_view(const _CharT* __s) noexcept | ||
: __data_{__s} | ||
, __size_{_Traits::length(__s)} | ||
|
@@ -146,6 +147,21 @@ public: | |
, __size_{_CUDA_VRANGES::size(__r)} | ||
{} | ||
|
||
#if !_CCCL_COMPILER(NVRTC) | ||
_CCCL_HIDE_FROM_ABI constexpr basic_string_view(::std::basic_string_view<_CharT, _Traits> __sv) noexcept | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Do you think this is wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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::basic_string_view<my_char, my_char_traits>{std::basic_string_view<my_char, std::char_traits<my_char>>{...}}; which is wrong There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
: __data_{__sv.data()} | ||
, __size_{__sv.size()} | ||
{} | ||
|
||
_CCCL_TEMPLATE(bool = true) | ||
_CCCL_REQUIRES(is_same_v<_Traits, char_traits<_CharT>>) | ||
davebayer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_CCCL_HIDE_FROM_ABI constexpr basic_string_view( | ||
::std::basic_string_view<_CharT, ::std::char_traits<_CharT>> __sv) noexcept | ||
: __data_{__sv.data()} | ||
, __size_{__sv.size()} | ||
{} | ||
#endif // !_CCCL_COMPILER(NVRTC) | ||
|
||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept | ||
{ | ||
return const_iterator{__data_}; | ||
|
@@ -262,6 +278,7 @@ public: | |
__other.__size_ = __sz; | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
_LIBCUDACXX_HIDE_FROM_ABI constexpr size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const | ||
{ | ||
if (__pos > __size_) | ||
|
@@ -288,6 +305,7 @@ public: | |
|
||
// compare | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr int compare(basic_string_view __sv) const noexcept | ||
{ | ||
const auto __rlen = _CUDA_VSTD::min(__size_, __sv.__size_); | ||
|
@@ -350,6 +368,7 @@ public: | |
return _CUDA_VSTD::__cccl_str_find<value_type, size_type, traits_type, npos>(__data_, __size_, __s, __pos, __n); | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr size_type find(const _CharT* __s, size_type __pos = 0) const noexcept | ||
{ | ||
_CCCL_ASSERT(__s != nullptr, "string_view::find(): received nullptr"); | ||
|
@@ -379,6 +398,7 @@ public: | |
return _CUDA_VSTD::__cccl_str_rfind<value_type, size_type, traits_type, npos>(__data_, __size_, __s, __pos, __n); | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr size_type | ||
rfind(const _CharT* __s, size_type __pos = npos) const noexcept | ||
{ | ||
|
@@ -411,6 +431,7 @@ public: | |
__data_, __size_, __s, __pos, __n); | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr size_type | ||
find_first_of(const _CharT* __s, size_type __pos = 0) const noexcept | ||
{ | ||
|
@@ -443,6 +464,7 @@ public: | |
__data_, __size_, __s, __pos, __n); | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr size_type | ||
find_last_of(const _CharT* __s, size_type __pos = npos) const noexcept | ||
{ | ||
|
@@ -476,6 +498,7 @@ public: | |
__data_, __size_, __s, __pos, __n); | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr size_type | ||
find_first_not_of(const _CharT* __s, size_type __pos = 0) const noexcept | ||
{ | ||
|
@@ -509,6 +532,7 @@ public: | |
__data_, __size_, __s, __pos, __n); | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr size_type | ||
find_last_not_of(const _CharT* __s, size_type __pos = npos) const noexcept | ||
{ | ||
|
@@ -524,6 +548,7 @@ public: | |
return (__size_ >= __s.__size_) && compare(0, __s.__size_, __s) == 0; | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr bool starts_with(value_type __c) const noexcept | ||
{ | ||
return (__size_ > 0) && _Traits::eq(front(), __c); | ||
|
@@ -541,6 +566,7 @@ public: | |
return (__size_ >= __s.__size_) && compare(__size_ - __s.__size_, npos, __s) == 0; | ||
} | ||
|
||
_CCCL_EXEC_CHECK_DISABLE | ||
[[nodiscard]] _LIBCUDACXX_HIDE_FROM_ABI constexpr bool ends_with(value_type __c) const noexcept | ||
{ | ||
return (__size_ > 0) && _Traits::eq(back(), __c); | ||
|
@@ -711,6 +737,20 @@ public: | |
return __lhs.compare(__rhs) >= 0; | ||
} | ||
|
||
#if !_CCCL_COMPILER(NVRTC) | ||
_CCCL_HIDE_FROM_ABI constexpr operator ::std::basic_string_view<_CharT, _Traits>() const noexcept | ||
{ | ||
return ::std::basic_string_view<_CharT, _Traits>{__data_, __size_}; | ||
} | ||
|
||
_CCCL_TEMPLATE(bool = true) | ||
_CCCL_REQUIRES(is_same_v<_Traits, char_traits<_CharT>>) | ||
_CCCL_HIDE_FROM_ABI constexpr operator ::std::basic_string_view<_CharT, ::std::char_traits<_CharT>>() const noexcept | ||
{ | ||
return ::std::basic_string_view<_CharT, ::std::char_traits<_CharT>>{__data_, __size_}; | ||
} | ||
#endif // !_CCCL_COMPILER(NVRTC) | ||
|
||
private: | ||
enum class __assume_valid | ||
{ | ||
|
@@ -738,16 +778,32 @@ _CCCL_TEMPLATE(class _Range) | |
_CCCL_REQUIRES(_CUDA_VRANGES::contiguous_range<_Range>) | ||
_CCCL_HOST_DEVICE basic_string_view(_Range&&) -> basic_string_view<_CUDA_VRANGES::range_value_t<_Range>>; | ||
|
||
#if !_CCCL_COMPILER(NVRTC) | ||
_CCCL_TEMPLATE(class _CharT, class _Traits) | ||
_CCCL_HOST_DEVICE basic_string_view(::std::basic_string_view<_CharT, _Traits>) -> basic_string_view<_CharT, _Traits>; | ||
|
||
_CCCL_TEMPLATE(class _CharT) | ||
_CCCL_HOST_DEVICE basic_string_view(::std::basic_string_view<_CharT, ::std::char_traits<_CharT>>) | ||
-> basic_string_view<_CharT, char_traits<_CharT>>; | ||
#endif // !_CCCL_COMPILER(NVRTC) | ||
|
||
// operator << | ||
|
||
#if 0 // todo: we need to implement char_traits stream types & functions | ||
#if !_CCCL_COMPILER(NVRTC) | ||
template <class _CharT, class _Traits> | ||
_LIBCUDACXX_HIDE_FROM_ABI ::std::basic_ostream<_CharT, _Traits>& | ||
_CCCL_HIDE_FROM_ABI _CCCL_HOST ::std::basic_ostream<_CharT, _Traits>& | ||
operator<<(::std::basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __str) | ||
{ | ||
return __os.write(__str.data(), static_cast<::std::streamsize>(__str.size())); | ||
return __os << ::std::basic_string_view<_CharT, _Traits>{__str}; | ||
} | ||
#endif // 0 | ||
|
||
template <class _CharT> | ||
_CCCL_HIDE_FROM_ABI _CCCL_HOST ::std::basic_ostream<_CharT, ::std::char_traits<_CharT>>& operator<<( | ||
::std::basic_ostream<_CharT, ::std::char_traits<_CharT>>& __os, basic_string_view<_CharT, char_traits<_CharT>> __str) | ||
{ | ||
return __os << ::std::basic_string_view<_CharT, ::std::char_traits<_CharT>>{__str}; | ||
} | ||
#endif // !_CCCL_COMPILER(NVRTC) | ||
|
||
// literals | ||
|
||
|
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.
Adding
_CCCL_EXEC_CHECK_DISABLE
is necessary to allow using e. g. host only char_traitsThere 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 am a bit torn here, we should use host only
char_traits
as they will crash.I believe the solution with just casting to
::std::string_view
is the better solutionThere 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.
The problem is that the user can pass any type traits he wants, they might be host only, or device only..