![]() |
General Utility Library for C++17 25.4.1
|
Joining and splitting strings.
Functions | |
template<typename StringContainer = std::vector<std::string>, typename ContainerInsertFct = void (*)(StringContainer&, std::string_view)> | |
StringContainer | gul17::split (std::string_view text, std::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 a container. | |
template<typename StringContainer = std::vector<std::string>, typename ContainerInsertFct = void (*)(StringContainer&, std::string_view)> | |
StringContainer | gul17::split (std::string_view text, const std::regex &delimiter, ContainerInsertFct insert_fct=detail::emplace_back< StringContainer >) |
Separate a string at all occurrences of a delimiter described by a regular expression, returning the strings between the delimiters in a container. | |
template<typename StringContainer = std::vector<std::string_view>, typename ContainerInsertFct = void (*)(StringContainer&, std::string_view)> | |
StringContainer | gul17::split_sv (std::string_view text, std::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 a vector. | |
template<typename Iterator > | |
std::string | gul17::join (Iterator begin, Iterator end, std::string_view glue) |
Concatenate all strings (or string-like elements) in a range, placing a delimiter between them. | |
template<typename StringContainer > | |
std::string | gul17::join (const StringContainer &parts, std::string_view glue) |
Concatenate all strings (or string-like elements) in a range, placing a delimiter between them. | |
template<typename Iterator , typename ConversionFct > | |
std::string | gul17::join (Iterator begin, Iterator end, std::string_view glue, ConversionFct to_string, std::size_t prealloc=0) |
Concatenate the strings resulting from calling the given function on each element in a range, placing a delimiter between them. | |
template<typename Container , typename ConversionFct > | |
std::string | gul17::join (const Container &container, std::string_view glue, ConversionFct to_string, std::size_t prealloc=0) |
Concatenate the strings resulting from calling the given function on each element in a range, placing a delimiter between them. | |
|
inline |
Concatenate the strings resulting from calling the given function on each element in a range, placing a delimiter between them.
This algorithm iterates exactly once over the range. To speed up complex joining operations, string memory can be preallocated via the last parameter.
container | Container of input elements |
glue | String to be put between the string-converted elements |
to_string | Function object to convert the container elements to strings |
prealloc | Number of bytes to preallocate for the result string. Providing this parameter is not necessary, but it may provide a tiny performance boost. The default is zero (no preallocation). |
Container | A container type that provides a cbegin() and cend() member function returning forward iterators. |
ConversionFct | A function object type that can be called with the dereferenced iterator type and returns a string type. This string type must support concatenation with std::string::operator+=(). |
References gul17::bit_set(), and gul17::join().
|
inline |
Concatenate all strings (or string-like elements) in a range, placing a delimiter between them.
If the element type in the range has a size() member function, the algorithm iterates twice over the range in order to pre-allocate a string of the correct size. Otherwise, it only iterates once.
This is the inverse function of split(). It is guaranteed that join(split(text, del), del) == text
(unless del is a std::regex object).
parts | A container holding strings or string views that are to be concatenated |
glue | String that is put between each element of parts |
StringContainer | A container type that holds string-like elements, e.g. std::vector<std::string> or std::list<std::string_view>. The container must provide an STL-like forward iterator interface. The string type must must support concatenation using std::string::operator+=. An additional optimization is enabled if the string type has a size() member function. |
References gul17::bit_set(), and gul17::join().
|
inline |
Concatenate all strings (or string-like elements) in a range, placing a delimiter between them.
If the element type in the range has a size() member function, the algorithm iterates twice over the range in order to pre-allocate a string of the correct size. Otherwise, it only iterates once.
begin | Iterator pointing to the first string |
end | Iterator pointing past the last string |
glue | String that is put between each element of parts |
Iterator | A forward iterator type that dereferences to a string-like type. This string type must must support concatenation using std::string::operator+=. An additional optimization is enabled if the string type has a size() member function. |
References gul17::bit_set().
Referenced by gul17::join(), and gul17::join().
|
inline |
Concatenate the strings resulting from calling the given function on each element in a range, placing a delimiter between them.
This algorithm iterates exactly once over the range. To speed up complex joining operations, string memory can be preallocated via the last parameter.
begin | Iterator to the first input element |
end | Iterator pointing past the last input element |
glue | String to be put between the string-converted elements |
to_string | Function object to convert the container elements to strings |
prealloc | Number of bytes to preallocate for the result string. Providing this parameter is not necessary, but it may provide a tiny performance boost. The default is zero (no preallocation). |
Iterator | A forward iterator type that dereferences to a type that can be passed to the ConversionFct. |
ConversionFct | A function object type that can be called with the dereferenced iterator type and returns a string type. This string type must support concatenation with std::string::operator+=(). |
References gul17::bit_set().
|
inline |
Separate a string at all occurrences of a delimiter described by a regular expression, returning the strings between the delimiters in a container.
This function is a variant of split(std::string_view, std::string_view, ContainerInsertFct) that accepts a std::regex object to describe the delimiter:
text | The string to be split |
delimiter | A std::regex object describing the delimiters |
insert_fct | Custom container inserter function |
References gul17::bit_set().
|
inline |
Separate a string at all occurrences of a delimiter, returning the strings between the delimiters in a container.
The result has at least one element. If the delimiter is not present in the text, the whole text is returned. If there are consecutive delimiters, the collected string between them is the empty string. If the delimiter is directly at the end of the input, the collected string between the end of the input and the delimiter is again the empty string.
split() is the inverse function of join(). It is guaranteed that join(split(text, del), del) == text
.
This function returns a std::vector<std::string>
by default, but a compatible container for string/string_view types can be specified via a template parameter:
StringContainer | A container for strings or std::string_view-like types, e.g. std::vector<std::string> or std::list<std::string_view> |
ContainerInsertFct | Type for the insert_fct function parameter. |
text | The string to be split |
delimiter | The delimiting substring |
insert_fct | By default, split() calls the emplace_back() member function on the container to insert strings. This parameter may contain a different function pointer or object with the signature void f(StringContainer&, std::string_view) that is called instead. This can be useful for containers that do not provide emplace_back() or for other customizations. |
References gul17::bit_set().
|
inline |
Separate a string at all occurrences of a delimiter, returning the strings between the delimiters in a vector.
This function is identical to split(std::string_view, std::string_view, ContainerInsertFct) except that it returns a std::vector of std::string_views instead of strings by default:
References gul17::bit_set().