aboutsummaryrefslogtreecommitdiff
path: root/src/util/string_impl.hpp
blob: 8c859475d3ddc4c566bd76ee41ac5d0a21ed437b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
}