/src/poco/Foundation/src/StringTokenizer.cpp
Line | Count | Source |
1 | | // |
2 | | // StringTokenizer.cpp |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Core |
6 | | // Module: StringTokenizer |
7 | | // |
8 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | | // and Contributors. |
10 | | // |
11 | | // SPDX-License-Identifier: BSL-1.0 |
12 | | // |
13 | | |
14 | | |
15 | | #include "Poco/StringTokenizer.h" |
16 | | #include "Poco/Ascii.h" |
17 | | #include <algorithm> |
18 | | |
19 | | |
20 | | namespace Poco { |
21 | | |
22 | | |
23 | | StringTokenizer::StringTokenizer(const std::string& str, const std::string& separators, int options) |
24 | 1.34M | { |
25 | 1.34M | std::string::const_iterator it = str.begin(); |
26 | 1.34M | std::string::const_iterator end = str.end(); |
27 | 1.34M | std::string token; |
28 | 1.34M | bool doTrim = ((options & TOK_TRIM) != 0); |
29 | 1.34M | bool ignoreEmpty = ((options & TOK_IGNORE_EMPTY) != 0); |
30 | 1.34M | bool lastToken = false; |
31 | | |
32 | 1.14G | for (; it != end; ++it) |
33 | 1.14G | { |
34 | 1.14G | if (separators.find(*it) != std::string::npos) |
35 | 61.9M | { |
36 | 61.9M | if (doTrim) trim(token); |
37 | 61.9M | if (!token.empty() || !ignoreEmpty) _tokens.push_back(token); |
38 | 61.9M | if (!ignoreEmpty) lastToken = true; |
39 | 61.9M | token.clear(); |
40 | 61.9M | } |
41 | 1.07G | else |
42 | 1.07G | { |
43 | 1.07G | token += *it; |
44 | 1.07G | lastToken = false; |
45 | 1.07G | } |
46 | 1.14G | } |
47 | | |
48 | 1.34M | if (!token.empty()) |
49 | 56.6k | { |
50 | 56.6k | if (doTrim) trim(token); |
51 | 56.6k | if (!token.empty() || !ignoreEmpty) _tokens.push_back(token); |
52 | 56.6k | } |
53 | 1.28M | else if (lastToken) |
54 | 91.8k | { |
55 | 91.8k | _tokens.push_back(std::string()); |
56 | 91.8k | } |
57 | 1.34M | } |
58 | | |
59 | | |
60 | | StringTokenizer::~StringTokenizer() |
61 | 1.34M | { |
62 | 1.34M | } |
63 | | |
64 | | |
65 | | void StringTokenizer::trim(std::string& token) |
66 | 56.3k | { |
67 | 56.3k | std::string::size_type front = 0; |
68 | 56.3k | std::string::size_type back = 0; |
69 | 56.3k | std::string::size_type length = token.length(); |
70 | 56.3k | std::string::const_iterator tIt = token.begin(); |
71 | 56.3k | std::string::const_iterator tEnd = token.end(); |
72 | 62.2k | for (; tIt != tEnd; ++tIt, ++front) |
73 | 52.8k | { |
74 | 52.8k | if (!Ascii::isSpace(*tIt)) break; |
75 | 52.8k | } |
76 | 56.3k | if (tIt != tEnd) |
77 | 46.9k | { |
78 | 46.9k | std::string::const_reverse_iterator tRit = token.rbegin(); |
79 | 46.9k | std::string::const_reverse_iterator tRend = token.rend(); |
80 | 58.2k | for (; tRit != tRend; ++tRit, ++back) |
81 | 58.2k | { |
82 | 58.2k | if (!Ascii::isSpace(*tRit)) break; |
83 | 58.2k | } |
84 | 46.9k | } |
85 | 56.3k | token = token.substr(front, length - back - front); |
86 | 56.3k | } |
87 | | |
88 | | |
89 | | std::size_t StringTokenizer::count(const std::string& token) const |
90 | 0 | { |
91 | 0 | std::size_t result = 0; |
92 | 0 | TokenVec::const_iterator it = std::find(_tokens.begin(), _tokens.end(), token); |
93 | 0 | while (it != _tokens.end()) |
94 | 0 | { |
95 | 0 | result++; |
96 | 0 | it = std::find(++it, _tokens.end(), token); |
97 | 0 | } |
98 | 0 | return result; |
99 | 0 | } |
100 | | |
101 | | |
102 | | std::string::size_type StringTokenizer::find(const std::string& token, std::string::size_type pos) const |
103 | 0 | { |
104 | 0 | TokenVec::const_iterator it = std::find(_tokens.begin() + pos, _tokens.end(), token); |
105 | 0 | if (it != _tokens.end()) |
106 | 0 | { |
107 | 0 | return it - _tokens.begin(); |
108 | 0 | } |
109 | 0 | throw NotFoundException(token); |
110 | 0 | } |
111 | | |
112 | | |
113 | | bool StringTokenizer::has(const std::string& token) const |
114 | 0 | { |
115 | 0 | TokenVec::const_iterator it = std::find(_tokens.begin(), _tokens.end(), token); |
116 | 0 | return it != _tokens.end(); |
117 | 0 | } |
118 | | |
119 | | |
120 | | std::size_t StringTokenizer::replace(const std::string& oldToken, const std::string& newToken, std::string::size_type pos) |
121 | 0 | { |
122 | 0 | std::size_t result = 0; |
123 | 0 | TokenVec::iterator it = std::find(_tokens.begin() + pos, _tokens.end(), oldToken); |
124 | 0 | while (it != _tokens.end()) |
125 | 0 | { |
126 | 0 | result++; |
127 | 0 | *it = newToken; |
128 | 0 | it = std::find(++it, _tokens.end(), oldToken); |
129 | 0 | } |
130 | 0 | return result; |
131 | 0 | } |
132 | | |
133 | | |
134 | | } // namespace Poco |
135 | | |