1 | // |
---|
2 | // TextIterator.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/TextIterator.cpp#5 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Text |
---|
8 | // Module: TextIterator |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/TextIterator.h> |
---|
38 | #include <Poco/TextEncoding.h> |
---|
39 | #include <algorithm> |
---|
40 | |
---|
41 | |
---|
42 | namespace Poco { |
---|
43 | |
---|
44 | |
---|
45 | TextIterator::TextIterator(): |
---|
46 | _pEncoding(0) |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | TextIterator::TextIterator(const std::string& str, const TextEncoding& encoding): |
---|
52 | _pEncoding(&encoding), |
---|
53 | _it(str.begin()), |
---|
54 | _end(str.end()) |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | TextIterator::TextIterator(const std::string::const_iterator& begin, const std::string::const_iterator& end, const TextEncoding& encoding): |
---|
59 | _pEncoding(&encoding), |
---|
60 | _it(begin), |
---|
61 | _end(end) |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | TextIterator::TextIterator(const std::string& str): |
---|
67 | _pEncoding(0), |
---|
68 | _it(str.end()), |
---|
69 | _end(str.end()) |
---|
70 | { |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | TextIterator::TextIterator(const std::string::const_iterator& end): |
---|
75 | _pEncoding(0), |
---|
76 | _it(end), |
---|
77 | _end(end) |
---|
78 | { |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | TextIterator::~TextIterator() |
---|
83 | { |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | TextIterator::TextIterator(const TextIterator& it): |
---|
88 | _pEncoding(it._pEncoding), |
---|
89 | _it(it._it), |
---|
90 | _end(it._end) |
---|
91 | { |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | TextIterator& TextIterator::operator = (const TextIterator& it) |
---|
96 | { |
---|
97 | if (&it != this) |
---|
98 | { |
---|
99 | _pEncoding = it._pEncoding; |
---|
100 | _it = it._it; |
---|
101 | _end = it._end; |
---|
102 | } |
---|
103 | return *this; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | void TextIterator::swap(TextIterator& it) |
---|
108 | { |
---|
109 | std::swap(_pEncoding, it._pEncoding); |
---|
110 | std::swap(_it, it._it); |
---|
111 | std::swap(_end, it._end); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | int TextIterator::operator * () const |
---|
116 | { |
---|
117 | poco_check_ptr (_pEncoding); |
---|
118 | poco_assert (_it != _end); |
---|
119 | std::string::const_iterator it = _it; |
---|
120 | |
---|
121 | unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH]; |
---|
122 | unsigned char* p = buffer; |
---|
123 | |
---|
124 | if (it != _end) |
---|
125 | *p++ = *it++; |
---|
126 | else |
---|
127 | *p++ = 0; |
---|
128 | |
---|
129 | int read = 1; |
---|
130 | int n = _pEncoding->queryConvert(buffer, 1); |
---|
131 | |
---|
132 | while (-1 > n && (_end - it) >= -n - read) |
---|
133 | { |
---|
134 | while (read < -n && it != _end) |
---|
135 | { |
---|
136 | *p++ = *it++; |
---|
137 | read++; |
---|
138 | } |
---|
139 | n = _pEncoding->queryConvert(buffer, read); |
---|
140 | } |
---|
141 | |
---|
142 | if (-1 > n) |
---|
143 | { |
---|
144 | return -1; |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | return n; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | TextIterator& TextIterator::operator ++ () |
---|
154 | { |
---|
155 | poco_check_ptr (_pEncoding); |
---|
156 | poco_assert (_it != _end); |
---|
157 | |
---|
158 | unsigned char buffer[TextEncoding::MAX_SEQUENCE_LENGTH]; |
---|
159 | unsigned char* p = buffer; |
---|
160 | |
---|
161 | if (_it != _end) |
---|
162 | *p++ = *_it++; |
---|
163 | else |
---|
164 | *p++ = 0; |
---|
165 | |
---|
166 | int read = 1; |
---|
167 | int n = _pEncoding->sequenceLength(buffer, 1); |
---|
168 | |
---|
169 | while (-1 > n && (_end - _it) >= -n - read) |
---|
170 | { |
---|
171 | while (read < -n && _it != _end) |
---|
172 | { |
---|
173 | *p++ = *_it++; |
---|
174 | read++; |
---|
175 | } |
---|
176 | n = _pEncoding->sequenceLength(buffer, read); |
---|
177 | } |
---|
178 | while (read < n && _it != _end) |
---|
179 | { |
---|
180 | _it++; |
---|
181 | read++; |
---|
182 | } |
---|
183 | |
---|
184 | return *this; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | TextIterator TextIterator::operator ++ (int) |
---|
189 | { |
---|
190 | TextIterator prev(*this); |
---|
191 | operator ++ (); |
---|
192 | return prev; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | } // namespace Poco |
---|