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
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/
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
15 * The Original Code is Mozilla.
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.
23 * Scott Collins <scc@mozilla.org> (original author)
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.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsStringIterator_h___
40 #define nsStringIterator_h___
42 #ifndef nsCharTraits_h___
43 #include "nsCharTraits.h"
46 #ifndef nsAlgorithm_h___
47 #include "nsAlgorithm.h"
58 template <class CharT
>
59 class nsReadingIterator
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
;
69 friend class nsAString
;
70 friend class nsACString
;
72 // unfortunately, the API for nsReadingIterator requires that the
73 // iterator know its start and end positions. this was needed when
74 // we supported multi-fragment strings, but now it is really just
75 // extra baggage. we should remove mStart and mEnd at some point.
79 const CharT
* mPosition
;
82 nsReadingIterator() { }
83 // nsReadingIterator( const nsReadingIterator<CharT>& ); // auto-generated copy-constructor OK
84 // nsReadingIterator<CharT>& operator=( const nsReadingIterator<CharT>& ); // auto-generated copy-assignment operator OK
86 inline void normalize_forward() {}
87 inline void normalize_backward() {}
114 // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
115 // don't like this when |CharT| is a type without members.
133 self_type
result(*this);
148 self_type
result(*this);
156 return mEnd
- mPosition
;
160 size_backward() const
162 return mPosition
- mStart
;
166 advance( difference_type n
)
170 difference_type step
= NS_MIN(n
, size_forward());
172 NS_ASSERTION(step
>0, "can't advance a reading iterator beyond the end of a string");
178 difference_type step
= NS_MAX(n
, -size_backward());
180 NS_ASSERTION(step
<0, "can't advance (backward) a reading iterator beyond the end of a string");
192 template <class CharT
>
193 class nsWritingIterator
196 typedef nsWritingIterator
<CharT
> self_type
;
197 typedef ptrdiff_t difference_type
;
198 typedef CharT value_type
;
199 typedef CharT
* pointer
;
200 typedef CharT
& reference
;
203 friend class nsAString
;
204 friend class nsACString
;
206 // unfortunately, the API for nsWritingIterator requires that the
207 // iterator know its start and end positions. this was needed when
208 // we supported multi-fragment strings, but now it is really just
209 // extra baggage. we should remove mStart and mEnd at some point.
216 nsWritingIterator() { }
217 // nsWritingIterator( const nsWritingIterator<CharT>& ); // auto-generated copy-constructor OK
218 // nsWritingIterator<CharT>& operator=( const nsWritingIterator<CharT>& ); // auto-generated copy-assignment operator OK
220 inline void normalize_forward() {}
221 inline void normalize_backward() {}
248 // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
249 // don't like this when |CharT| is a type without members.
267 self_type
result(*this);
282 self_type
result(*this);
290 return mEnd
- mPosition
;
294 size_backward() const
296 return mPosition
- mStart
;
300 advance( difference_type n
)
304 difference_type step
= NS_MIN(n
, size_forward());
306 NS_ASSERTION(step
>0, "can't advance a writing iterator beyond the end of a string");
312 difference_type step
= NS_MAX(n
, -size_backward());
314 NS_ASSERTION(step
<0, "can't advance (backward) a writing iterator beyond the end of a string");
322 write( const value_type
* s
, PRUint32 n
)
324 NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!");
326 nsCharTraits
<value_type
>::move(mPosition
, s
, n
);
327 advance( difference_type(n
) );
331 template <class CharT
>
334 operator==( const nsReadingIterator
<CharT
>& lhs
, const nsReadingIterator
<CharT
>& rhs
)
336 return lhs
.get() == rhs
.get();
339 template <class CharT
>
342 operator!=( const nsReadingIterator
<CharT
>& lhs
, const nsReadingIterator
<CharT
>& rhs
)
344 return lhs
.get() != rhs
.get();
349 // |nsWritingIterator|s
352 template <class CharT
>
355 operator==( const nsWritingIterator
<CharT
>& lhs
, const nsWritingIterator
<CharT
>& rhs
)
357 return lhs
.get() == rhs
.get();
360 template <class CharT
>
363 operator!=( const nsWritingIterator
<CharT
>& lhs
, const nsWritingIterator
<CharT
>& rhs
)
365 return lhs
.get() != rhs
.get();
368 #endif /* !defined(nsStringIterator_h___) */