diff options
| author | William A. Kennington III <wak@google.com> | 2019-06-27 12:48:59 -0700 |
|---|---|---|
| committer | William A. Kennington III <wak@google.com> | 2019-07-08 16:26:42 -0700 |
| commit | 4ef36e7904aa354256ab5818c543df0a9c61f90f (patch) | |
| tree | 80880e7259ada0b0757a8d2e437bc8a123fff857 /src | |
| parent | 079cba7212a9a2e5eb7260492696675d67979346 (diff) | |
| download | stdplus-4ef36e7904aa354256ab5818c543df0a9c61f90f.tar.gz stdplus-4ef36e7904aa354256ab5818c543df0a9c61f90f.zip | |
util/str: Add string concatentation methods
Change-Id: I5caf8e0731eb9ac0f18b84d25256ea0068fab03c
Signed-off-by: William A. Kennington III <wak@google.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/meson.build | 1 | ||||
| -rw-r--r-- | src/stdplus/util/string.hpp | 57 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/meson.build b/src/meson.build index a6e82b9..aadb68d 100644 --- a/src/meson.build +++ b/src/meson.build @@ -31,4 +31,5 @@ install_headers( install_headers( 'stdplus/util/cexec.hpp', + 'stdplus/util/string.hpp', subdir: 'stdplus/util') diff --git a/src/stdplus/util/string.hpp b/src/stdplus/util/string.hpp new file mode 100644 index 0000000..4e37e3b --- /dev/null +++ b/src/stdplus/util/string.hpp @@ -0,0 +1,57 @@ +#pragma once +#include <cstring> +#include <string> +#include <string_view> +#include <utility> + +namespace stdplus +{ +namespace util +{ +namespace detail +{ + +template <typename... Views> +void strAppendViews(std::string& dst, Views... views) +{ + dst.reserve((dst.size() + ... + views.size())); + (dst.append(views), ...); +} + +} // namespace detail + +/** @brief Appends multiple strings to the end of the destination string + * in the most optimal way for the given inputs. + * + * @param[in, out] dst - The string being appended to + * @param[in] ...strs - An arbitrary number of strings to concatenate + */ +template <typename... Strs> +void strAppend(std::string& dst, const Strs&... strs) +{ + detail::strAppendViews(dst, std::string_view(strs)...); +} + +/** @brief Concatenates multiple strings together in the most optimal + * way for the given inputs. + * + * @param[in] ...strs - An arbitrary number of strings to concatenate + * @return The concatenated result string + */ +template <typename... Strs> +std::string strCat(const Strs&... strs) +{ + std::string ret; + strAppend(ret, strs...); + return ret; +} +template <typename... Strs> +std::string strCat(std::string&& in, const Strs&... strs) +{ + std::string ret = std::move(in); + strAppend(ret, strs...); + return ret; +} + +} // namespace util +} // namespace stdplus |

