1 // 2 // Copyright 2017 The Abseil Authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // https://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ 18 #define ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ 19 20 #include <algorithm> 21 #include <string> 22 #include <type_traits> 23 #include <utility> 24 25 #include "absl/base/port.h" 26 #include "absl/meta/type_traits.h" // for void_t 27 28 namespace absl { 29 ABSL_NAMESPACE_BEGIN 30 namespace strings_internal { 31 32 // Is a subclass of true_type or false_type, depending on whether or not 33 // T has a __resize_default_init member. 34 template <typename string_type, typename = void> 35 struct ResizeUninitializedTraits { 36 using HasMember = std::false_type; ResizeResizeUninitializedTraits37 static void Resize(string_type* s, size_t new_size) { s->resize(new_size); } 38 }; 39 40 // __resize_default_init is provided by libc++ >= 8.0 41 template <typename string_type> 42 struct ResizeUninitializedTraits< 43 string_type, absl::void_t<decltype(std::declval<string_type&>() 44 .__resize_default_init(237))> > { 45 using HasMember = std::true_type; 46 static void Resize(string_type* s, size_t new_size) { 47 s->__resize_default_init(new_size); 48 } 49 }; 50 51 // Returns true if the std::string implementation supports a resize where 52 // the new characters added to the std::string are left untouched. 53 // 54 // (A better name might be "STLStringSupportsUninitializedResize", alluding to 55 // the previous function.) 56 template <typename string_type> 57 inline constexpr bool STLStringSupportsNontrashingResize(string_type*) { 58 return ResizeUninitializedTraits<string_type>::HasMember::value; 59 } 60 61 // Like str->resize(new_size), except any new characters added to "*str" as a 62 // result of resizing may be left uninitialized, rather than being filled with 63 // '0' bytes. Typically used when code is then going to overwrite the backing 64 // store of the std::string with known data. 65 template <typename string_type, typename = void> 66 inline void STLStringResizeUninitialized(string_type* s, size_t new_size) { 67 ResizeUninitializedTraits<string_type>::Resize(s, new_size); 68 } 69 70 // Used to ensure exponential growth so that the amortized complexity of 71 // increasing the string size by a small amount is O(1), in contrast to 72 // O(str->size()) in the case of precise growth. 73 template <typename string_type> 74 void STLStringReserveAmortized(string_type* s, size_t new_size) { 75 const size_t cap = s->capacity(); 76 if (new_size > cap) { 77 // Make sure to always grow by at least a factor of 2x. 78 s->reserve((std::max)(new_size, 2 * cap)); 79 } 80 } 81 82 // Like STLStringResizeUninitialized(str, new_size), except guaranteed to use 83 // exponential growth so that the amortized complexity of increasing the string 84 // size by a small amount is O(1), in contrast to O(str->size()) in the case of 85 // precise growth. 86 template <typename string_type> 87 void STLStringResizeUninitializedAmortized(string_type* s, size_t new_size) { 88 STLStringReserveAmortized(s, new_size); 89 STLStringResizeUninitialized(s, new_size); 90 } 91 92 } // namespace strings_internal 93 ABSL_NAMESPACE_END 94 } // namespace absl 95 96 #endif // ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ 97