added StringTokenizer::find()

This commit is contained in:
Marian Krivos
2012-04-28 09:53:56 +00:00
parent ae48e7465a
commit 9690a4b50f
2 changed files with 595 additions and 437 deletions

View File

@@ -1,131 +1,159 @@
// //
// StringTokenizer.h // StringTokenizer.h
// //
// $Id: //poco/1.4/Foundation/include/Poco/StringTokenizer.h#1 $ // $Id: //poco/1.4/Foundation/include/Poco/StringTokenizer.h#1 $
// //
// Library: Foundation // Library: Foundation
// Package: Core // Package: Core
// Module: StringTokenizer // Module: StringTokenizer
// //
// Definition of the StringTokenizer class. // Definition of the StringTokenizer class.
// //
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // 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 // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // 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 // 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 // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // 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 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#ifndef Foundation_StringTokenizer_INCLUDED #ifndef Foundation_StringTokenizer_INCLUDED
#define Foundation_StringTokenizer_INCLUDED #define Foundation_StringTokenizer_INCLUDED
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <vector> #include <vector>
#include <cstddef> #include <cstddef>
namespace Poco { namespace Poco {
class Foundation_API StringTokenizer class Foundation_API StringTokenizer
/// A simple tokenizer that splits a string into /// A simple tokenizer that splits a string into
/// tokens, which are separated by separator characters. /// tokens, which are separated by separator characters.
/// An iterator is used to iterate over all tokens. /// An iterator is used to iterate over all tokens.
{ {
public: public:
enum Options enum Options
{ {
TOK_IGNORE_EMPTY = 1, /// ignore empty tokens TOK_IGNORE_EMPTY = 1, /// ignore empty tokens
TOK_TRIM = 2 /// remove leading and trailing whitespace from tokens TOK_TRIM = 2 /// remove leading and trailing whitespace from tokens
}; };
typedef std::vector<std::string>::const_iterator Iterator; typedef std::vector<std::string> TokenVec;
typedef TokenVec::const_iterator Iterator;
StringTokenizer(const std::string& str, const std::string& separators, int options = 0);
/// Splits the given string into tokens. The tokens are expected to be StringTokenizer(const std::string& str, const std::string& separators, int options = 0);
/// separated by one of the separator characters given in separators. /// Splits the given string into tokens. The tokens are expected to be
/// Additionally, options can be specified: /// separated by one of the separator characters given in separators.
/// * TOK_IGNORE_EMPTY: empty tokens are ignored /// Additionally, options can be specified:
/// * TOK_TRIM: trailing and leading whitespace is removed from tokens. /// * TOK_IGNORE_EMPTY: empty tokens are ignored
/// An empty token at the end of str is always ignored. For example, /// * TOK_TRIM: trailing and leading whitespace is removed from tokens.
/// a StringTokenizer with the following arguments: /// An empty token at the end of str is always ignored. For example,
/// StringTokenizer(",ab,cd,", ","); /// a StringTokenizer with the following arguments:
/// will produce three tokens, "", "ab" and "cd". /// StringTokenizer(",ab,cd,", ",");
/// will produce three tokens, "", "ab" and "cd".
~StringTokenizer();
/// Destroys the tokenizer. ~StringTokenizer();
/// Destroys the tokenizer.
Iterator begin() const;
Iterator end() const; Iterator begin() const;
Iterator end() const;
const std::string& operator [] (std::size_t index) const;
/// Returns the index'th token. const std::string& operator [] (std::size_t index) const;
/// Throws a RangeException if the index is out of range. /// Returns const reference the index'th token.
/// Throws a RangeException if the index is out of range.
std::size_t count() const;
/// Returns the number of tokens. std::string& operator [] (std::size_t index);
/// Returns reference to the index'th token.
private: /// Throws a RangeException if the index is out of range.
StringTokenizer(const StringTokenizer&);
StringTokenizer& operator = (const StringTokenizer&); bool has(const std::string& token) const;
/// Returns true if token exists, false otherwise.
std::vector<std::string> _tokens;
}; std::size_t find(const std::string& token, std::size_t pos = 0) const;
/// Returns the index of the first occurence of the token
/// starting at position pos.
// /// Throws a NotFoundException if the token is not found.
// inlines
// std::size_t replace(const std::string& oldToken, const std::string& newToken, std::size_t pos = 0);
/// Starting at position pos, replaces all subsequent tokens having value
/// equal to oldToken with newToken.
inline StringTokenizer::Iterator StringTokenizer::begin() const /// Returns the number of modified tokens.
{
return _tokens.begin(); std::size_t count() const;
} /// Returns the total number of tokens.
std::size_t count(const std::string& token) const;
inline StringTokenizer::Iterator StringTokenizer::end() const /// Returns the number of tokens equal to the specified token.
{
return _tokens.end(); private:
} StringTokenizer(const StringTokenizer&);
StringTokenizer& operator = (const StringTokenizer&);
inline const std::string& StringTokenizer::operator [] (std::size_t index) const TokenVec _tokens;
{ };
if (index >= _tokens.size()) throw RangeException();
return _tokens[index];
} //
// inlines
//
inline std::size_t StringTokenizer::count() const
{
return _tokens.size(); inline StringTokenizer::Iterator StringTokenizer::begin() const
} {
return _tokens.begin();
}
} // namespace Poco
inline StringTokenizer::Iterator StringTokenizer::end() const
#endif // Foundation_StringTokenizer_INCLUDED {
return _tokens.end();
}
inline std::string& StringTokenizer::operator [] (std::size_t index)
{
if (index >= _tokens.size()) throw RangeException();
return _tokens[index];
}
inline const std::string& StringTokenizer::operator [] (std::size_t index) const
{
if (index >= _tokens.size()) throw RangeException();
return _tokens[index];
}
inline std::size_t StringTokenizer::count() const
{
return _tokens.size();
}
} // namespace Poco
#endif // Foundation_StringTokenizer_INCLUDED

View File

@@ -1,307 +1,437 @@
// //
// StringTokenizerTest.cpp // StringTokenizerTest.cpp
// //
// $Id: //poco/1.4/Foundation/testsuite/src/StringTokenizerTest.cpp#1 $ // $Id: //poco/svn/Foundation/testsuite/src/StringTokenizerTest.cpp#2 $
// //
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by // obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute, // this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the // 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 // Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following: // do so, all subject to the following:
// //
// The copyright notices in the Software and this entire statement, including // The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer, // 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 // 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 // all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by // works are solely in the form of machine-executable object code generated by
// a source language processor. // a source language processor.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // 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 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#include "StringTokenizerTest.h" #include "StringTokenizerTest.h"
#include "CppUnit/TestCaller.h" #include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h" #include "CppUnit/TestSuite.h"
#include "Poco/StringTokenizer.h" #include "Poco/StringTokenizer.h"
#include "Poco/Exception.h"
using Poco::StringTokenizer;
using Poco::StringTokenizer;
using Poco::RangeException;
StringTokenizerTest::StringTokenizerTest(const std::string& name): CppUnit::TestCase(name) using Poco::NotFoundException;
{
}
StringTokenizerTest::StringTokenizerTest(const std::string& name): CppUnit::TestCase(name)
{
StringTokenizerTest::~StringTokenizerTest() }
{
}
StringTokenizerTest::~StringTokenizerTest()
{
void StringTokenizerTest::testStringTokenizer() }
{
{
StringTokenizer st("", ""); void StringTokenizerTest::testStringTokenizer()
assert (st.begin() == st.end()); {
} {
{ StringTokenizer st("", "");
StringTokenizer st("", "", StringTokenizer::TOK_IGNORE_EMPTY); assert (st.begin() == st.end());
assert (st.begin() == st.end()); }
} {
{ StringTokenizer st("", "", StringTokenizer::TOK_IGNORE_EMPTY);
StringTokenizer st("", "", StringTokenizer::TOK_TRIM); assert (st.begin() == st.end());
assert (st.begin() == st.end()); }
} {
{ StringTokenizer st("", "", StringTokenizer::TOK_TRIM);
StringTokenizer st("", "", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); assert (st.begin() == st.end());
assert (st.begin() == st.end()); }
} {
{ StringTokenizer st("", "", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
StringTokenizer st("abc", ""); assert (st.begin() == st.end());
StringTokenizer::Iterator it = st.begin(); }
assert (it != st.end()); {
assert (*it++ == "abc"); StringTokenizer st("abc", "");
assert (it == st.end()); StringTokenizer::Iterator it = st.begin();
} assert (st.find("abc") == 0);
{ assert (it != st.end());
StringTokenizer st("abc ", "", StringTokenizer::TOK_TRIM); assert (*it++ == "abc");
StringTokenizer::Iterator it = st.begin(); assert (it == st.end());
assert (it != st.end()); }
assert (*it++ == "abc"); {
assert (it == st.end()); StringTokenizer st("abc ", "", StringTokenizer::TOK_TRIM);
} StringTokenizer::Iterator it = st.begin();
{ assert (st.find("abc") == 0);
StringTokenizer st(" abc ", "", StringTokenizer::TOK_TRIM); assert (it != st.end());
StringTokenizer::Iterator it = st.begin(); assert (*it++ == "abc");
assert (it != st.end()); assert (it == st.end());
assert (*it++ == "abc"); }
assert (it == st.end()); {
} StringTokenizer st(" abc ", "", StringTokenizer::TOK_TRIM);
{ StringTokenizer::Iterator it = st.begin();
StringTokenizer st(" abc", "", StringTokenizer::TOK_TRIM); assert (st.find("abc") == 0);
StringTokenizer::Iterator it = st.begin(); assert (it != st.end());
assert (it != st.end()); assert (*it++ == "abc");
assert (*it++ == "abc"); assert (it == st.end());
assert (it == st.end()); }
} {
{ StringTokenizer st(" abc", "", StringTokenizer::TOK_TRIM);
StringTokenizer st("abc", "b"); StringTokenizer::Iterator it = st.begin();
StringTokenizer::Iterator it = st.begin(); assert (st.find("abc") == 0);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "a"); assert (*it++ == "abc");
assert (it != st.end()); assert (it == st.end());
assert (*it++ == "c"); }
assert (it == st.end()); {
} StringTokenizer st("abc", "b");
{ StringTokenizer::Iterator it = st.begin();
StringTokenizer st("abc", "b", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); assert (st.find("a") == 0);
StringTokenizer::Iterator it = st.begin(); assert (st.find("c") == 1);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "a"); assert (*it++ == "a");
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "c"); assert (*it++ == "c");
assert (it == st.end()); assert (it == st.end());
} }
{ {
StringTokenizer st("abc", "bc"); StringTokenizer st("abc", "b", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
StringTokenizer::Iterator it = st.begin(); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find("a") == 0);
assert (*it++ == "a"); assert (st.find("c") == 1);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == ""); assert (*it++ == "a");
assert (it == st.end()); assert (it != st.end());
} assert (*it++ == "c");
{ assert (it == st.end());
StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM); }
StringTokenizer::Iterator it = st.begin(); {
assert (it != st.end()); StringTokenizer st("abc", "bc");
assert (*it++ == "a"); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find("a") == 0);
assert (*it++ == ""); assert (st.find("") == 1);
assert (it == st.end()); assert (it != st.end());
} assert (*it++ == "a");
{ assert (it != st.end());
StringTokenizer st("abc", "bc", StringTokenizer::TOK_IGNORE_EMPTY); assert (*it++ == "");
StringTokenizer::Iterator it = st.begin(); assert (it == st.end());
assert (it != st.end()); }
assert (*it++ == "a"); {
assert (it == st.end()); StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM);
} StringTokenizer::Iterator it = st.begin();
{ assert (st.find("a") == 0);
StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); assert (st.find("") == 1);
StringTokenizer::Iterator it = st.begin(); assert (it != st.end());
assert (it != st.end()); assert (*it++ == "a");
assert (*it++ == "a"); assert (it != st.end());
assert (it == st.end()); assert (*it++ == "");
} assert (it == st.end());
{ }
StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); {
StringTokenizer::Iterator it = st.begin(); StringTokenizer st("abc", "bc", StringTokenizer::TOK_IGNORE_EMPTY);
assert (it != st.end()); StringTokenizer::Iterator it = st.begin();
assert (*it++ == "a"); assert (st.find("a") == 0);
assert (it == st.end()); assert (it != st.end());
} assert (*it++ == "a");
{ assert (it == st.end());
StringTokenizer st("a a,c c", ","); }
StringTokenizer::Iterator it = st.begin(); {
assert (it != st.end()); StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
assert (*it++ == "a a"); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find("a") == 0);
assert (*it++ == "c c"); assert (it != st.end());
assert (it == st.end()); assert (*it++ == "a");
} assert (it == st.end());
{ }
StringTokenizer st("a a,c c", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); {
StringTokenizer::Iterator it = st.begin(); StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
assert (it != st.end()); StringTokenizer::Iterator it = st.begin();
assert (*it++ == "a a"); assert (st.find("a") == 0);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "c c"); assert (*it++ == "a");
assert (it == st.end()); assert (it == st.end());
} }
{ {
StringTokenizer st(" a a , , c c ", ","); StringTokenizer st("a a,c c", ",");
StringTokenizer::Iterator it = st.begin(); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find("a a") == 0);
assert (*it++ == " a a "); assert (st.find("c c") == 1);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == " "); assert (*it++ == "a a");
assert (it != st.end()); assert (it != st.end());
assert (*it++ == " c c "); assert (*it++ == "c c");
assert (it == st.end()); assert (it == st.end());
} }
{ {
StringTokenizer st(" a a , , c c ", ",", StringTokenizer::TOK_TRIM); StringTokenizer st("a a,c c", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
StringTokenizer::Iterator it = st.begin(); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find("a a") == 0);
assert (*it++ == "a a"); assert (st.find("c c") == 1);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == ""); assert (*it++ == "a a");
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "c c"); assert (*it++ == "c c");
assert (it == st.end()); assert (it == st.end());
} }
{ {
StringTokenizer st(" a a , , c c ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); StringTokenizer st(" a a , , c c ", ",");
StringTokenizer::Iterator it = st.begin(); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find(" a a ") == 0);
assert (*it++ == "a a"); assert (st.find(" ") == 1);
assert (it != st.end()); assert (st.find(" c c ") == 2);
assert (*it++ == "c c"); assert (it != st.end());
assert (it == st.end()); assert (*it++ == " a a ");
} assert (it != st.end());
{ assert (*it++ == " ");
StringTokenizer st("abc,def,,ghi , jk, l ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); assert (it != st.end());
StringTokenizer::Iterator it = st.begin(); assert (*it++ == " c c ");
assert (it != st.end()); assert (it == st.end());
assert (*it++ == "abc"); }
assert (it != st.end()); {
assert (*it++ == "def"); StringTokenizer st(" a a , , c c ", ",", StringTokenizer::TOK_TRIM);
assert (it != st.end()); StringTokenizer::Iterator it = st.begin();
assert (*it++ == "ghi"); assert (st.find("a a") == 0);
assert (it != st.end()); assert (st.find("") == 1);
assert (*it++ == "jk"); assert (st.find("c c") == 2);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "l"); assert (*it++ == "a a");
assert (it == st.end()); assert (it != st.end());
} assert (*it++ == "");
{ assert (it != st.end());
StringTokenizer st("abc,def,,ghi // jk, l ", ",/", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); assert (*it++ == "c c");
StringTokenizer::Iterator it = st.begin(); assert (it == st.end());
assert (it != st.end()); }
assert (*it++ == "abc"); {
assert (it != st.end()); StringTokenizer st(" a a , , c c ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
assert (*it++ == "def"); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find("a a") == 0);
assert (*it++ == "ghi"); assert (st.find("c c") == 1);
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "jk"); assert (*it++ == "a a");
assert (it != st.end()); assert (it != st.end());
assert (*it++ == "l"); assert (*it++ == "c c");
assert (it == st.end()); assert (it == st.end());
} }
{ {
StringTokenizer st("a/bc,def,,ghi // jk, l ", ",/", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); StringTokenizer st("abc,def,,ghi , jk, l ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
StringTokenizer::Iterator it = st.begin(); StringTokenizer::Iterator it = st.begin();
assert (it != st.end()); assert (st.find("abc") == 0);
assert (*it++ == "a"); assert (st.find("def") == 1);
assert (it != st.end()); assert (st.find("ghi") == 2);
assert (*it++ == "bc"); assert (st.find("jk") == 3);
assert (it != st.end()); assert (st.find("l") == 4);
assert (*it++ == "def"); assert (it != st.end());
assert (it != st.end()); assert (*it++ == "abc");
assert (*it++ == "ghi"); assert (it != st.end());
assert (it != st.end()); assert (*it++ == "def");
assert (*it++ == "jk"); assert (it != st.end());
assert (it != st.end()); assert (*it++ == "ghi");
assert (*it++ == "l"); assert (it != st.end());
assert (it == st.end()); assert (*it++ == "jk");
} assert (it != st.end());
{ assert (*it++ == "l");
StringTokenizer st(",ab,cd,", ","); assert (it == st.end());
StringTokenizer::Iterator it = st.begin(); }
assert (it != st.end()); {
assert (*it++ == ""); StringTokenizer st("abc,def,,ghi // jk, l ", ",/", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
assert (it != st.end()); StringTokenizer::Iterator it = st.begin();
assert (*it++ == "ab"); assert (st.find("abc") == 0);
assert (it != st.end()); assert (st.find("def") == 1);
assert (*it++ == "cd"); assert (st.find("ghi") == 2);
assert (it == st.end()); assert (st.find("jk") == 3);
} assert (st.find("l") == 4);
{ assert (it != st.end());
StringTokenizer st(",ab,cd,", ",", StringTokenizer::TOK_IGNORE_EMPTY); assert (*it++ == "abc");
StringTokenizer::Iterator it = st.begin(); assert (it != st.end());
assert (it != st.end()); assert (*it++ == "def");
assert (*it++ == "ab"); assert (it != st.end());
assert (it != st.end()); assert (*it++ == "ghi");
assert (*it++ == "cd"); assert (it != st.end());
assert (it == st.end()); assert (*it++ == "jk");
} assert (it != st.end());
{ assert (*it++ == "l");
StringTokenizer st(" , ab , cd , ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); assert (it == st.end());
StringTokenizer::Iterator it = st.begin(); }
assert (it != st.end()); {
assert (*it++ == "ab"); StringTokenizer st("a/bc,def,,ghi // jk, l ", ",/", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
assert (it != st.end()); StringTokenizer::Iterator it = st.begin();
assert (*it++ == "cd"); assert (st.find("a") == 0);
assert (it == st.end()); assert (st.find("bc") == 1);
} assert (st.find("def") == 2);
{ assert (st.find("ghi") == 3);
StringTokenizer st("1 : 2 , : 3 ", ":,", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY); assert (st.find("jk") == 4);
assert (st.count() == 3); assert (st.find("l") == 5);
assert (st[0] == "1"); assert (it != st.end());
assert (st[1] == "2"); assert (*it++ == "a");
assert (st[2] == "3"); assert (it != st.end());
} assert (*it++ == "bc");
} assert (it != st.end());
assert (*it++ == "def");
assert (it != st.end());
void StringTokenizerTest::setUp() assert (*it++ == "ghi");
{ assert (it != st.end());
} assert (*it++ == "jk");
assert (it != st.end());
assert (*it++ == "l");
void StringTokenizerTest::tearDown() assert (it == st.end());
{ }
} {
StringTokenizer st(",ab,cd,", ",");
StringTokenizer::Iterator it = st.begin();
CppUnit::Test* StringTokenizerTest::suite() assert (st.find("") == 0);
{ assert (st.find("ab") == 1);
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StringTokenizerTest"); assert (st.find("cd") == 2);
assert (it != st.end());
CppUnit_addTest(pSuite, StringTokenizerTest, testStringTokenizer); assert (*it++ == "");
assert (it != st.end());
return pSuite; assert (*it++ == "ab");
} assert (it != st.end());
assert (*it++ == "cd");
assert (it == st.end());
}
{
StringTokenizer st(",ab,cd,", ",", StringTokenizer::TOK_IGNORE_EMPTY);
StringTokenizer::Iterator it = st.begin();
assert (st.find("ab") == 0);
assert (st.find("cd") == 1);
assert (it != st.end());
assert (*it++ == "ab");
assert (it != st.end());
assert (*it++ == "cd");
assert (it == st.end());
}
{
StringTokenizer st(" , ab , cd , ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
StringTokenizer::Iterator it = st.begin();
assert (st.find("ab") == 0);
assert (st.find("cd") == 1);
assert (it != st.end());
assert (*it++ == "ab");
assert (it != st.end());
assert (*it++ == "cd");
assert (it == st.end());
}
{
StringTokenizer st("1 : 2 , : 3 ", ":,", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
assert (st.count() == 3);
assert (st[0] == "1");
assert (st[1] == "2");
assert (st[2] == "3");
assert (st.find("1") == 0);
assert (st.find("2") == 1);
assert (st.find("3") == 2);
}
}
void StringTokenizerTest::testFind()
{
StringTokenizer st("0,1,2,3,3,2,1,0", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
assert (st.count() == 8);
assert (2 == st.count("0"));
assert (2 == st.count("1"));
assert (2 == st.count("2"));
assert (2 == st.count("3"));
assert (0 == st.count("4"));
assert (0 == st.count("5"));
assert (0 == st.count("6"));
assert (0 == st.count("7"));
assert (st[0] == "0");
assert (st[1] == "1");
assert (st[2] == "2");
assert (st[3] == "3");
assert (st[4] == "3");
assert (st[5] == "2");
assert (st[6] == "1");
assert (st[7] == "0");
assert (st.has("0"));
assert (st.has("1"));
assert (st.has("2"));
assert (st.has("3"));
assert (!st.has("4"));
assert (!st.has("5"));
assert (!st.has("6"));
assert (!st.has("7"));
assert (st.find("0") == 0);
assert (st.find("1") == 1);
assert (st.find("2") == 2);
assert (st.find("3") == 3);
assert (st.find("0", 1) == 7);
assert (st.find("1", 2) == 6);
assert (st.find("2", 3) == 5);
assert (st.find("3", 4) == 4);
try
{
std::size_t p = st.find("4");
fail ("must fail");
}
catch (NotFoundException&) { }
try
{
std::string s = st[8];
fail ("must fail");
}
catch (RangeException&) { }
st[0] = "1";
st[7] = "1";
assert (st[0] == "1");
assert (st[7] == "1");
assert (0 == st.count("0"));
assert (4 == st.count("1"));
st.replace("2", "5");
assert (0 == st.count("2"));
assert (2 == st.count("5"));
st.replace("3", "6", 4);
assert (1 == st.count("3"));
assert (1 == st.count("6"));
}
void StringTokenizerTest::setUp()
{
}
void StringTokenizerTest::tearDown()
{
}
CppUnit::Test* StringTokenizerTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StringTokenizerTest");
CppUnit_addTest(pSuite, StringTokenizerTest, testStringTokenizer);
CppUnit_addTest(pSuite, StringTokenizerTest, testFind);
return pSuite;
}