Bug 1305422 - part 6a - add operator- support for ns{Reading,Writing}Iterator; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Thu, 29 Sep 2016 22:33:58 -0400
changeset 315876 9b814ee26cd7a8d4eeb1f9e3206a4e6b37d22b2a
parent 315875 1c06cb170aa873c9ee4e18f111a600752eba6c22
child 315877 9dc21248ab121e9d0acf01dc24d0fa297f2632ce
push id20634
push usercbook@mozilla.com
push dateFri, 30 Sep 2016 10:10:13 +0000
treeherderfx-team@afe79b010d13 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1305422
milestone52.0a1
Bug 1305422 - part 6a - add operator- support for ns{Reading,Writing}Iterator; r=erahm This change is necessary so we can start using a pair of iterators (current, end) and subtract them to figure out how far we have left to go. The current code just uses size_forward and size_backward for this purpose, and it's quite an unusual iterator that knows how far it can go until it's done.
xpcom/string/nsStringIterator.h
--- a/xpcom/string/nsStringIterator.h
+++ b/xpcom/string/nsStringIterator.h
@@ -16,16 +16,17 @@
  */
 
 template <class CharT>
 class nsReadingIterator
 {
 public:
   typedef nsReadingIterator<CharT>    self_type;
   typedef ptrdiff_t                   difference_type;
+  typedef size_t                      size_type;
   typedef CharT                       value_type;
   typedef const CharT*                pointer;
   typedef const CharT&                reference;
 
 private:
   friend class nsAString;
   friend class nsACString;
 
@@ -124,28 +125,40 @@ public:
 
       NS_ASSERTION(step < 0,
                    "can't advance (backward) a reading iterator beyond the end of a string");
 
       mPosition += step;
     }
     return *this;
   }
+
+  // We return an unsigned type here (with corresponding assert) rather than
+  // the more usual difference_type because we want to make this class go
+  // away in favor of mozilla::RangedPtr.  Since RangedPtr has the same
+  // requirement we are enforcing here, the transition ought to be much
+  // smoother.
+  size_type operator-(const self_type& aOther) const
+  {
+    MOZ_ASSERT(mPosition >= aOther.mPosition);
+    return mPosition - aOther.mPosition;
+  }
 };
 
 /**
  * @see nsTAString
  */
 
 template <class CharT>
 class nsWritingIterator
 {
 public:
   typedef nsWritingIterator<CharT>   self_type;
   typedef ptrdiff_t                  difference_type;
+  typedef size_t                     size_type;
   typedef CharT                      value_type;
   typedef CharT*                     pointer;
   typedef CharT&                     reference;
 
 private:
   friend class nsAString;
   friend class nsACString;
 
@@ -254,16 +267,27 @@ public:
   void write(const value_type* aS, uint32_t aN)
   {
     NS_ASSERTION(size_forward() > 0,
                  "You can't |write| into an |nsWritingIterator| with no space!");
 
     nsCharTraits<value_type>::move(mPosition, aS, aN);
     advance(difference_type(aN));
   }
+
+  // We return an unsigned type here (with corresponding assert) rather than
+  // the more usual difference_type because we want to make this class go
+  // away in favor of mozilla::RangedPtr.  Since RangedPtr has the same
+  // requirement we are enforcing here, the transition ought to be much
+  // smoother.
+  size_type operator-(const self_type& aOther) const
+  {
+    MOZ_ASSERT(mPosition >= aOther.mPosition);
+    return mPosition - aOther.mPosition;
+  }
 };
 
 template <class CharT>
 inline bool
 operator==(const nsReadingIterator<CharT>& aLhs,
            const nsReadingIterator<CharT>& aRhs)
 {
   return aLhs.get() == aRhs.get();