From 6d66d5675d0fb78827bc47c49f9d4a1852c7255d Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 8 Jun 2022 18:31:58 +0200 Subject: feat: implement command mode --- src/util/string.hpp | 16 ++++++++++++++++ src/util/string_impl.hpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/util/string.hpp create mode 100644 src/util/string_impl.hpp (limited to 'src/util') diff --git a/src/util/string.hpp b/src/util/string.hpp new file mode 100644 index 0000000..a571c50 --- /dev/null +++ b/src/util/string.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "util/concepts.hpp" + +#include +#include + +template < + typename ContainerType, + typename Char = char, + typename String = std::basic_string> +requires Container && HasPushBack && + std::same_as +auto split_string(const String &str, Char delimiter) noexcept -> ContainerType; + +#include "string_impl.hpp" diff --git a/src/util/string_impl.hpp b/src/util/string_impl.hpp new file mode 100644 index 0000000..8c85947 --- /dev/null +++ b/src/util/string_impl.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "string.hpp" + +template +requires Container && HasPushBack && + std::same_as +auto split_string(const String &str, Char delimiter) noexcept -> ContainerType +{ + auto result_container = ContainerType(); + + auto str_copy = str; + + size_t pos = 0; + String token; + + while ((pos = str_copy.find(delimiter)) != String::npos) + { + token = str_copy.substr(0, pos); + + result_container.push_back(token); + + str_copy.erase(0, pos + 1U); + } + + result_container.push_back(str_copy); + + return result_container; +} -- cgit v1.2.3-18-g5258