1 // 2 // Copyright 2018 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_CORD_TEST_HELPERS_H_ 18 #define ABSL_STRINGS_CORD_TEST_HELPERS_H_ 19 20 #include "absl/strings/cord.h" 21 22 namespace absl { 23 ABSL_NAMESPACE_BEGIN 24 25 // Creates a multi-segment Cord from an iterable container of strings. The 26 // resulting Cord is guaranteed to have one segment for every string in the 27 // container. This allows code to be unit tested with multi-segment Cord 28 // inputs. 29 // 30 // Example: 31 // 32 // absl::Cord c = absl::MakeFragmentedCord({"A ", "fragmented ", "Cord"}); 33 // EXPECT_FALSE(c.GetFlat(&unused)); 34 // 35 // The mechanism by which this Cord is created is an implementation detail. Any 36 // implementation that produces a multi-segment Cord may produce a flat Cord in 37 // the future as new optimizations are added to the Cord class. 38 // MakeFragmentedCord will, however, always be updated to return a multi-segment 39 // Cord. 40 template <typename Container> MakeFragmentedCord(const Container & c)41Cord MakeFragmentedCord(const Container& c) { 42 Cord result; 43 for (const auto& s : c) { 44 auto* external = new std::string(s); 45 Cord tmp = absl::MakeCordFromExternal( 46 *external, [external](absl::string_view) { delete external; }); 47 tmp.Prepend(result); 48 result = tmp; 49 } 50 return result; 51 } 52 MakeFragmentedCord(std::initializer_list<absl::string_view> list)53inline Cord MakeFragmentedCord(std::initializer_list<absl::string_view> list) { 54 return MakeFragmentedCord<std::initializer_list<absl::string_view>>(list); 55 } 56 57 ABSL_NAMESPACE_END 58 } // namespace absl 59 60 #endif // ABSL_STRINGS_CORD_TEST_HELPERS_H_ 61