KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
tokenizer.hpp
Go to the documentation of this file.
1 //
2 // tokenizer.hpp
3 // kssutil
4 //
5 // Created by Steven W. Klassen on 2012-12-21.
6 // Copyright (c) 2012 Klassen Software Solutions. All rights reserved.
7 // Licensing follows the MIT License.
8 //
9 
15 #ifndef kssutil_tokenizer_hpp
16 #define kssutil_tokenizer_hpp
17 
18 #include <string>
19 #include "iterator.hpp"
20 
21 namespace kss { namespace util { namespace strings {
22 
31  class Tokenizer {
32  public:
36  using value_type = std::string;
38 
50  explicit Tokenizer(const std::string& s,
51  const std::string& delims = " \t\n\r",
52  std::string::size_type start = 0,
53  std::string::size_type end = std::string::npos);
54  explicit Tokenizer(std::string&& s,
55  const std::string& delims = " \t\n\r",
56  std::string::size_type start = 0,
57  std::string::size_type end = std::string::npos);
58  ~Tokenizer() noexcept = default;
59 
60  // Methods needed for the ForwardIterator access. Note that the iterators are
61  // written in terms of hasAnother() and next(), so you should use one or the
62  // other approaches, but not both at the same time.
63 
67  inline bool hasAnother() const noexcept {
68  return (_lastPos <= _end);
69  }
70 
76  std::string& next(std::string& token);
77 
83  iterator begin() { return iterator(*this); }
84  iterator end() { return iterator(); }
85 
86  private:
87  std::string _s;
88  std::string _delim;
89  std::string::size_type _lastPos;
90  std::string::size_type _end;
91  };
92 
93 }}}
94 
95 #endif
std::string & next(std::string &token)
Base implementation of a forward iterator.
Definition: iterator.hpp:61
bool hasAnother() const noexcept
Definition: tokenizer.hpp:67
Tokenizer(const std::string &s, const std::string &delims=" \t\n\r", std::string::size_type start=0, std::string::size_type end=std::string::npos)
~Tokenizer() noexcept=default
Container like access to a string separated by tokens.
Definition: tokenizer.hpp:31
kss::util::iterators::ForwardIterator< Tokenizer, std::string > iterator
Definition: tokenizer.hpp:37
Base classes to easily generate custom iterators.