• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
21 #include <type_traits>
22 #include <utility>
23 
24 #include "absl/base/port.h"
25 #include "absl/meta/type_traits.h"  //  for void_t
26 
27 namespace absl {
28 ABSL_NAMESPACE_BEGIN
29 namespace strings_internal {
30 
31 // Is a subclass of true_type or false_type, depending on whether or not
32 // T has a __resize_default_init member.
33 template <typename string_type, typename = void>
34 struct ResizeUninitializedTraits {
35   using HasMember = std::false_type;
ResizeResizeUninitializedTraits36   static void Resize(string_type* s, size_t new_size) { s->resize(new_size); }
37 };
38 
39 // __resize_default_init is provided by libc++ >= 8.0
40 template <typename string_type>
41 struct ResizeUninitializedTraits<
42     string_type, absl::void_t<decltype(std::declval<string_type&>()
43                                            .__resize_default_init(237))> > {
44   using HasMember = std::true_type;
45   static void Resize(string_type* s, size_t new_size) {
46     s->__resize_default_init(new_size);
47   }
48 };
49 
50 // Returns true if the std::string implementation supports a resize where
51 // the new characters added to the std::string are left untouched.
52 //
53 // (A better name might be "STLStringSupportsUninitializedResize", alluding to
54 // the previous function.)
55 template <typename string_type>
56 inline constexpr bool STLStringSupportsNontrashingResize(string_type*) {
57   return ResizeUninitializedTraits<string_type>::HasMember::value;
58 }
59 
60 // Like str->resize(new_size), except any new characters added to "*str" as a
61 // result of resizing may be left uninitialized, rather than being filled with
62 // '0' bytes. Typically used when code is then going to overwrite the backing
63 // store of the std::string with known data.
64 template <typename string_type, typename = void>
65 inline void STLStringResizeUninitialized(string_type* s, size_t new_size) {
66   ResizeUninitializedTraits<string_type>::Resize(s, new_size);
67 }
68 
69 }  // namespace strings_internal
70 ABSL_NAMESPACE_END
71 }  // namespace absl
72 
73 #endif  // ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_
74