diff --git a/include/fast_strcat/string_builder.hpp b/include/fast_strcat/string_builder.hpp index e69de29..c686a9e 100644 --- a/include/fast_strcat/string_builder.hpp +++ b/include/fast_strcat/string_builder.hpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +template +struct string_builder +{ + std::tuple vals; + + template + constexpr auto operator<<(T& v) && -> string_builder + { + return {std::tuple_cat(vals, std::tuple{v})}; + } + + constexpr operator std::string() && + { + auto len = std::size_t{0}; + std::apply([&](auto&... x){ (len += ... += std::string_view{x}.length()); } , vals); + + auto res = std::string{}; + res.reserve(len); + + std::apply([&](auto&... x){ (res += ... += x); } , vals); + + return res; + } +}; + +constexpr auto build_string() +{ + return string_builder<>{}; +} diff --git a/test/main.cpp b/test/main.cpp index ffd73c3..def8981 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,8 +1,13 @@ #include #include +#include int main() { + std::string test2 = "test2"; + const char* test4 = "test4"; + std::string test = build_string() << "test1 " << test2 << " test3 " << test4; + assert(test == "test1 test2 test3 test4"); return 0; }