xpcom/string/public/nsStringIterator.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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/. */
     6 #ifndef nsStringIterator_h___
     7 #define nsStringIterator_h___
     9 #include "nsCharTraits.h"
    10 #include "nsAlgorithm.h"
    11 #include "nsDebug.h"
    13   /**
    14    * @see nsTAString
    15    */
    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;
    27     private:
    28       friend class nsAString;
    29       friend class nsACString;
    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.
    36       const CharT* mStart;
    37       const CharT* mEnd;
    38       const CharT* mPosition;
    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
    45       inline void normalize_forward() {}
    46       inline void normalize_backward() {}
    48       pointer
    49       start() const
    50         {
    51           return mStart;
    52         }
    54       pointer
    55       end() const
    56         {
    57           return mEnd;
    58         }
    60       pointer
    61       get() const
    62         {
    63           return mPosition;
    64         }
    66       CharT
    67       operator*() const
    68         {
    69           return *get();
    70         }
    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
    82       self_type&
    83       operator++()
    84         {
    85           ++mPosition;
    86           return *this;
    87         }
    89       self_type
    90       operator++( int )
    91         {
    92           self_type result(*this);
    93           ++mPosition;
    94           return result;
    95         }
    97       self_type&
    98       operator--()
    99         {
   100           --mPosition;
   101           return *this;
   102         }
   104       self_type
   105       operator--( int )
   106         {
   107           self_type result(*this);
   108           --mPosition;
   109           return result;
   110         }
   112       difference_type
   113       size_forward() const
   114         {
   115           return mEnd - mPosition;
   116         }
   118       difference_type
   119       size_backward() const
   120         {
   121           return mPosition - mStart;
   122         }
   124       self_type&
   125       advance( difference_type n )
   126         {
   127           if (n > 0)
   128             {
   129               difference_type step = XPCOM_MIN(n, size_forward());
   131               NS_ASSERTION(step>0, "can't advance a reading iterator beyond the end of a string");
   133               mPosition += step;
   134             }
   135           else if (n < 0)
   136             {
   137               difference_type step = XPCOM_MAX(n, -size_backward());
   139               NS_ASSERTION(step<0, "can't advance (backward) a reading iterator beyond the end of a string");
   141               mPosition += step;
   142             }
   143           return *this;
   144         }
   145   };
   147   /**
   148    * @see nsTAString
   149    */
   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;
   161     private:
   162       friend class nsAString;
   163       friend class nsACString;
   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.
   170       CharT* mStart;
   171       CharT* mEnd;
   172       CharT* mPosition;
   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
   179       inline void normalize_forward() {}
   180       inline void normalize_backward() {}
   182       pointer
   183       start() const
   184         {
   185           return mStart;
   186         }
   188       pointer
   189       end() const
   190         {
   191           return mEnd;
   192         }
   194       pointer
   195       get() const
   196         {
   197           return mPosition;
   198         }
   200       reference
   201       operator*() const
   202         {
   203           return *get();
   204         }
   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
   216       self_type&
   217       operator++()
   218         {
   219           ++mPosition;
   220           return *this;
   221         }
   223       self_type
   224       operator++( int )
   225         {
   226           self_type result(*this);
   227           ++mPosition;
   228           return result;
   229         }
   231       self_type&
   232       operator--()
   233         {
   234           --mPosition;
   235           return *this;
   236         }
   238       self_type
   239       operator--( int )
   240         {
   241           self_type result(*this);
   242           --mPosition;
   243           return result;
   244         }
   246       difference_type
   247       size_forward() const
   248         {
   249           return mEnd - mPosition;
   250         }
   252       difference_type
   253       size_backward() const
   254         {
   255           return mPosition - mStart;
   256         }
   258       self_type&
   259       advance( difference_type n )
   260         {
   261           if (n > 0)
   262             {
   263               difference_type step = XPCOM_MIN(n, size_forward());
   265               NS_ASSERTION(step>0, "can't advance a writing iterator beyond the end of a string");
   267               mPosition += step;
   268             }
   269           else if (n < 0)
   270             {
   271               difference_type step = XPCOM_MAX(n, -size_backward());
   273               NS_ASSERTION(step<0, "can't advance (backward) a writing iterator beyond the end of a string");
   275               mPosition += step;
   276             }
   277           return *this;
   278         }
   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!");
   285           nsCharTraits<value_type>::move(mPosition, s, n);
   286           advance( difference_type(n) );
   287         }
   288   };
   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   }
   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   }
   307   //
   308   // |nsWritingIterator|s
   309   //
   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   }
   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   }
   327 #endif /* !defined(nsStringIterator_h___) */

mercurial