-
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
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 |
---|---|---|
|
@@ -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)} | ||
|
@@ -262,6 +263,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 +290,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 +353,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 +383,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 +416,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 +449,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 +483,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 +517,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 +533,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 +551,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); | ||
|
@@ -740,14 +751,19 @@ _CCCL_HOST_DEVICE basic_string_view(_Range&&) -> basic_string_view<_CUDA_VRANGES | |
|
||
// operator << | ||
|
||
#if 0 // todo: we need to implement char_traits stream types & functions | ||
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.data(), __str.size()}; | ||
} | ||
|
||
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.data(), __str.size()}; | ||
davebayer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
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've had to split the implementation to map the |
||
#endif // 0 | ||
|
||
// literals | ||
|
||
|
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
120 changes: 120 additions & 0 deletions
120
libcudacxx/test/libcudacxx/std/strings/string.view/string.view.io/stream_insert.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,120 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <cuda/std/string_view> | ||
|
||
// template<class charT, class traits, class Allocator> | ||
// basic_ostream<charT, traits>& | ||
// operator<<(basic_ostream<charT, traits>& os, | ||
// const basic_string_view<charT,traits> str); | ||
|
||
#include <cuda/std/cassert> | ||
#include <cuda/std/string_view> | ||
#include <cuda/std/type_traits> | ||
|
||
#include <sstream> | ||
|
||
#include "literal.h" | ||
|
||
template <class CharT> | ||
void test_with_default_type_traits() | ||
{ | ||
using OS = std::basic_ostringstream<CharT>; | ||
using SV = 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 OS::char_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename OS::traits_type, std::char_traits<CharT>>); | ||
static_assert(cuda::std::is_same_v<typename SV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename SV::traits_type, cuda::std::char_traits<CharT>>); | ||
|
||
const CharT* str = TEST_STRLIT(CharT, "some text"); | ||
|
||
// 1. test basic write without formatting | ||
{ | ||
OS out{}; | ||
SV sv{str}; | ||
|
||
out << sv; | ||
assert(out.good()); | ||
assert(out.str() == str); | ||
} | ||
|
||
// 2. test basic write with formatting | ||
{ | ||
OS out{}; | ||
SV sv{str}; | ||
|
||
out.width(12); | ||
out << sv; | ||
assert(out.good()); | ||
assert(out.str() == TEST_STRLIT(CharT, " some text")); | ||
} | ||
} | ||
|
||
template <class CharT> | ||
struct custom_type_traits : std::char_traits<CharT> | ||
{}; | ||
|
||
template <class CharT> | ||
void test_with_custom_type_traits() | ||
{ | ||
using OS = std::basic_ostringstream<CharT, custom_type_traits<CharT>>; | ||
using SV = 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 OS::char_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename OS::traits_type, custom_type_traits<CharT>>); | ||
static_assert(cuda::std::is_same_v<typename SV::value_type, CharT>); | ||
static_assert(cuda::std::is_same_v<typename SV::traits_type, custom_type_traits<CharT>>); | ||
|
||
const CharT* str = TEST_STRLIT(CharT, "some text"); | ||
|
||
// 1. test basic write without formatting | ||
{ | ||
OS out{}; | ||
SV sv{str}; | ||
|
||
out << sv; | ||
assert(out.good()); | ||
assert(out.str() == str); | ||
} | ||
|
||
// 2. test basic write with formatting | ||
{ | ||
OS out{}; | ||
SV sv{str}; | ||
|
||
out.width(12); | ||
out << sv; | ||
assert(out.good()); | ||
assert(out.str() == TEST_STRLIT(CharT, " some text")); | ||
} | ||
} | ||
|
||
template <class CharT> | ||
void test_type() | ||
{ | ||
test_with_default_type_traits<CharT>(); | ||
test_with_custom_type_traits<CharT>(); | ||
} | ||
|
||
void test() | ||
{ | ||
test_type<char>(); | ||
#if _CCCL_HAS_WCHAR_T() | ||
test_type<wchar_t>(); | ||
#endif // _CCCL_HAS_WCHAR_T() | ||
} | ||
|
||
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.
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..