General Utility Library for C++14  2.11
tokenize.h
Go to the documentation of this file.
1 
23 #ifndef GUL14_TOKENIZE_H_
24 #define GUL14_TOKENIZE_H_
25 
26 #include <string>
27 #include <vector>
28 
29 #include "gul14/internal.h"
30 #include "gul14/string_util.h"
31 #include "gul14/string_view.h"
32 
33 namespace gul14 {
34 
114 template <typename StringContainer = std::vector<std::string>,
115  typename ContainerInsertFct = void (*)(StringContainer&, string_view)>
116 inline StringContainer
118  ContainerInsertFct insert_fct = detail::emplace_back<StringContainer>)
119 {
120  StringContainer tokens{ };
121 
122  string_view::size_type token_start = 0;
123  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 == string_view::npos)
129  break;
130 
131  token_end = str.find_first_of(delimiters, token_start);
132  if (token_end == string_view::npos)
133  {
134  insert_fct(tokens,
135  string_view{ str.data() + token_start, str.length() - token_start });
136  break;
137  }
138  else
139  {
140  insert_fct(tokens,
141  string_view{ str.data() + token_start, token_end - token_start });
142  }
143  }
144 
145  return tokens;
146 }
147 
168 template <typename StringContainer = std::vector<string_view>,
169  typename ContainerInsertFct = void (*)(StringContainer&, string_view)>
170 inline StringContainer
172  ContainerInsertFct insert_fct = detail::emplace_back<StringContainer>)
173 {
174  return tokenize<StringContainer>(str, delimiters, insert_fct);
175 }
176 
178 
179 } // namespace gul14
180 
181 #endif
GUL_EXPORT const string_view default_whitespace_characters
The default characters that are treated as whitespace by GUL.
Definition: string_util.cc:29
StringContainer tokenize_sv(string_view str, 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(string_view str, 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 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.