| 1 | // | 
|---|
| 2 | // StringTokenizer.h | 
|---|
| 3 | // | 
|---|
| 4 | // Library: Foundation | 
|---|
| 5 | // Package: Core | 
|---|
| 6 | // Module:  StringTokenizer | 
|---|
| 7 | // | 
|---|
| 8 | // Definition of the StringTokenizer class. | 
|---|
| 9 | // | 
|---|
| 10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. | 
|---|
| 11 | // and Contributors. | 
|---|
| 12 | // | 
|---|
| 13 | // SPDX-License-Identifier:	BSL-1.0 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | #ifndef Foundation_StringTokenizer_INCLUDED | 
|---|
| 18 | #define Foundation_StringTokenizer_INCLUDED | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "Poco/Foundation.h" | 
|---|
| 22 | #include "Poco/Exception.h" | 
|---|
| 23 | #include <vector> | 
|---|
| 24 | #include <cstddef> | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | namespace Poco { | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | class Foundation_API StringTokenizer | 
|---|
| 31 | /// A simple tokenizer that splits a string into | 
|---|
| 32 | /// tokens, which are separated by separator characters. | 
|---|
| 33 | /// An iterator is used to iterate over all tokens. | 
|---|
| 34 | { | 
|---|
| 35 | public: | 
|---|
| 36 | enum Options | 
|---|
| 37 | { | 
|---|
| 38 | TOK_IGNORE_EMPTY = 1, /// ignore empty tokens | 
|---|
| 39 | TOK_TRIM         = 2  /// remove leading and trailing whitespace from tokens | 
|---|
| 40 | }; | 
|---|
| 41 |  | 
|---|
| 42 | typedef std::vector<std::string> TokenVec; | 
|---|
| 43 | typedef TokenVec::const_iterator Iterator; | 
|---|
| 44 |  | 
|---|
| 45 | StringTokenizer(const std::string& str, const std::string& separators, int options = 0); | 
|---|
| 46 | /// Splits the given string into tokens. The tokens are expected to be | 
|---|
| 47 | /// separated by one of the separator characters given in separators. | 
|---|
| 48 | /// Additionally, options can be specified: | 
|---|
| 49 | ///   * TOK_IGNORE_EMPTY: empty tokens are ignored | 
|---|
| 50 | ///   * TOK_TRIM: trailing and leading whitespace is removed from tokens. | 
|---|
| 51 |  | 
|---|
| 52 | ~StringTokenizer(); | 
|---|
| 53 | /// Destroys the tokenizer. | 
|---|
| 54 |  | 
|---|
| 55 | Iterator begin() const; | 
|---|
| 56 | Iterator end() const; | 
|---|
| 57 |  | 
|---|
| 58 | const std::string& operator [] (std::size_t index) const; | 
|---|
| 59 | /// Returns const reference the index'th token. | 
|---|
| 60 | /// Throws a RangeException if the index is out of range. | 
|---|
| 61 |  | 
|---|
| 62 | std::string& operator [] (std::size_t index); | 
|---|
| 63 | /// Returns reference to the index'th token. | 
|---|
| 64 | /// Throws a RangeException if the index is out of range. | 
|---|
| 65 |  | 
|---|
| 66 | bool has(const std::string& token) const; | 
|---|
| 67 | /// Returns true if token exists, false otherwise. | 
|---|
| 68 |  | 
|---|
| 69 | std::string::size_type find(const std::string& token, std::string::size_type pos = 0) const; | 
|---|
| 70 | /// Returns the index of the first occurrence of the token | 
|---|
| 71 | /// starting at position pos. | 
|---|
| 72 | /// Throws a NotFoundException if the token is not found. | 
|---|
| 73 |  | 
|---|
| 74 | std::size_t replace(const std::string& oldToken, const std::string& newToken, std::string::size_type pos = 0); | 
|---|
| 75 | /// Starting at position pos, replaces all subsequent tokens having value | 
|---|
| 76 | /// equal to oldToken with newToken. | 
|---|
| 77 | /// Returns the number of modified tokens. | 
|---|
| 78 |  | 
|---|
| 79 | std::size_t count() const; | 
|---|
| 80 | /// Returns the total number of tokens. | 
|---|
| 81 |  | 
|---|
| 82 | std::size_t count(const std::string& token) const; | 
|---|
| 83 | /// Returns the number of tokens equal to the specified token. | 
|---|
| 84 |  | 
|---|
| 85 | private: | 
|---|
| 86 | void (std::string& token); | 
|---|
| 87 |  | 
|---|
| 88 | TokenVec _tokens; | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | // | 
|---|
| 93 | // inlines | 
|---|
| 94 | // | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | inline StringTokenizer::Iterator StringTokenizer::begin() const | 
|---|
| 98 | { | 
|---|
| 99 | return _tokens.begin(); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | inline StringTokenizer::Iterator StringTokenizer::end() const | 
|---|
| 104 | { | 
|---|
| 105 | return _tokens.end(); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 | inline std::string& StringTokenizer::operator [] (std::size_t index) | 
|---|
| 110 | { | 
|---|
| 111 | if (index >= _tokens.size()) throw RangeException(); | 
|---|
| 112 | return _tokens[index]; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | inline const std::string& StringTokenizer::operator [] (std::size_t index) const | 
|---|
| 117 | { | 
|---|
| 118 | if (index >= _tokens.size()) throw RangeException(); | 
|---|
| 119 | return _tokens[index]; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | inline std::size_t StringTokenizer::count() const | 
|---|
| 124 | { | 
|---|
| 125 | return _tokens.size(); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 | } // namespace Poco | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | #endif // Foundation_StringTokenizer_INCLUDED | 
|---|
| 133 |  | 
|---|