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
27namespace Poco {
28
29
30class 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{
35public:
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
85private:
86 StringTokenizer(const StringTokenizer&);
87 StringTokenizer& operator = (const StringTokenizer&);
88
89 void trim(std::string& token);
90
91 TokenVec _tokens;
92};
93
94
95//
96// inlines
97//
98
99
100inline StringTokenizer::Iterator StringTokenizer::begin() const
101{
102 return _tokens.begin();
103}
104
105
106inline StringTokenizer::Iterator StringTokenizer::end() const
107{
108 return _tokens.end();
109}
110
111
112inline std::string& StringTokenizer::operator [] (std::size_t index)
113{
114 if (index >= _tokens.size()) throw RangeException();
115 return _tokens[index];
116}
117
118
119inline const std::string& StringTokenizer::operator [] (std::size_t index) const
120{
121 if (index >= _tokens.size()) throw RangeException();
122 return _tokens[index];
123}
124
125
126inline std::size_t StringTokenizer::count() const
127{
128 return _tokens.size();
129}
130
131
132} // namespace Poco
133
134
135#endif // Foundation_StringTokenizer_INCLUDED
136