1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is Mozilla.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2001
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Scott Collins <[email protected]> (original author)
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
27 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #ifndef nsStringIterator_h___
|
---|
40 | #define nsStringIterator_h___
|
---|
41 |
|
---|
42 | #ifndef nsCharTraits_h___
|
---|
43 | #include "nsCharTraits.h"
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #ifndef nsAlgorithm_h___
|
---|
47 | #include "nsAlgorithm.h"
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | #ifndef nsDebug_h___
|
---|
51 | #include "nsDebug.h"
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * @see nsTAString
|
---|
56 | */
|
---|
57 |
|
---|
58 | template <class CharT>
|
---|
59 | class nsReadingIterator
|
---|
60 | {
|
---|
61 | public:
|
---|
62 | typedef nsReadingIterator<CharT> self_type;
|
---|
63 | typedef ptrdiff_t difference_type;
|
---|
64 | typedef CharT value_type;
|
---|
65 | typedef const CharT* pointer;
|
---|
66 | typedef const CharT& reference;
|
---|
67 |
|
---|
68 | private:
|
---|
69 | friend class nsAString;
|
---|
70 | friend class nsACString;
|
---|
71 | friend class nsSubstring;
|
---|
72 | friend class nsCSubstring;
|
---|
73 |
|
---|
74 | // unfortunately, the API for nsReadingIterator requires that the
|
---|
75 | // iterator know its start and end positions. this was needed when
|
---|
76 | // we supported multi-fragment strings, but now it is really just
|
---|
77 | // extra baggage. we should remove mStart and mEnd at some point.
|
---|
78 |
|
---|
79 | const CharT* mStart;
|
---|
80 | const CharT* mEnd;
|
---|
81 | const CharT* mPosition;
|
---|
82 |
|
---|
83 | public:
|
---|
84 | nsReadingIterator() { }
|
---|
85 | // nsReadingIterator( const nsReadingIterator<CharT>& ); // auto-generated copy-constructor OK
|
---|
86 | // nsReadingIterator<CharT>& operator=( const nsReadingIterator<CharT>& ); // auto-generated copy-assignment operator OK
|
---|
87 |
|
---|
88 | inline void normalize_forward() {}
|
---|
89 | inline void normalize_backward() {}
|
---|
90 |
|
---|
91 | pointer
|
---|
92 | start() const
|
---|
93 | {
|
---|
94 | return mStart;
|
---|
95 | }
|
---|
96 |
|
---|
97 | pointer
|
---|
98 | end() const
|
---|
99 | {
|
---|
100 | return mEnd;
|
---|
101 | }
|
---|
102 |
|
---|
103 | pointer
|
---|
104 | get() const
|
---|
105 | {
|
---|
106 | return mPosition;
|
---|
107 | }
|
---|
108 |
|
---|
109 | CharT
|
---|
110 | operator*() const
|
---|
111 | {
|
---|
112 | return *get();
|
---|
113 | }
|
---|
114 |
|
---|
115 | #if 0
|
---|
116 | // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
|
---|
117 | // don't like this when |CharT| is a type without members.
|
---|
118 | pointer
|
---|
119 | operator->() const
|
---|
120 | {
|
---|
121 | return get();
|
---|
122 | }
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | self_type&
|
---|
126 | operator++()
|
---|
127 | {
|
---|
128 | ++mPosition;
|
---|
129 | return *this;
|
---|
130 | }
|
---|
131 |
|
---|
132 | self_type
|
---|
133 | operator++( int )
|
---|
134 | {
|
---|
135 | self_type result(*this);
|
---|
136 | ++mPosition;
|
---|
137 | return result;
|
---|
138 | }
|
---|
139 |
|
---|
140 | self_type&
|
---|
141 | operator--()
|
---|
142 | {
|
---|
143 | --mPosition;
|
---|
144 | return *this;
|
---|
145 | }
|
---|
146 |
|
---|
147 | self_type
|
---|
148 | operator--( int )
|
---|
149 | {
|
---|
150 | self_type result(*this);
|
---|
151 | --mPosition;
|
---|
152 | return result;
|
---|
153 | }
|
---|
154 |
|
---|
155 | difference_type
|
---|
156 | size_forward() const
|
---|
157 | {
|
---|
158 | return mEnd - mPosition;
|
---|
159 | }
|
---|
160 |
|
---|
161 | difference_type
|
---|
162 | size_backward() const
|
---|
163 | {
|
---|
164 | return mPosition - mStart;
|
---|
165 | }
|
---|
166 |
|
---|
167 | self_type&
|
---|
168 | advance( difference_type n )
|
---|
169 | {
|
---|
170 | if (n > 0)
|
---|
171 | {
|
---|
172 | difference_type step = NS_MIN(n, size_forward());
|
---|
173 |
|
---|
174 | NS_ASSERTION(step>0, "can't advance a reading iterator beyond the end of a string");
|
---|
175 |
|
---|
176 | mPosition += step;
|
---|
177 | }
|
---|
178 | else if (n < 0)
|
---|
179 | {
|
---|
180 | difference_type step = NS_MAX(n, -size_backward());
|
---|
181 |
|
---|
182 | NS_ASSERTION(step<0, "can't advance (backward) a reading iterator beyond the end of a string");
|
---|
183 |
|
---|
184 | mPosition += step;
|
---|
185 | }
|
---|
186 | return *this;
|
---|
187 | }
|
---|
188 | };
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * @see nsTAString
|
---|
192 | */
|
---|
193 |
|
---|
194 | template <class CharT>
|
---|
195 | class nsWritingIterator
|
---|
196 | {
|
---|
197 | public:
|
---|
198 | typedef nsWritingIterator<CharT> self_type;
|
---|
199 | typedef ptrdiff_t difference_type;
|
---|
200 | typedef CharT value_type;
|
---|
201 | typedef CharT* pointer;
|
---|
202 | typedef CharT& reference;
|
---|
203 |
|
---|
204 | private:
|
---|
205 | friend class nsAString;
|
---|
206 | friend class nsACString;
|
---|
207 | friend class nsSubstring;
|
---|
208 | friend class nsCSubstring;
|
---|
209 |
|
---|
210 | // unfortunately, the API for nsWritingIterator requires that the
|
---|
211 | // iterator know its start and end positions. this was needed when
|
---|
212 | // we supported multi-fragment strings, but now it is really just
|
---|
213 | // extra baggage. we should remove mStart and mEnd at some point.
|
---|
214 |
|
---|
215 | CharT* mStart;
|
---|
216 | CharT* mEnd;
|
---|
217 | CharT* mPosition;
|
---|
218 |
|
---|
219 | public:
|
---|
220 | nsWritingIterator() { }
|
---|
221 | // nsWritingIterator( const nsWritingIterator<CharT>& ); // auto-generated copy-constructor OK
|
---|
222 | // nsWritingIterator<CharT>& operator=( const nsWritingIterator<CharT>& ); // auto-generated copy-assignment operator OK
|
---|
223 |
|
---|
224 | inline void normalize_forward() {}
|
---|
225 | inline void normalize_backward() {}
|
---|
226 |
|
---|
227 | pointer
|
---|
228 | start() const
|
---|
229 | {
|
---|
230 | return mStart;
|
---|
231 | }
|
---|
232 |
|
---|
233 | pointer
|
---|
234 | end() const
|
---|
235 | {
|
---|
236 | return mEnd;
|
---|
237 | }
|
---|
238 |
|
---|
239 | pointer
|
---|
240 | get() const
|
---|
241 | {
|
---|
242 | return mPosition;
|
---|
243 | }
|
---|
244 |
|
---|
245 | reference
|
---|
246 | operator*() const
|
---|
247 | {
|
---|
248 | return *get();
|
---|
249 | }
|
---|
250 |
|
---|
251 | #if 0
|
---|
252 | // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
|
---|
253 | // don't like this when |CharT| is a type without members.
|
---|
254 | pointer
|
---|
255 | operator->() const
|
---|
256 | {
|
---|
257 | return get();
|
---|
258 | }
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | self_type&
|
---|
262 | operator++()
|
---|
263 | {
|
---|
264 | ++mPosition;
|
---|
265 | return *this;
|
---|
266 | }
|
---|
267 |
|
---|
268 | self_type
|
---|
269 | operator++( int )
|
---|
270 | {
|
---|
271 | self_type result(*this);
|
---|
272 | ++mPosition;
|
---|
273 | return result;
|
---|
274 | }
|
---|
275 |
|
---|
276 | self_type&
|
---|
277 | operator--()
|
---|
278 | {
|
---|
279 | --mPosition;
|
---|
280 | return *this;
|
---|
281 | }
|
---|
282 |
|
---|
283 | self_type
|
---|
284 | operator--( int )
|
---|
285 | {
|
---|
286 | self_type result(*this);
|
---|
287 | --mPosition;
|
---|
288 | return result;
|
---|
289 | }
|
---|
290 |
|
---|
291 | difference_type
|
---|
292 | size_forward() const
|
---|
293 | {
|
---|
294 | return mEnd - mPosition;
|
---|
295 | }
|
---|
296 |
|
---|
297 | difference_type
|
---|
298 | size_backward() const
|
---|
299 | {
|
---|
300 | return mPosition - mStart;
|
---|
301 | }
|
---|
302 |
|
---|
303 | self_type&
|
---|
304 | advance( difference_type n )
|
---|
305 | {
|
---|
306 | if (n > 0)
|
---|
307 | {
|
---|
308 | difference_type step = NS_MIN(n, size_forward());
|
---|
309 |
|
---|
310 | NS_ASSERTION(step>0, "can't advance a writing iterator beyond the end of a string");
|
---|
311 |
|
---|
312 | mPosition += step;
|
---|
313 | }
|
---|
314 | else if (n < 0)
|
---|
315 | {
|
---|
316 | difference_type step = NS_MAX(n, -size_backward());
|
---|
317 |
|
---|
318 | NS_ASSERTION(step<0, "can't advance (backward) a writing iterator beyond the end of a string");
|
---|
319 |
|
---|
320 | mPosition += step;
|
---|
321 | }
|
---|
322 | return *this;
|
---|
323 | }
|
---|
324 |
|
---|
325 | PRUint32
|
---|
326 | write( const value_type* s, PRUint32 n )
|
---|
327 | {
|
---|
328 | NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!");
|
---|
329 |
|
---|
330 | nsCharTraits<value_type>::move(mPosition, s, n);
|
---|
331 | advance( difference_type(n) );
|
---|
332 | return n;
|
---|
333 | }
|
---|
334 | };
|
---|
335 |
|
---|
336 | template <class CharT>
|
---|
337 | inline
|
---|
338 | PRBool
|
---|
339 | operator==( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
---|
340 | {
|
---|
341 | return lhs.get() == rhs.get();
|
---|
342 | }
|
---|
343 |
|
---|
344 | template <class CharT>
|
---|
345 | inline
|
---|
346 | PRBool
|
---|
347 | operator!=( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
---|
348 | {
|
---|
349 | return lhs.get() != rhs.get();
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | //
|
---|
354 | // |nsWritingIterator|s
|
---|
355 | //
|
---|
356 |
|
---|
357 | template <class CharT>
|
---|
358 | inline
|
---|
359 | PRBool
|
---|
360 | operator==( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
---|
361 | {
|
---|
362 | return lhs.get() == rhs.get();
|
---|
363 | }
|
---|
364 |
|
---|
365 | template <class CharT>
|
---|
366 | inline
|
---|
367 | PRBool
|
---|
368 | operator!=( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
---|
369 | {
|
---|
370 | return lhs.get() != rhs.get();
|
---|
371 | }
|
---|
372 |
|
---|
373 | #endif /* !defined(nsStringIterator_h___) */
|
---|