/src/mozilla-central/xpcom/string/nsStringIterator.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef nsStringIterator_h___ |
8 | | #define nsStringIterator_h___ |
9 | | |
10 | | #include "nsCharTraits.h" |
11 | | #include "nsAlgorithm.h" |
12 | | #include "nsDebug.h" |
13 | | |
14 | | /** |
15 | | * @see nsTAString |
16 | | */ |
17 | | |
18 | | template <class CharT> |
19 | | class nsReadingIterator |
20 | | { |
21 | | public: |
22 | | typedef nsReadingIterator<CharT> self_type; |
23 | | typedef ptrdiff_t difference_type; |
24 | | typedef size_t size_type; |
25 | | typedef CharT value_type; |
26 | | typedef const CharT* pointer; |
27 | | typedef const CharT& reference; |
28 | | |
29 | | private: |
30 | | friend class mozilla::detail::nsTStringRepr<CharT>; |
31 | | |
32 | | // unfortunately, the API for nsReadingIterator requires that the |
33 | | // iterator know its start and end positions. this was needed when |
34 | | // we supported multi-fragment strings, but now it is really just |
35 | | // extra baggage. we should remove mStart and mEnd at some point. |
36 | | |
37 | | const CharT* mStart; |
38 | | const CharT* mEnd; |
39 | | const CharT* mPosition; |
40 | | |
41 | | public: |
42 | | nsReadingIterator() |
43 | | : mStart(nullptr) |
44 | | , mEnd(nullptr) |
45 | | , mPosition(nullptr) |
46 | 316k | { |
47 | 316k | } |
48 | | // nsReadingIterator( const nsReadingIterator<CharT>& ); // auto-generated copy-constructor OK |
49 | | // nsReadingIterator<CharT>& operator=( const nsReadingIterator<CharT>& ); // auto-generated copy-assignment operator OK |
50 | | |
51 | | pointer get() const |
52 | 10.4M | { |
53 | 10.4M | return mPosition; |
54 | 10.4M | } |
55 | | |
56 | | CharT operator*() const |
57 | | { |
58 | | return *get(); |
59 | | } |
60 | | |
61 | | self_type& operator++() |
62 | 301k | { |
63 | 301k | ++mPosition; |
64 | 301k | return *this; |
65 | 301k | } nsReadingIterator<char>::operator++() Line | Count | Source | 62 | 301k | { | 63 | 301k | ++mPosition; | 64 | 301k | return *this; | 65 | 301k | } |
Unexecuted instantiation: nsReadingIterator<char16_t>::operator++() |
66 | | |
67 | | self_type operator++(int) |
68 | | { |
69 | | self_type result(*this); |
70 | | ++mPosition; |
71 | | return result; |
72 | | } |
73 | | |
74 | | self_type& operator--() |
75 | 295k | { |
76 | 295k | --mPosition; |
77 | 295k | return *this; |
78 | 295k | } Unexecuted instantiation: nsReadingIterator<char16_t>::operator--() nsReadingIterator<char>::operator--() Line | Count | Source | 75 | 295k | { | 76 | 295k | --mPosition; | 77 | 295k | return *this; | 78 | 295k | } |
|
79 | | |
80 | | self_type operator--(int) |
81 | | { |
82 | | self_type result(*this); |
83 | | --mPosition; |
84 | | return result; |
85 | | } |
86 | | |
87 | | self_type& advance(difference_type aN) |
88 | 66 | { |
89 | 66 | if (aN > 0) { |
90 | 66 | difference_type step = XPCOM_MIN(aN, mEnd - mPosition); |
91 | 66 | |
92 | 66 | NS_ASSERTION(step > 0, |
93 | 66 | "can't advance a reading iterator beyond the end of a string"); |
94 | 66 | |
95 | 66 | mPosition += step; |
96 | 66 | } else if (aN < 0) { |
97 | 0 | difference_type step = XPCOM_MAX(aN, -(mPosition - mStart)); |
98 | 0 |
|
99 | 0 | NS_ASSERTION(step < 0, |
100 | 0 | "can't advance (backward) a reading iterator beyond the end of a string"); |
101 | 0 |
|
102 | 0 | mPosition += step; |
103 | 0 | } |
104 | 66 | return *this; |
105 | 66 | } Unexecuted instantiation: nsReadingIterator<char16_t>::advance(long) nsReadingIterator<char>::advance(long) Line | Count | Source | 88 | 66 | { | 89 | 66 | if (aN > 0) { | 90 | 66 | difference_type step = XPCOM_MIN(aN, mEnd - mPosition); | 91 | 66 | | 92 | 66 | NS_ASSERTION(step > 0, | 93 | 66 | "can't advance a reading iterator beyond the end of a string"); | 94 | 66 | | 95 | 66 | mPosition += step; | 96 | 66 | } else if (aN < 0) { | 97 | 0 | difference_type step = XPCOM_MAX(aN, -(mPosition - mStart)); | 98 | 0 |
| 99 | 0 | NS_ASSERTION(step < 0, | 100 | 0 | "can't advance (backward) a reading iterator beyond the end of a string"); | 101 | 0 |
| 102 | 0 | mPosition += step; | 103 | 0 | } | 104 | 66 | return *this; | 105 | 66 | } |
|
106 | | |
107 | | // We return an unsigned type here (with corresponding assert) rather than |
108 | | // the more usual difference_type because we want to make this class go |
109 | | // away in favor of mozilla::RangedPtr. Since RangedPtr has the same |
110 | | // requirement we are enforcing here, the transition ought to be much |
111 | | // smoother. |
112 | | size_type operator-(const self_type& aOther) const |
113 | | { |
114 | | MOZ_ASSERT(mPosition >= aOther.mPosition); |
115 | | return mPosition - aOther.mPosition; |
116 | | } |
117 | | }; |
118 | | |
119 | | template <class CharT> |
120 | | inline bool |
121 | | operator==(const nsReadingIterator<CharT>& aLhs, |
122 | | const nsReadingIterator<CharT>& aRhs) |
123 | 24.2k | { |
124 | 24.2k | return aLhs.get() == aRhs.get(); |
125 | 24.2k | } bool operator==<char>(nsReadingIterator<char> const&, nsReadingIterator<char> const&) Line | Count | Source | 123 | 24.2k | { | 124 | 24.2k | return aLhs.get() == aRhs.get(); | 125 | 24.2k | } |
Unexecuted instantiation: bool operator==<char16_t>(nsReadingIterator<char16_t> const&, nsReadingIterator<char16_t> const&) |
126 | | |
127 | | template <class CharT> |
128 | | inline bool |
129 | | operator!=(const nsReadingIterator<CharT>& aLhs, |
130 | | const nsReadingIterator<CharT>& aRhs) |
131 | 29.9M | { |
132 | 29.9M | return aLhs.get() != aRhs.get(); |
133 | 29.9M | } bool operator!=<char>(nsReadingIterator<char> const&, nsReadingIterator<char> const&) Line | Count | Source | 131 | 26.2M | { | 132 | 26.2M | return aLhs.get() != aRhs.get(); | 133 | 26.2M | } |
bool operator!=<char16_t>(nsReadingIterator<char16_t> const&, nsReadingIterator<char16_t> const&) Line | Count | Source | 131 | 3.66M | { | 132 | 3.66M | return aLhs.get() != aRhs.get(); | 133 | 3.66M | } |
|
134 | | |
135 | | |
136 | | #endif /* !defined(nsStringIterator_h___) */ |