Bug 572417 - Release mouse capture in flash subclass after mouse events get delivered...
[mozilla-central.git] / xpcom / string / public / nsStringIterator.h
blob4565d569c480ce9a3e897ca24abeb100060e29d4
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
13 * License.
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.
22 * Contributor(s):
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"
44 #endif
46 #ifndef nsAlgorithm_h___
47 #include "nsAlgorithm.h"
48 #endif
50 #ifndef nsDebug_h___
51 #include "nsDebug.h"
52 #endif
54 /**
55 * @see nsTAString
58 template <class CharT>
59 class nsReadingIterator
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;
68 private:
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.
77 const CharT* mStart;
78 const CharT* mEnd;
79 const CharT* mPosition;
81 public:
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() {}
89 pointer
90 start() const
92 return mStart;
95 pointer
96 end() const
98 return mEnd;
101 pointer
102 get() const
104 return mPosition;
107 CharT
108 operator*() const
110 return *get();
113 #if 0
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.
116 pointer
117 operator->() const
119 return get();
121 #endif
123 self_type&
124 operator++()
126 ++mPosition;
127 return *this;
130 self_type
131 operator++( int )
133 self_type result(*this);
134 ++mPosition;
135 return result;
138 self_type&
139 operator--()
141 --mPosition;
142 return *this;
145 self_type
146 operator--( int )
148 self_type result(*this);
149 --mPosition;
150 return result;
153 difference_type
154 size_forward() const
156 return mEnd - mPosition;
159 difference_type
160 size_backward() const
162 return mPosition - mStart;
165 self_type&
166 advance( difference_type n )
168 if (n > 0)
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");
174 mPosition += step;
176 else if (n < 0)
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");
182 mPosition += step;
184 return *this;
189 * @see nsTAString
192 template <class CharT>
193 class nsWritingIterator
195 public:
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;
202 private:
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.
211 CharT* mStart;
212 CharT* mEnd;
213 CharT* mPosition;
215 public:
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() {}
223 pointer
224 start() const
226 return mStart;
229 pointer
230 end() const
232 return mEnd;
235 pointer
236 get() const
238 return mPosition;
241 reference
242 operator*() const
244 return *get();
247 #if 0
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.
250 pointer
251 operator->() const
253 return get();
255 #endif
257 self_type&
258 operator++()
260 ++mPosition;
261 return *this;
264 self_type
265 operator++( int )
267 self_type result(*this);
268 ++mPosition;
269 return result;
272 self_type&
273 operator--()
275 --mPosition;
276 return *this;
279 self_type
280 operator--( int )
282 self_type result(*this);
283 --mPosition;
284 return result;
287 difference_type
288 size_forward() const
290 return mEnd - mPosition;
293 difference_type
294 size_backward() const
296 return mPosition - mStart;
299 self_type&
300 advance( difference_type n )
302 if (n > 0)
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");
308 mPosition += step;
310 else if (n < 0)
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");
316 mPosition += step;
318 return *this;
321 void
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>
332 inline
333 PRBool
334 operator==( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
336 return lhs.get() == rhs.get();
339 template <class CharT>
340 inline
341 PRBool
342 operator!=( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
344 return lhs.get() != rhs.get();
349 // |nsWritingIterator|s
352 template <class CharT>
353 inline
354 PRBool
355 operator==( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
357 return lhs.get() == rhs.get();
360 template <class CharT>
361 inline
362 PRBool
363 operator!=( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
365 return lhs.get() != rhs.get();
368 #endif /* !defined(nsStringIterator_h___) */