Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:00

0001 //
0002 // Copyright 2017 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 //      https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
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 // In this type trait, we look for a __resize_default_init member function, and
0033 // we use it if available, otherwise, we use resize. We provide HasMember to
0034 // indicate whether __resize_default_init is present.
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 // __resize_default_init is provided by libc++ >= 8.0
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 // Returns true if the std::string implementation supports a resize where
0053 // the new characters added to the std::string are left untouched.
0054 //
0055 // (A better name might be "STLStringSupportsUninitializedResize", alluding to
0056 // the previous function.)
0057 template <typename string_type>
0058 inline constexpr bool STLStringSupportsNontrashingResize(string_type*) {
0059   return ResizeUninitializedTraits<string_type>::HasMember::value;
0060 }
0061 
0062 // Like str->resize(new_size), except any new characters added to "*str" as a
0063 // result of resizing may be left uninitialized, rather than being filled with
0064 // '0' bytes. Typically used when code is then going to overwrite the backing
0065 // store of the std::string with known data.
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 // Used to ensure exponential growth so that the amortized complexity of
0072 // increasing the string size by a small amount is O(1), in contrast to
0073 // O(str->size()) in the case of precise growth.
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     // Make sure to always grow by at least a factor of 2x.
0079     s->reserve((std::max)(new_size, 2 * cap));
0080   }
0081 }
0082 
0083 // In this type trait, we look for an __append_default_init member function, and
0084 // we use it if available, otherwise, we use append.
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 // Like STLStringResizeUninitialized(str, new_size), except guaranteed to use
0102 // exponential growth so that the amortized complexity of increasing the string
0103 // size by a small amount is O(1), in contrast to O(str->size()) in the case of
0104 // precise growth.
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 }  // namespace strings_internal
0116 ABSL_NAMESPACE_END
0117 }  // namespace absl
0118 
0119 #endif  // ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_