TextIterator.cpp
Go to the documentation of this file.
00001 //
00002 // TextIterator.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/TextIterator.cpp#2 $
00005 //
00006 // Library: Foundation
00007 // Package: Text
00008 // Module:  TextIterator
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
00034 //
00035 
00036 
00037 #include "Poco/TextIterator.h"
00038 #include "Poco/TextEncoding.h"
00039 #include <algorithm>
00040 
00041 
00042 namespace Poco {
00043 
00044 
00045 TextIterator::TextIterator():
00046         _pEncoding(0)
00047 {
00048 }
00049 
00050 
00051 TextIterator::TextIterator(const std::string& str, const TextEncoding& encoding):
00052         _pEncoding(&encoding),
00053         _it(str.begin()),
00054         _end(str.end())
00055 {
00056 }
00057 
00058 TextIterator::TextIterator(const std::string::const_iterator& begin, const std::string::const_iterator& end, const TextEncoding& encoding):
00059         _pEncoding(&encoding),
00060         _it(begin),
00061         _end(end)
00062 {
00063 }
00064 
00065 
00066 TextIterator::TextIterator(const std::string& str):
00067         _pEncoding(0),
00068         _it(str.end()),
00069         _end(str.end())
00070 {
00071 }
00072 
00073 
00074 TextIterator::TextIterator(const std::string::const_iterator& end):
00075         _pEncoding(0),
00076         _it(end),
00077         _end(end)
00078 {
00079 }
00080 
00081 
00082 TextIterator::~TextIterator()
00083 {
00084 }
00085 
00086 
00087 TextIterator::TextIterator(const TextIterator& it):
00088         _pEncoding(it._pEncoding),
00089         _it(it._it),
00090         _end(it._end)
00091 {
00092 }
00093 
00094 
00095 TextIterator& TextIterator::operator = (const TextIterator& it)
00096 {
00097         if (&it != this)
00098         {
00099                 _pEncoding = it._pEncoding;
00100                 _it        = it._it;
00101                 _end       = it._end;
00102         }
00103         return *this;
00104 }
00105 
00106 
00107 void TextIterator::swap(TextIterator& it)
00108 {
00109         std::swap(_pEncoding, it._pEncoding);
00110         std::swap(_it, it._it);
00111         std::swap(_end, it._end);
00112 }
00113 
00114 
00115 int TextIterator::operator * () const
00116 {
00117         poco_check_ptr (_pEncoding);
00118         poco_assert (_it != _end);
00119         
00120         unsigned char c = (unsigned char) *_it;
00121         int n = _pEncoding->characterMap()[c];
00122         if (n >= -1) return n;
00123         else
00124         {
00125                 poco_assert_dbg (n >= -TextEncoding::MAX_SEQUENCE_LENGTH);
00126                 unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH];
00127                 unsigned char* p = buffer;
00128                 std::string::const_iterator it = _it;
00129                 while (n < 0 && it != _end) { *p++ = *it++; ++n; }
00130                 if (n == 0)
00131                         return _pEncoding->convert(buffer);
00132                 else
00133                         return -1;
00134         }
00135 }
00136 
00137         
00138 TextIterator& TextIterator::operator ++ ()
00139 {
00140         poco_check_ptr (_pEncoding);
00141         poco_assert (_it != _end);
00142         
00143         unsigned char c = (unsigned char) *_it;
00144         int n = _pEncoding->characterMap()[c];
00145         if (n >= -1)
00146                 ++_it;
00147         else
00148                 while (n < 0 && _it != _end) { ++_it; ++n; }
00149 
00150         return *this;
00151 }
00152 
00153 
00154 TextIterator TextIterator::operator ++ (int)
00155 {
00156         TextIterator prev(*this);
00157         operator ++ ();
00158         return prev;
00159 }
00160 
00161 
00162 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19