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