Utils: Rewrite fmt::split

This commit is contained in:
Eladash 2021-01-05 17:34:35 +02:00 committed by Ivan
parent 36159c2bd4
commit bf6f43ec3a
4 changed files with 27 additions and 18 deletions

View File

@ -418,32 +418,41 @@ std::string fmt::replace_all(const std::string& src, const std::string& from, co
return target; return target;
} }
std::vector<std::string> fmt::split(const std::string& source, std::initializer_list<std::string> separators, bool is_skip_empty) std::vector<std::string> fmt::split(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty)
{ {
std::vector<std::string> result; std::vector<std::string> result;
usz cursor_begin = 0; for (usz index = 0; index < source.size();)
for (usz cursor_end = 0; cursor_end < source.length(); ++cursor_end)
{ {
usz pos = -1;
usz sep_size = 0;
for (auto& separator : separators) for (auto& separator : separators)
{ {
if (strncmp(source.c_str() + cursor_end, separator.c_str(), separator.length()) == 0) if (usz pos0 = source.find(separator, index); pos0 != umax)
{ {
std::string candidate = source.substr(cursor_begin, cursor_end - cursor_begin); pos = pos0;
if (!is_skip_empty || !candidate.empty()) sep_size = separator.size();
result.push_back(candidate);
cursor_begin = cursor_end + separator.length();
cursor_end = cursor_begin - 1;
break; break;
} }
} }
}
if (cursor_begin != source.length()) if (!sep_size)
{ {
result.push_back(source.substr(cursor_begin)); result.emplace_back(&source[index], source.size() - index);
return result;
}
std::string_view piece = {&source[index], pos - index};
index = pos + sep_size;
if (piece.empty() && is_skip_empty)
{
continue;
}
result.emplace_back(std::string(piece));
} }
return result; return result;

View File

@ -75,7 +75,7 @@ namespace fmt
return src; return src;
} }
std::vector<std::string> split(const std::string& source, std::initializer_list<std::string> separators, bool is_skip_empty = true); std::vector<std::string> split(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty = true);
std::string trim(const std::string& source, const std::string& values = " \t"); std::string trim(const std::string& source, const std::string& values = " \t");
template <typename T> template <typename T>

View File

@ -45,7 +45,7 @@ avconf_manager::avconf_manager()
{ {
u32 curindex = 0; u32 curindex = 0;
auto mic_list = fmt::split(g_cfg.audio.microphone_devices, {"@@@"}); auto mic_list = fmt::split(g_cfg.audio.microphone_devices.to_string(), {"@@@"});
if (!mic_list.empty()) if (!mic_list.empty())
{ {
switch (g_cfg.audio.microphone_type) switch (g_cfg.audio.microphone_type)

View File

@ -125,7 +125,7 @@ void mic_context::operator()()
void mic_context::load_config_and_init() void mic_context::load_config_and_init()
{ {
auto device_list = fmt::split(g_cfg.audio.microphone_devices, {"@@@"}); auto device_list = fmt::split(g_cfg.audio.microphone_devices.to_string(), {"@@@"});
if (!device_list.empty() && mic_list.empty()) if (!device_list.empty() && mic_list.empty())
{ {