-
Notifications
You must be signed in to change notification settings - Fork 414
[libcu++] Implement cuda::std::format_to_n
#9482
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
Merged
+257
−1
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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) 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _CUDA_STD___FORMAT_FORMAT_TO_N_H | ||
| #define _CUDA_STD___FORMAT_FORMAT_TO_N_H | ||
|
|
||
| #include <cuda/std/detail/__config> | ||
|
|
||
| #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) | ||
| # pragma GCC system_header | ||
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) | ||
| # pragma clang system_header | ||
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) | ||
| # pragma system_header | ||
| #endif // no system header | ||
|
|
||
| #include <cuda/std/__cstddef/types.h> | ||
| #include <cuda/std/__format/buffer.h> | ||
| #include <cuda/std/__format/format_args.h> | ||
| #include <cuda/std/__format/format_context.h> | ||
| #include <cuda/std/__format/format_parse_context.h> | ||
| #include <cuda/std/__format/vformat_to.h> | ||
| #include <cuda/std/__iterator/incrementable_traits.h> | ||
| #include <cuda/std/__memory/addressof.h> | ||
| #include <cuda/std/__utility/cmp.h> | ||
| #include <cuda/std/__utility/ctad_support.h> | ||
| #include <cuda/std/__utility/move.h> | ||
| #include <cuda/std/string_view> | ||
|
|
||
| #include <cuda/std/__cccl/prologue.h> | ||
|
|
||
| _CCCL_BEGIN_NAMESPACE_CUDA_STD | ||
|
|
||
| template <class _OutIt> | ||
| struct _CCCL_TYPE_VISIBILITY_DEFAULT format_to_n_result | ||
| { | ||
| _OutIt out; | ||
| iter_difference_t<_OutIt> size; | ||
| }; | ||
| _CCCL_CTAD_SUPPORTED_FOR_TYPE(format_to_n_result); | ||
|
|
||
| // A buffer that counts and limits the number of insertions. | ||
| template <class _OutIt, class _CharT> | ||
| class __fmt_format_to_n_buffer : __fmt_buffer_select_t<_OutIt, _CharT> | ||
| { | ||
| __fmt_max_output_size __max_output_size_; | ||
|
|
||
| public: | ||
| using _Base _CCCL_NODEBUG_ALIAS = __fmt_buffer_select_t<_OutIt, _CharT>; | ||
|
|
||
| _CCCL_API constexpr __fmt_format_to_n_buffer(_OutIt __out_it, iter_difference_t<_OutIt> __n) | ||
| : _Base{::cuda::std::move(__out_it), &__max_output_size_} | ||
| , __max_output_size_{::cuda::std::cmp_less(__n, 0) ? size_t{0} : static_cast<size_t>(__n)} | ||
| {} | ||
|
|
||
| [[nodiscard]] _CCCL_API constexpr auto __make_output_iterator() | ||
| { | ||
| return _Base::__make_output_iterator(); | ||
| } | ||
|
|
||
| [[nodiscard]] _CCCL_API constexpr format_to_n_result<_OutIt> __result() && | ||
| { | ||
| return {static_cast<_Base&&>(*this).__out_it(), | ||
| static_cast<iter_difference_t<_OutIt>>(__max_output_size_.__code_units_written())}; | ||
| } | ||
| }; | ||
|
|
||
| template <class _Context, class _OutIt, class _CharT> | ||
| [[nodiscard]] _CCCL_API format_to_n_result<_OutIt> __format_to_n_impl( | ||
| _OutIt __out_it, iter_difference_t<_OutIt> __n, basic_string_view<_CharT> __fmt, basic_format_args<_Context> __args) | ||
| { | ||
| __fmt_format_to_n_buffer<_OutIt, _CharT> __buffer{::cuda::std::move(__out_it), __n}; | ||
| (void) ::cuda::std::__fmt_vformat_to( | ||
| basic_format_parse_context{__fmt, __args.__size()}, | ||
| ::cuda::std::__fmt_make_format_context(__buffer.__make_output_iterator(), __args)); | ||
| return ::cuda::std::move(__buffer).__result(); | ||
| } | ||
|
|
||
| _CCCL_TEMPLATE(class _OutIt, class... _Args) | ||
| _CCCL_REQUIRES(output_iterator<_OutIt, const char&>) | ||
| /*discard*/ _CCCL_API format_to_n_result<_OutIt> | ||
| format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, format_string<_Args...> __fmt, _Args&&... __args) | ||
| { | ||
| return ::cuda::std::__format_to_n_impl<format_context>( | ||
| ::cuda::std::move(__out_it), __n, __fmt.get(), ::cuda::std::make_format_args(__args...)); | ||
| } | ||
|
|
||
| #if _CCCL_HAS_WCHAR_T() | ||
| _CCCL_TEMPLATE(class _OutIt, class... _Args) | ||
| _CCCL_REQUIRES(output_iterator<_OutIt, const wchar_t&>) | ||
| /*discard*/ _CCCL_API format_to_n_result<_OutIt> | ||
| format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, wformat_string<_Args...> __fmt, _Args&&... __args) | ||
| { | ||
| return ::cuda::std::__format_to_n_impl<wformat_context>( | ||
| ::cuda::std::move(__out_it), __n, __fmt.get(), ::cuda::std::make_wformat_args(__args...)); | ||
| } | ||
| #endif // _CCCL_HAS_WCHAR_T() | ||
|
|
||
| _CCCL_END_NAMESPACE_CUDA_STD | ||
|
|
||
| #include <cuda/std/__cccl/epilogue.h> | ||
|
|
||
| #endif // _CUDA_STD___FORMAT_FORMAT_TO_N_H |
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
106 changes: 106 additions & 0 deletions
106
libcudacxx/test/libcudacxx/std/text/format/format.functions/checkers/format_to_n.h
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,106 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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) 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <cuda/functional> | ||
| #include <cuda/std/__format_> | ||
| #include <cuda/std/algorithm> | ||
| #include <cuda/std/inplace_vector> | ||
| #include <cuda/std/iterator> | ||
| #include <cuda/std/string_view> | ||
| #include <cuda/std/utility> | ||
|
|
||
| #include "format_functions_common.h" | ||
| #include "test_macros.h" | ||
|
|
||
| // Marking checkers with _CCCL_NOINLINE greatly improves ptxas compile times. | ||
|
|
||
| template <class CharT, class... Args> | ||
| TEST_FUNC _CCCL_NOINLINE bool | ||
| check(cuda::std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) | ||
| { | ||
| assert(expected.size() < 4096 && "Update the size of the buffer."); | ||
| { | ||
| cuda::std::inplace_vector<CharT, 4096> out; | ||
| const auto result = | ||
| cuda::std::format_to_n(cuda::std::__back_insert_iterator{out}, 0, fmt, cuda::std::forward<Args>(args)...); | ||
| if (cuda::std::cmp_not_equal(result.size, expected.size())) | ||
| { | ||
| return false; | ||
| } | ||
| if (!out.empty()) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| { | ||
| constexpr auto n = 5; | ||
| cuda::std::inplace_vector<CharT, 4096> out; | ||
| const auto result = | ||
| cuda::std::format_to_n(cuda::std::__back_insert_iterator{out}, n, fmt, cuda::std::forward<Args>(args)...); | ||
| if (cuda::std::cmp_not_equal(result.size, expected.size())) | ||
| { | ||
| return false; | ||
| } | ||
| const auto size = cuda::std::min(cuda::std::size_t{n}, expected.size()); | ||
| if (!cuda::std::equal(out.begin(), out.end(), expected.begin(), expected.begin() + size)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| { | ||
| constexpr auto n = 10; | ||
| cuda::std::inplace_vector<CharT, 4096> out(n, CharT{' '}); | ||
| const auto result = cuda::std::format_to_n(out.begin(), n, fmt, cuda::std::forward<Args>(args)...); | ||
| if (cuda::std::cmp_not_equal(result.size, expected.size())) | ||
| { | ||
| return false; | ||
| } | ||
| const auto size = cuda::std::min(cuda::std::size_t{n}, expected.size()); | ||
| if (result.out != out.begin() + size) | ||
| { | ||
| return false; | ||
| } | ||
| if (!cuda::std::equal(out.begin(), out.begin() + size, expected.begin(), expected.begin() + size)) | ||
| { | ||
| return false; | ||
| } | ||
| if (!cuda::std::all_of(out.begin() + size, out.end(), cuda::equal_to_value{CharT{' '}})) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| { | ||
| static_assert(cuda::std::is_signed_v<cuda::std::iter_difference_t<CharT*>>); | ||
| CharT out[]{CharT{0}}; | ||
| const auto result = cuda::std::format_to_n(out, -1, fmt, cuda::std::forward<Args>(args)...); | ||
| if (cuda::std::cmp_not_equal(result.size, expected.size())) | ||
| { | ||
| return false; | ||
| } | ||
| if (result.out != out) | ||
| { | ||
| return false; | ||
| } | ||
| if (out[0] != CharT{0}) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| template <class CharT, class... Args> | ||
| TEST_FUNC bool check_exception(cuda::std::string_view, cuda::std::basic_string_view<CharT>, Args&&...) | ||
| { | ||
| // After P2216 most exceptions thrown by std::format become ill-formed. | ||
| // Therefore this tests does nothing. | ||
| // A basic ill-formed test is done in format.verify.cpp | ||
| // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument. | ||
| return true; | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
libcudacxx/test/libcudacxx/std/text/format/format.functions/format_to_n.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,38 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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) 2026 NVIDIA CORPORATION & AFFILIATES. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // <cuda/std/format> | ||
|
|
||
| // template<class Out, class... Args> | ||
| // format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, | ||
| // format-string<Args...> fmt, const Args&... args); | ||
| // template<class Out, class... Args> | ||
| // format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, | ||
| // wformat-string<Args...> fmt, const Args&... args); | ||
|
|
||
| #include <cuda/std/__format_> | ||
|
davebayer marked this conversation as resolved.
|
||
| #include <cuda/std/iterator> | ||
| #include <cuda/std/type_traits> | ||
| #include <cuda/std/utility> | ||
|
|
||
| static_assert(cuda::std::is_same_v< | ||
| cuda::std::format_to_n_result<char*>, | ||
| decltype(cuda::std::format_to_n(cuda::std::declval<char*>(), cuda::std::iter_difference_t<char*>{}, ""))>); | ||
| static_assert(!noexcept(cuda::std::format_to_n(cuda::std::declval<char*>(), cuda::std::iter_difference_t<char*>{}, ""))); | ||
| #if _CCCL_HAS_WCHAR_T() | ||
| static_assert( | ||
| cuda::std::is_same_v< | ||
| cuda::std::format_to_n_result<wchar_t*>, | ||
| decltype(cuda::std::format_to_n(cuda::std::declval<wchar_t*>(), cuda::std::iter_difference_t<wchar_t*>{}, L""))>); | ||
| static_assert( | ||
| !noexcept(cuda::std::format_to_n(cuda::std::declval<wchar_t*>(), cuda::std::iter_difference_t<wchar_t*>{}, L""))); | ||
| #endif // _CCCL_HAS_WCHAR_T() | ||
|
|
||
| #include "checkers/format_to_n.h" | ||
| #include "tests/smoke.h" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.