diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-08 18:31:58 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:57:01 +0200 |
commit | 6d66d5675d0fb78827bc47c49f9d4a1852c7255d (patch) | |
tree | e8cbf56d895c6d4acc496fffb076938e822dba40 /src/util | |
parent | 7e84d664079d9c407bdf94861825bb05ccf1b0f7 (diff) |
feat: implement command mode
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/string.hpp | 16 | ||||
-rw-r--r-- | src/util/string_impl.hpp | 29 |
2 files changed, 45 insertions, 0 deletions
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 <concepts> +#include <string> + +template < + typename ContainerType, + typename Char = char, + typename String = std::basic_string<Char>> +requires Container<ContainerType> && HasPushBack<ContainerType> && + std::same_as<typename ContainerType::value_type, String> +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 <typename ContainerType, typename Char, typename String> +requires Container<ContainerType> && HasPushBack<ContainerType> && + std::same_as<typename ContainerType::value_type, String> +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; +} |