strcat.hpp
Go to the documentation of this file.
1 #ifndef STRCAT_HPP
2 #define STRCAT_HPP
3 
4 #include <string_view>
5 #include <string>
6 
7 namespace BT
8 {
9 
10 // -----------------------------------------------------------------------------
11 // StrCat()
12 // -----------------------------------------------------------------------------
13 //
14 // Merges given strings, using no delimiter(s).
15 //
16 // `StrCat()` is designed to be the fastest possible way to construct a string
17 // out of a mix of raw C strings, string_views, strings.
18 
19 namespace strings_internal
20 {
21 
22 inline void AppendPieces(std::string* dest,
23  std::initializer_list<std::string_view> pieces)
24 {
25  size_t size = 0;
26  for(const auto& piece : pieces)
27  {
28  size += piece.size();
29  }
30  dest->reserve(dest->size() + size);
31  for(const auto& piece : pieces)
32  {
33  dest->append(piece.data(), piece.size());
34  }
35 }
36 
37 inline std::string CatPieces(std::initializer_list<std::string_view> pieces)
38 {
39  std::string out;
40  AppendPieces(&out, std::move(pieces));
41  return out;
42 }
43 
44 } // namespace strings_internal
45 
46 inline std::string StrCat()
47 {
48  return std::string();
49 }
50 
51 inline std::string StrCat(const std::string_view& a)
52 {
53  return std::string(a.data(), a.size());
54 }
55 
56 inline std::string StrCat(const std::string_view& a, const std::string_view& b)
57 {
58  return strings_internal::CatPieces({ a, b });
59 }
60 
61 inline std::string StrCat(const std::string_view& a, const std::string_view& b,
62  const std::string_view& c)
63 {
64  return strings_internal::CatPieces({ a, b, c });
65 }
66 
67 // Support 4 or more arguments
68 template <typename... AV>
69 inline std::string StrCat(const std::string_view& a, const std::string_view& b,
70  const std::string_view& c, const std::string_view& d,
71  const AV&... args)
72 {
74  { a, b, c, d, static_cast<const std::string_view&>(args)... });
75 }
76 
77 //-----------------------------------------------
78 
79 inline void StrAppend(std::string* destination, const std::string_view& a)
80 {
81  destination->append(a.data(), a.size());
82 }
83 
84 inline void StrAppend(std::string* destination, const std::string_view& a,
85  const std::string_view& b)
86 {
87  strings_internal::AppendPieces(destination, { a, b });
88 }
89 
90 inline void StrAppend(std::string* destination, const std::string_view& a,
91  const std::string_view& b, const std::string_view& c)
92 {
93  strings_internal::AppendPieces(destination, { a, b, c });
94 }
95 
96 // Support 4 or more arguments
97 template <typename... AV>
98 inline void StrAppend(std::string* destination, const std::string_view& a,
99  const std::string_view& b, const std::string_view& c,
100  const std::string_view& d, const AV&... args)
101 {
103  destination, { a, b, c, d, static_cast<const std::string_view&>(args)... });
104 }
105 
106 } // namespace BT
107 
108 #endif // STRCAT_HPP
cx::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: wildcards.hpp:636
BT
Definition: ex01_wrap_legacy.cpp:29
BT::strings_internal::CatPieces
std::string CatPieces(std::initializer_list< std::string_view > pieces)
Definition: strcat.hpp:37
BT::StrAppend
void StrAppend(std::string *destination, const std::string_view &a)
Definition: strcat.hpp:79
BT::strings_internal::AppendPieces
void AppendPieces(std::string *dest, std::initializer_list< std::string_view > pieces)
Definition: strcat.hpp:22
BT::StrCat
std::string StrCat()
Definition: strcat.hpp:46
lexy::_detail::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:184


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08