xpcom/string/public/nsStringIterator.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial