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