Skip to content

Commit

Permalink
[Minor] Add utility to split strings on some character
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Jun 24, 2023
1 parent 8c1c1dd commit 43adbcf
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/libutil/cxx/util.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ inline auto string_foreach_line(const S &input, const F &functor)
}
}

template<class S, typename std::enable_if_t<std::is_constructible_v<std::string_view, S>, bool> = true>
inline auto string_split_on(const S &input, std::string_view::value_type chr) -> std::pair<std::string_view, std::string_view>
{
auto pos = std::find(std::begin(input), std::end(input), chr);

if (pos != input.end()) {
auto first = std::string_view{std::begin(input), pos};
while (*pos == chr && pos != input.end()) {
++pos;
}
auto last = std::string_view{pos, std::end(input)};

return {first, last};
}

return {std::string_view{input}, std::string_view{}};
}

/**
* Enumerate for range loop
*/
Expand Down

0 comments on commit 43adbcf

Please sign in to comment.