General Utility Library for C++17 26.5.0
tokenize.h
Go to the documentation of this file.
1
26#ifndef GUL17_TOKENIZE_H_
27#define GUL17_TOKENIZE_H_
28
29#include <string_view>
30#include <string>
31#include <vector>
32
33#include "gul17/internal.h"
34#include "gul17/string_util.h"
35
36namespace gul17 {
37
117template <typename StringContainer = std::vector<std::string>,
118 typename ContainerInsertFct = void (*)(StringContainer&, std::string_view)>
119inline StringContainer
120tokenize(std::string_view str, std::string_view delimiters = default_whitespace_characters,
121 ContainerInsertFct insert_fct = detail::emplace_back<StringContainer>)
122{
124
125 std::string_view::size_type token_start = 0;
126 std::string_view::size_type token_end = 0;
127
128 while (true)
129 {
130 token_start = str.find_first_not_of(delimiters, token_end);
131 if (token_start == std::string_view::npos)
132 break;
133
134 token_end = str.find_first_of(delimiters, token_start);
135 if (token_end == std::string_view::npos)
136 {
138 std::string_view{ str.data() + token_start, str.length() - token_start });
139 break;
140 }
141 else
142 {
144 std::string_view{ str.data() + token_start, token_end - token_start });
145 }
146 }
147
148 return tokens;
149}
150
171template <typename StringContainer = std::vector<std::string_view>,
172 typename ContainerInsertFct = void (*)(StringContainer&, std::string_view)>
173inline StringContainer
174tokenize_sv(std::string_view str, std::string_view delimiters = default_whitespace_characters,
175 ContainerInsertFct insert_fct = detail::emplace_back<StringContainer>)
176{
178}
179
181
182} // namespace gul17
183
184#endif
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
GUL_EXPORT const std::string_view default_whitespace_characters
The default characters that are treated as whitespace by GUL.
Definition string_util.cc:32
StringContainer tokenize_sv(std::string_view str, std::string_view delimiters=default_whitespace_characters, ContainerInsertFct insert_fct=detail::emplace_back< StringContainer >)
Split the given string into a vector of substrings (tokens) delimited by any of the characters in the...
Definition tokenize.h:174
StringContainer tokenize(std::string_view str, std::string_view delimiters=default_whitespace_characters, ContainerInsertFct insert_fct=detail::emplace_back< StringContainer >)
Split the given string into a vector of substrings (tokens) delimited by any of the characters in the...
Definition tokenize.h:120
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29
Declaration of string utility functions.