2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
// StringTokenizer.cpp
|
|
|
|
//
|
|
|
|
// $Id: //poco/1.4/Foundation/src/StringTokenizer.cpp#1 $
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Core
|
|
|
|
// Module: StringTokenizer
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person or organization
|
|
|
|
// obtaining a copy of the software and accompanying documentation covered by
|
|
|
|
// this license (the "Software") to use, reproduce, display, distribute,
|
|
|
|
// execute, and transmit the Software, and to prepare derivative works of the
|
|
|
|
// Software, and to permit third-parties to whom the Software is furnished to
|
|
|
|
// do so, all subject to the following:
|
|
|
|
//
|
|
|
|
// The copyright notices in the Software and this entire statement, including
|
|
|
|
// the above license grant, this restriction and the following disclaimer,
|
|
|
|
// must be included in all copies of the Software, in whole or in part, and
|
|
|
|
// all derivative works of the Software, unless such copies or derivative
|
|
|
|
// works are solely in the form of machine-executable object code generated by
|
|
|
|
// a source language processor.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
|
|
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
|
|
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
|
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/StringTokenizer.h"
|
|
|
|
#include "Poco/Ascii.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
StringTokenizer::StringTokenizer(const std::string& str, const std::string& separators, int options)
|
|
|
|
{
|
2012-11-07 07:30:01 +01:00
|
|
|
std::string::const_iterator it = str.begin();
|
2012-04-29 20:52:25 +02:00
|
|
|
std::string::const_iterator end = str.end();
|
2012-11-07 07:30:01 +01:00
|
|
|
std::string token;
|
|
|
|
bool doTrim = ((options & TOK_TRIM) != 0);
|
|
|
|
bool ignoreEmpty = ((options & TOK_IGNORE_EMPTY) != 0);
|
|
|
|
bool lastToken = false;
|
|
|
|
|
|
|
|
for (;it != end; ++it)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
2012-11-07 07:30:01 +01:00
|
|
|
if (separators.find(*it) != std::string::npos)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
2012-11-07 07:30:01 +01:00
|
|
|
if (doTrim) trim(token);
|
|
|
|
if (!token.empty() || !ignoreEmpty)_tokens.push_back(token);
|
|
|
|
if (!ignoreEmpty) lastToken = true;
|
|
|
|
token = "";
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-07 07:30:01 +01:00
|
|
|
token += *it;
|
|
|
|
lastToken = false;
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-07 07:30:01 +01:00
|
|
|
|
|
|
|
if (!token.empty())
|
|
|
|
{
|
|
|
|
if (doTrim) trim(token);
|
|
|
|
if (!token.empty()) _tokens.push_back(token);
|
|
|
|
}
|
|
|
|
else if (lastToken) _tokens.push_back("");
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StringTokenizer::~StringTokenizer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-07 07:30:01 +01:00
|
|
|
void StringTokenizer::trim (std::string& token)
|
|
|
|
{
|
2012-12-19 07:38:18 +01:00
|
|
|
std::size_t front = 0, back = 0, length = token.length();
|
2012-11-07 07:30:01 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
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::size_t StringTokenizer::find(const std::string& token, std::size_t 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::size_t 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
|
|
|
|
|