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


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:05