File indexing completed on 2025-12-16 09:41:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_
0018 #define ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_
0019
0020 #include <algorithm>
0021 #include <string>
0022 #include <type_traits>
0023 #include <utility>
0024
0025 #include "absl/base/port.h"
0026 #include "absl/meta/type_traits.h" // for void_t
0027
0028 namespace absl {
0029 ABSL_NAMESPACE_BEGIN
0030 namespace strings_internal {
0031
0032
0033
0034
0035 template <typename string_type, typename = void>
0036 struct ResizeUninitializedTraits {
0037 using HasMember = std::false_type;
0038 static void Resize(string_type* s, size_t new_size) { s->resize(new_size); }
0039 };
0040
0041
0042 template <typename string_type>
0043 struct ResizeUninitializedTraits<
0044 string_type, absl::void_t<decltype(std::declval<string_type&>()
0045 .__resize_default_init(237))> > {
0046 using HasMember = std::true_type;
0047 static void Resize(string_type* s, size_t new_size) {
0048 s->__resize_default_init(new_size);
0049 }
0050 };
0051
0052
0053
0054
0055
0056
0057 template <typename string_type>
0058 inline constexpr bool STLStringSupportsNontrashingResize(string_type*) {
0059 return ResizeUninitializedTraits<string_type>::HasMember::value;
0060 }
0061
0062
0063
0064
0065
0066 template <typename string_type, typename = void>
0067 inline void STLStringResizeUninitialized(string_type* s, size_t new_size) {
0068 ResizeUninitializedTraits<string_type>::Resize(s, new_size);
0069 }
0070
0071
0072
0073
0074 template <typename string_type>
0075 void STLStringReserveAmortized(string_type* s, size_t new_size) {
0076 const size_t cap = s->capacity();
0077 if (new_size > cap) {
0078
0079 s->reserve((std::max)(new_size, 2 * cap));
0080 }
0081 }
0082
0083
0084
0085 template <typename string_type, typename = void>
0086 struct AppendUninitializedTraits {
0087 static void Append(string_type* s, size_t n) {
0088 s->append(n, typename string_type::value_type());
0089 }
0090 };
0091
0092 template <typename string_type>
0093 struct AppendUninitializedTraits<
0094 string_type, absl::void_t<decltype(std::declval<string_type&>()
0095 .__append_default_init(237))> > {
0096 static void Append(string_type* s, size_t n) {
0097 s->__append_default_init(n);
0098 }
0099 };
0100
0101
0102
0103
0104
0105 template <typename string_type>
0106 void STLStringResizeUninitializedAmortized(string_type* s, size_t new_size) {
0107 const size_t size = s->size();
0108 if (new_size > size) {
0109 AppendUninitializedTraits<string_type>::Append(s, new_size - size);
0110 } else {
0111 s->erase(new_size);
0112 }
0113 }
0114
0115 }
0116 ABSL_NAMESPACE_END
0117 }
0118
0119 #endif