Go to the documentation of this file.00001 #ifndef STRCAT_HPP
00002 #define STRCAT_HPP
00003
00004 #include "string_view.hpp"
00005 #include <array>
00006 #include <cstdint>
00007 #include <string>
00008 #include <type_traits>
00009
00010 namespace BT {
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 namespace strings_internal {
00022
00023 inline void AppendPieces(std::string* dest,
00024 std::initializer_list<nonstd::string_view> pieces)
00025 {
00026 size_t size = 0;
00027 for (const auto& piece: pieces)
00028 {
00029 size += piece.size();
00030 }
00031 dest->reserve(dest->size() + size);
00032 for (const auto& piece: pieces)
00033 {
00034 dest->append( piece.data(), piece.size() );
00035 }
00036 }
00037
00038 inline std::string CatPieces(std::initializer_list<nonstd::string_view> pieces)
00039 {
00040 std::string out;
00041 AppendPieces(&out, std::move(pieces));
00042 return out;
00043 }
00044
00045 }
00046
00047 inline std::string StrCat() { return std::string(); }
00048
00049 inline std::string StrCat(const nonstd::string_view& a) {
00050 return std::string(a.data(), a.size());
00051 }
00052
00053 inline std::string StrCat(const nonstd::string_view& a,
00054 const nonstd::string_view& b)
00055 {
00056 return strings_internal::CatPieces( {a, b} );
00057 }
00058
00059 inline std::string StrCat(const nonstd::string_view& a,
00060 const nonstd::string_view& b,
00061 const nonstd::string_view& c)
00062 {
00063 return strings_internal::CatPieces( {a, b, c} );
00064 }
00065
00066
00067 template <typename... AV>
00068 inline std::string StrCat(const nonstd::string_view& a,
00069 const nonstd::string_view& b,
00070 const nonstd::string_view& c,
00071 const nonstd::string_view& d,
00072 const AV&... args)
00073 {
00074 return strings_internal::CatPieces( {a, b, c, d, static_cast<const nonstd::string_view&>(args)...});
00075 }
00076
00077
00078
00079
00080 inline void StrAppend(std::string* destination,
00081 const nonstd::string_view& a)
00082 {
00083 destination->append( a.data(), a.size());
00084 }
00085
00086 inline void StrAppend(std::string* destination,
00087 const nonstd::string_view& a,
00088 const nonstd::string_view& b)
00089 {
00090 strings_internal::AppendPieces( destination, {a, b} );
00091 }
00092
00093 inline void StrAppend(std::string* destination,
00094 const nonstd::string_view& a,
00095 const nonstd::string_view& b,
00096 const nonstd::string_view& c)
00097 {
00098 strings_internal::AppendPieces( destination, {a, b, c} );
00099 }
00100
00101
00102 template <typename... AV>
00103 inline void StrAppend(std::string* destination,
00104 const nonstd::string_view& a,
00105 const nonstd::string_view& b,
00106 const nonstd::string_view& c,
00107 const nonstd::string_view& d,
00108 const AV&... args)
00109 {
00110 strings_internal::AppendPieces( destination, {a, b, c, d, static_cast<const nonstd::string_view&>(args)...});
00111 }
00112
00113
00114 }
00115
00116
00117 #endif // STRCAT_HPP