-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic_strcat and basic_string_builder
- Loading branch information
Showing
3 changed files
with
71 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef jm4R_STRCAT | ||
#define jm4R_STRCAT | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace mj { | ||
|
||
template <typename CharT, typename... Args> | ||
constexpr std::basic_string<CharT> basic_strcat(const Args&... vals) | ||
{ | ||
auto len = std::size_t{ 0 }; | ||
(len += ... += std::basic_string_view<CharT>{ vals }.length()); | ||
auto res = std::basic_string<CharT>{}; | ||
res.reserve(len); | ||
(res += ... += vals); | ||
return res; | ||
} | ||
|
||
template <typename... Args> | ||
constexpr std::string strcat(const Args&... vals) | ||
{ | ||
return basic_strcat<std::string::value_type>(vals...); | ||
} | ||
|
||
template <typename... Args> | ||
constexpr std::wstring wcscat(const Args&... vals) | ||
{ | ||
return basic_strcat<std::wstring::value_type>(vals...); | ||
} | ||
} | ||
|
||
#endif //jm4R_STRCAT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,50 @@ | ||
#include <iostream> | ||
#include <string_view> | ||
#include <tuple> | ||
#ifndef jm4R_STRING_BUILDER | ||
#define jm4R_STRING_BUILDER | ||
|
||
#include "strcat.hpp" | ||
|
||
#include <string> | ||
#include <tuple> | ||
|
||
template<typename... Args> | ||
struct string_builder | ||
{ | ||
std::tuple<Args&...> vals; | ||
namespace mj { | ||
|
||
template <typename CharT, typename... Args> | ||
struct basic_string_builder { | ||
std::tuple<const Args&...> vals; | ||
|
||
template <typename T> | ||
constexpr auto operator<<(T& v) && -> string_builder<Args..., T> | ||
constexpr auto operator<<(const T& v) && -> basic_string_builder<CharT, Args..., T> | ||
{ | ||
return {std::tuple_cat(vals, std::tuple<T&>{v})}; | ||
return { std::tuple_cat(vals, std::tuple<const T&>{ v }) }; | ||
} | ||
|
||
constexpr operator std::string() && | ||
constexpr operator std::basic_string<CharT>() && | ||
{ | ||
auto len = std::size_t{0}; | ||
std::apply([&](auto&... x){ (len += ... += std::string_view{x}.length()); } , vals); | ||
return std::apply([&](auto&... vals) { return mj::basic_strcat<CharT>(vals...); }, vals); | ||
} | ||
}; | ||
|
||
auto res = std::string{}; | ||
res.reserve(len); | ||
template <typename... Args> | ||
using string_builder = basic_string_builder<char, Args...>; | ||
|
||
std::apply([&](auto&... x){ (res += ... += x); } , vals); | ||
template <typename... Args> | ||
using wstring_builder = basic_string_builder<wchar_t, Args...>; | ||
|
||
return res; | ||
} | ||
}; | ||
template <typename CharT> | ||
constexpr auto basic_build_string() | ||
{ | ||
return basic_string_builder<CharT>{}; | ||
} | ||
|
||
constexpr auto build_string() | ||
{ | ||
return string_builder<>{}; | ||
} | ||
|
||
constexpr auto build_wstring() | ||
{ | ||
return wstring_builder<>{}; | ||
} | ||
} | ||
|
||
#endif //jm4R_STRING_BUILDER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters