|
| 1 | +// Copyright 2025 The Abseil Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// ----------------------------------------------------------------------------- |
| 16 | +// File: resize_and_overwrite.h |
| 17 | +// ----------------------------------------------------------------------------- |
| 18 | +// |
| 19 | +// This file contains a polyfill for C++23's |
| 20 | +// std::basic_string<CharT,Traits,Allocator>::resize_and_overwrite |
| 21 | +// |
| 22 | +// The polyfill takes the form of a free function: |
| 23 | + |
| 24 | +// template<typename T, typename Op> |
| 25 | +// void StringResizeAndOverwrite(T& str, typename T::size_type count, Op op); |
| 26 | +// |
| 27 | +// This avoids the cost of initializing a suitably-sized std::string when it is |
| 28 | +// intended to be used as a char array, for example, to be populated by a |
| 29 | +// C-style API. |
| 30 | +// |
| 31 | +// Example usage: |
| 32 | +// |
| 33 | +// std::string IntToString(int n) { |
| 34 | +// std::string result; |
| 35 | +// constexpr size_t kMaxIntChars = 10; |
| 36 | +// absl::StringResizeAndOverwrite( |
| 37 | +// result, kMaxIntChars, [n](char* buffer, size_t buffer_size) { |
| 38 | +// return snprintf(buffer, buffer_size, "%d", n); |
| 39 | +// }); |
| 40 | +// return result; |
| 41 | +// } |
| 42 | +// |
| 43 | +// https://en.cppreference.com/w/cpp/string/basic_string/resize_and_overwrite.html |
| 44 | +// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1072r10.html |
| 45 | + |
| 46 | +#ifndef ABSL_STRINGS_RESIZE_AND_OVERWRITE_H_ |
| 47 | +#define ABSL_STRINGS_RESIZE_AND_OVERWRITE_H_ |
| 48 | + |
| 49 | +#include <cstddef> |
| 50 | +#include <string> // IWYU pragma: keep |
| 51 | +#include <type_traits> |
| 52 | +#include <utility> |
| 53 | + |
| 54 | +#include "absl/base/config.h" |
| 55 | +#include "absl/base/internal/throw_delegate.h" |
| 56 | +#include "absl/base/macros.h" |
| 57 | +#include "absl/base/optimization.h" |
| 58 | + |
| 59 | +#if defined(__cpp_lib_string_resize_and_overwrite) && \ |
| 60 | + __cpp_lib_string_resize_and_overwrite >= 202110L |
| 61 | +#define ABSL_INTERNAL_HAS_RESIZE_AND_OVERWRITE 1 |
| 62 | +#endif |
| 63 | + |
| 64 | +namespace absl { |
| 65 | +ABSL_NAMESPACE_BEGIN |
| 66 | + |
| 67 | +namespace strings_internal { |
| 68 | + |
| 69 | +#ifndef ABSL_INTERNAL_HAS_RESIZE_AND_OVERWRITE |
| 70 | + |
| 71 | +inline size_t ProbeResizeAndOverwriteOp(char*, size_t) { return 0; } |
| 72 | + |
| 73 | +// Prior to C++23, Google's libc++ backports resize_and_overwrite as |
| 74 | +// __google_nonstandard_backport_resize_and_overwrite |
| 75 | +template <typename T, typename = void> |
| 76 | +struct has__google_nonstandard_backport_resize_and_overwrite : std::false_type { |
| 77 | +}; |
| 78 | + |
| 79 | +template <typename T> |
| 80 | +struct has__google_nonstandard_backport_resize_and_overwrite< |
| 81 | + T, |
| 82 | + std::void_t< |
| 83 | + decltype(std::declval<T&>() |
| 84 | + .__google_nonstandard_backport_resize_and_overwrite( |
| 85 | + std::declval<size_t>(), ProbeResizeAndOverwriteOp))>> |
| 86 | + : std::true_type {}; |
| 87 | + |
| 88 | +// Prior to C++23, the version of libstdc++ that shipped with GCC >= 14 |
| 89 | +// has __resize_and_overwrite. |
| 90 | +template <typename T, typename = void> |
| 91 | +struct has__resize_and_overwrite : std::false_type {}; |
| 92 | + |
| 93 | +template <typename T> |
| 94 | +struct has__resize_and_overwrite< |
| 95 | + T, std::void_t<decltype(std::declval<T&>().__resize_and_overwrite( |
| 96 | + std::declval<size_t>(), ProbeResizeAndOverwriteOp))>> |
| 97 | + : std::true_type {}; |
| 98 | + |
| 99 | +// libc++ used __resize_default_init to achieve uninitialized string resizes |
| 100 | +// before removing it September 2025, in favor of resize_and_overwrite. |
| 101 | +// https://github.com/llvm/llvm-project/commit/92f5d8df361bb1bb6dea88f86faeedfd295ab970 |
| 102 | +template <typename T, typename = void> |
| 103 | +struct has__resize_default_init : std::false_type {}; |
| 104 | + |
| 105 | +template <typename T> |
| 106 | +struct has__resize_default_init< |
| 107 | + T, std::void_t<decltype(std::declval<T&>().__resize_default_init(42))>> |
| 108 | + : std::true_type {}; |
| 109 | + |
| 110 | +// Prior to C++23, some versions of MSVC have _Resize_and_overwrite. |
| 111 | +template <typename T, typename = void> |
| 112 | +struct has_Resize_and_overwrite : std::false_type {}; |
| 113 | + |
| 114 | +template <typename T> |
| 115 | +struct has_Resize_and_overwrite< |
| 116 | + T, std::void_t<decltype(std::declval<T&>()._Resize_and_overwrite( |
| 117 | + std::declval<size_t>(), ProbeResizeAndOverwriteOp))>> |
| 118 | + : std::true_type {}; |
| 119 | + |
| 120 | +#endif // ifndef ABSL_INTERNAL_HAS_RESIZE_AND_OVERWRITE |
| 121 | + |
| 122 | +// A less-efficient fallback implementation that uses resize(). |
| 123 | +template <typename T, typename Op> |
| 124 | +void StringResizeAndOverwriteFallback(T& str, typename T::size_type n, Op op) { |
| 125 | + if (ABSL_PREDICT_FALSE(n > str.max_size())) { |
| 126 | + absl::base_internal::ThrowStdLengthError("absl::StringResizeAndOverwrite"); |
| 127 | + } |
| 128 | + // The callback is allowed to write an arbitrary value to buf+n, but it is |
| 129 | + // undefined behavior to write anything other than T::value_type{} to |
| 130 | + // str.data()[n]. Therefore the initial resize uses an extra byte. |
| 131 | + str.resize(n + 1); |
| 132 | + auto new_size = std::move(op)(str.data(), n); |
| 133 | + ABSL_HARDENING_ASSERT(new_size >= 0 && new_size <= n); |
| 134 | + str.erase(static_cast<typename T::size_type>(new_size)); |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace strings_internal |
| 138 | + |
| 139 | +// Resizes `str` to contain at most `n` characters, using the user-provided |
| 140 | +// operation `op` to modify the possibly indeterminate contents. `op` must |
| 141 | +// return the finalized length of `str`. Note that `op` is allowed write to |
| 142 | +// `data()[n]`, which facilitiates interoperation with functions that write a |
| 143 | +// trailing NUL. |
| 144 | +template <typename T, typename Op> |
| 145 | +void StringResizeAndOverwrite(T& str, typename T::size_type n, Op op) { |
| 146 | +#ifdef ABSL_INTERNAL_HAS_RESIZE_AND_OVERWRITE |
| 147 | + str.resize_and_overwrite(n, std::move(op)); |
| 148 | +#else |
| 149 | + if constexpr (strings_internal:: |
| 150 | + has__google_nonstandard_backport_resize_and_overwrite< |
| 151 | + T>::value) { |
| 152 | + str.__google_nonstandard_backport_resize_and_overwrite(n, std::move(op)); |
| 153 | + } else if constexpr (strings_internal::has__resize_and_overwrite<T>::value) { |
| 154 | + str.__resize_and_overwrite(n, std::move(op)); |
| 155 | + } else if constexpr (strings_internal::has__resize_default_init<T>::value) { |
| 156 | + str.__resize_default_init(n); |
| 157 | + str.__resize_default_init( |
| 158 | + static_cast<typename T::size_type>(std::move(op)(str.data(), n))); |
| 159 | + } else if constexpr (strings_internal::has_Resize_and_overwrite<T>::value) { |
| 160 | + str._Resize_and_overwrite(n, std::move(op)); |
| 161 | + } else { |
| 162 | + strings_internal::StringResizeAndOverwriteFallback(str, n, op); |
| 163 | + } |
| 164 | +#endif |
| 165 | +} |
| 166 | + |
| 167 | +ABSL_NAMESPACE_END |
| 168 | +} // namespace absl |
| 169 | + |
| 170 | +#undef ABSL_INTERNAL_HAS_RESIZE_AND_OVERWRITE |
| 171 | + |
| 172 | +#endif // ABSL_STRINGS_RESIZE_AND_OVERWRITE_H_ |
0 commit comments