BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
strcat.hpp
1#ifndef STRCAT_HPP
2#define STRCAT_HPP
3
4#include <string>
5#include <string_view>
6
7namespace 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
19namespace strings_internal
20{
21
22inline 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
37inline 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
46inline std::string StrCat()
47{
48 return std::string();
49}
50
51inline std::string StrCat(const std::string_view& a)
52{
53 return std::string(a.data(), a.size());
54}
55
56inline std::string StrCat(const std::string_view& a, const std::string_view& b)
57{
58 return strings_internal::CatPieces({ a, b });
59}
60
61inline 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
68template <typename... AV>
69inline 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{
73 return strings_internal::CatPieces(
74 { a, b, c, d, static_cast<const std::string_view&>(args)... });
75}
76
77//-----------------------------------------------
78
79inline void StrAppend(std::string* destination, const std::string_view& a)
80{
81 destination->append(a.data(), a.size());
82}
83
84inline 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
90inline 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
97template <typename... AV>
98inline 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{
102 strings_internal::AppendPieces(
103 destination, { a, b, c, d, static_cast<const std::string_view&>(args)... });
104}
105
106} // namespace BT
107
108#endif // STRCAT_HPP
Definition: action_node.h:24