| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_PARSE_SPLIT_HPP |
| 4 |
#define MPT_PARSE_SPLIT_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/namespace.hpp" |
| 9 |
#include "mpt/parse/parse.hpp" |
| 10 |
#include "mpt/string/utility.hpp" |
| 11 |
|
| 12 |
#include <vector> |
| 13 |
|
| 14 |
#include <cstddef> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
namespace mpt { |
| 19 |
inline namespace MPT_INLINE_NS { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
template <typename T, typename Tstring> |
| 24 |
std::vector<T> split_parse(const Tstring & str, const Tstring & sep = Tstring(1, char_constants<typename Tstring::value_type>::comma)) { |
| 25 |
std::vector<T> vals; |
| 26 |
std::size_t pos = 0; |
| 27 |
while (str.find(sep, pos) != std::string::npos) { |
| 28 |
vals.push_back(mpt::parse<T>(str.substr(pos, str.find(sep, pos) - pos))); |
| 29 |
pos = str.find(sep, pos) + sep.length(); |
| 30 |
} |
| 31 |
if (!vals.empty() || (str.substr(pos).length() > 0)) { |
| 32 |
vals.push_back(mpt::parse<T>(str.substr(pos))); |
| 33 |
} |
| 34 |
return vals; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
} // namespace MPT_INLINE_NS |
| 40 |
} // namespace mpt |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#endif // MPT_PARSE_SPLIT_HPP |