23 #ifndef GUL14_JOIN_SPLIT_H_
24 #define GUL14_JOIN_SPLIT_H_
29 #include <type_traits>
121 template <
typename StringContainer = std::vector<std::
string>,
122 typename ContainerInsertFct =
void (*)(StringContainer&,
string_view)>
123 inline StringContainer
125 ContainerInsertFct insert_fct = detail::emplace_back<StringContainer>)
127 using StringType =
typename StringContainer::value_type;
128 using SizeType =
typename StringType::size_type;
130 auto result = StringContainer{ };
131 auto search_start = SizeType{ 0 };
132 auto push_start = search_start;
135 const auto hit = text.find(delimiter.data(), search_start, delimiter.size());
136 if (hit == StringType::npos)
138 const auto hit_len = hit - push_start;
139 insert_fct(result, text.substr(push_start, hit_len));
140 search_start += std::max(delimiter.size() + hit_len, SizeType{ 1 });
141 push_start += delimiter.size() + hit_len;
143 insert_fct(result, text.substr(push_start));
171 template <
typename StringContainer = std::vector<std::
string>,
172 typename ContainerInsertFct =
void (*)(StringContainer&,
string_view)>
173 inline StringContainer
175 ContainerInsertFct insert_fct = detail::emplace_back<StringContainer>)
177 auto const end = std::cregex_iterator{ };
178 auto result = StringContainer{ };
180 auto parts = std::cregex_iterator(text.begin(), text.end(), delimiter);
183 insert_fct(result, text);
187 auto previous = std::cregex_iterator{ };
188 for (; parts != end; ++parts) {
189 if (parts == previous and not parts->length())
191 auto const& match = parts->prefix();
192 insert_fct(result,
string_view(match.first, match.length()));
196 auto const& match = previous->suffix();
197 insert_fct(result,
string_view(match.first, match.length()));
225 template <
typename StringContainer = std::vector<
string_view>,
226 typename ContainerInsertFct =
void (*)(StringContainer&,
string_view)>
227 inline StringContainer
229 ContainerInsertFct insert_fct = detail::emplace_back<StringContainer>)
231 return split<StringContainer>(text, delimiter, insert_fct);
257 template <
typename Iterator>
266 std::size_t num_strings = 0;
269 for (
auto it = begin; it != end; ++it)
274 len += (num_strings - 1) * glue.size();
281 for (
auto it = std::next(begin); it != end; ++it)
283 result.append(glue.data(), glue.size());
319 template <
typename StringContainer>
323 return join(parts.begin(), parts.end(), glue);
std::string join(Iterator begin, Iterator end, string_view glue)
Concatenate all strings in a range, placing a delimiter between them.
Definition: join_split.h:259
StringContainer split(string_view text, string_view delimiter, ContainerInsertFct insert_fct=detail::emplace_back< StringContainer >)
Separate a string at all occurrences of a delimiter, returning the strings between the delimiters in ...
Definition: join_split.h:124
StringContainer split_sv(string_view text, string_view delimiter, ContainerInsertFct insert_fct=detail::emplace_back< StringContainer >)
Separate a string at all occurrences of a delimiter, returning the strings between the delimiters in ...
Definition: join_split.h:228
basic_string_view< char > string_view
A view to a contiguous sequence of chars.
Definition: string_view.h:624
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
Declaration of string utility functions.
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.