xpcom/string/public/nsStringIterator.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:f03b6ca0c502
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef nsStringIterator_h___
7 #define nsStringIterator_h___
8
9 #include "nsCharTraits.h"
10 #include "nsAlgorithm.h"
11 #include "nsDebug.h"
12
13 /**
14 * @see nsTAString
15 */
16
17 template <class CharT>
18 class nsReadingIterator
19 {
20 public:
21 typedef nsReadingIterator<CharT> self_type;
22 typedef ptrdiff_t difference_type;
23 typedef CharT value_type;
24 typedef const CharT* pointer;
25 typedef const CharT& reference;
26
27 private:
28 friend class nsAString;
29 friend class nsACString;
30
31 // unfortunately, the API for nsReadingIterator requires that the
32 // iterator know its start and end positions. this was needed when
33 // we supported multi-fragment strings, but now it is really just
34 // extra baggage. we should remove mStart and mEnd at some point.
35
36 const CharT* mStart;
37 const CharT* mEnd;
38 const CharT* mPosition;
39
40 public:
41 nsReadingIterator() { }
42 // nsReadingIterator( const nsReadingIterator<CharT>& ); // auto-generated copy-constructor OK
43 // nsReadingIterator<CharT>& operator=( const nsReadingIterator<CharT>& ); // auto-generated copy-assignment operator OK
44
45 inline void normalize_forward() {}
46 inline void normalize_backward() {}
47
48 pointer
49 start() const
50 {
51 return mStart;
52 }
53
54 pointer
55 end() const
56 {
57 return mEnd;
58 }
59
60 pointer
61 get() const
62 {
63 return mPosition;
64 }
65
66 CharT
67 operator*() const
68 {
69 return *get();
70 }
71
72 #if 0
73 // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
74 // don't like this when |CharT| is a type without members.
75 pointer
76 operator->() const
77 {
78 return get();
79 }
80 #endif
81
82 self_type&
83 operator++()
84 {
85 ++mPosition;
86 return *this;
87 }
88
89 self_type
90 operator++( int )
91 {
92 self_type result(*this);
93 ++mPosition;
94 return result;
95 }
96
97 self_type&
98 operator--()
99 {
100 --mPosition;
101 return *this;
102 }
103
104 self_type
105 operator--( int )
106 {
107 self_type result(*this);
108 --mPosition;
109 return result;
110 }
111
112 difference_type
113 size_forward() const
114 {
115 return mEnd - mPosition;
116 }
117
118 difference_type
119 size_backward() const
120 {
121 return mPosition - mStart;
122 }
123
124 self_type&
125 advance( difference_type n )
126 {
127 if (n > 0)
128 {
129 difference_type step = XPCOM_MIN(n, size_forward());
130
131 NS_ASSERTION(step>0, "can't advance a reading iterator beyond the end of a string");
132
133 mPosition += step;
134 }
135 else if (n < 0)
136 {
137 difference_type step = XPCOM_MAX(n, -size_backward());
138
139 NS_ASSERTION(step<0, "can't advance (backward) a reading iterator beyond the end of a string");
140
141 mPosition += step;
142 }
143 return *this;
144 }
145 };
146
147 /**
148 * @see nsTAString
149 */
150
151 template <class CharT>
152 class nsWritingIterator
153 {
154 public:
155 typedef nsWritingIterator<CharT> self_type;
156 typedef ptrdiff_t difference_type;
157 typedef CharT value_type;
158 typedef CharT* pointer;
159 typedef CharT& reference;
160
161 private:
162 friend class nsAString;
163 friend class nsACString;
164
165 // unfortunately, the API for nsWritingIterator requires that the
166 // iterator know its start and end positions. this was needed when
167 // we supported multi-fragment strings, but now it is really just
168 // extra baggage. we should remove mStart and mEnd at some point.
169
170 CharT* mStart;
171 CharT* mEnd;
172 CharT* mPosition;
173
174 public:
175 nsWritingIterator() { }
176 // nsWritingIterator( const nsWritingIterator<CharT>& ); // auto-generated copy-constructor OK
177 // nsWritingIterator<CharT>& operator=( const nsWritingIterator<CharT>& ); // auto-generated copy-assignment operator OK
178
179 inline void normalize_forward() {}
180 inline void normalize_backward() {}
181
182 pointer
183 start() const
184 {
185 return mStart;
186 }
187
188 pointer
189 end() const
190 {
191 return mEnd;
192 }
193
194 pointer
195 get() const
196 {
197 return mPosition;
198 }
199
200 reference
201 operator*() const
202 {
203 return *get();
204 }
205
206 #if 0
207 // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
208 // don't like this when |CharT| is a type without members.
209 pointer
210 operator->() const
211 {
212 return get();
213 }
214 #endif
215
216 self_type&
217 operator++()
218 {
219 ++mPosition;
220 return *this;
221 }
222
223 self_type
224 operator++( int )
225 {
226 self_type result(*this);
227 ++mPosition;
228 return result;
229 }
230
231 self_type&
232 operator--()
233 {
234 --mPosition;
235 return *this;
236 }
237
238 self_type
239 operator--( int )
240 {
241 self_type result(*this);
242 --mPosition;
243 return result;
244 }
245
246 difference_type
247 size_forward() const
248 {
249 return mEnd - mPosition;
250 }
251
252 difference_type
253 size_backward() const
254 {
255 return mPosition - mStart;
256 }
257
258 self_type&
259 advance( difference_type n )
260 {
261 if (n > 0)
262 {
263 difference_type step = XPCOM_MIN(n, size_forward());
264
265 NS_ASSERTION(step>0, "can't advance a writing iterator beyond the end of a string");
266
267 mPosition += step;
268 }
269 else if (n < 0)
270 {
271 difference_type step = XPCOM_MAX(n, -size_backward());
272
273 NS_ASSERTION(step<0, "can't advance (backward) a writing iterator beyond the end of a string");
274
275 mPosition += step;
276 }
277 return *this;
278 }
279
280 void
281 write( const value_type* s, uint32_t n )
282 {
283 NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!");
284
285 nsCharTraits<value_type>::move(mPosition, s, n);
286 advance( difference_type(n) );
287 }
288 };
289
290 template <class CharT>
291 inline
292 bool
293 operator==( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
294 {
295 return lhs.get() == rhs.get();
296 }
297
298 template <class CharT>
299 inline
300 bool
301 operator!=( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
302 {
303 return lhs.get() != rhs.get();
304 }
305
306
307 //
308 // |nsWritingIterator|s
309 //
310
311 template <class CharT>
312 inline
313 bool
314 operator==( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
315 {
316 return lhs.get() == rhs.get();
317 }
318
319 template <class CharT>
320 inline
321 bool
322 operator!=( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
323 {
324 return lhs.get() != rhs.get();
325 }
326
327 #endif /* !defined(nsStringIterator_h___) */

mercurial