OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 template<class T> class TokenSequenceConstIterator; // forward declaration |
| 19 |
| 20 /** |
| 21 * A sequence of delimited tokens contained within a given string. |
| 22 */ |
| 23 template<class T> |
| 24 class TokenSequence |
| 25 { |
| 26 typedef TokenSequenceConstIterator<T> ConstIterator; |
| 27 friend class ConstIterator; |
| 28 const T delimiters; |
| 29 const T value; |
| 30 |
| 31 /** |
| 32 * Default constructor is deleted |
| 33 */ |
| 34 TokenSequence(); // = delete |
| 35 |
| 36 public: |
| 37 TokenSequence(const T& value, const T& delimiters) |
| 38 : value(value), delimiters(delimiters) |
| 39 { |
| 40 if (delimiters.empty()) |
| 41 { |
| 42 // If we don't have delimiter characters, the 'first_first_*' functions wi
ll have undefined behavior. |
| 43 throw std::runtime_error("Must have at least one delimiter character"); |
| 44 } |
| 45 } |
| 46 |
| 47 ConstIterator cbegin() const |
| 48 { |
| 49 return ConstIterator(*this, ConstIterator::BeginMarker()); |
| 50 } |
| 51 |
| 52 ConstIterator cend() const |
| 53 { |
| 54 return ConstIterator(*this, ConstIterator::EndMarker()); |
| 55 } |
| 56 }; |
| 57 |
| 58 /** |
| 59 * Iterator class for TokenSequence<T> |
| 60 * |
| 61 * All constructors are private, only available to TokenSequence<T> through a fr
iend declaration. |
| 62 * |
| 63 * Invariant |
| 64 * if posFirstCharInToken == npos |
| 65 * iterator is "end()" |
| 66 * else |
| 67 * 0 <= posFirstCharInToken < posFirstCharNotInToken <= length of underlying
container string |
| 68 * first token position = posFirstCharInToken |
| 69 * token length = posFirstCharNotInToken - posFirstCharInToken |
| 70 */ |
| 71 template<class T> |
| 72 class TokenSequenceConstIterator |
| 73 { |
| 74 typedef TokenSequence<T> Container; |
| 75 friend class Container; |
| 76 const Container& sequence; |
| 77 size_t posFirstCharInToken; |
| 78 size_t posFirstCharNotInToken; |
| 79 |
| 80 class BeginMarker {}; |
| 81 class EndMarker {}; |
| 82 |
| 83 /** |
| 84 * Default constructor is deleted |
| 85 */ |
| 86 TokenSequenceConstIterator(); // = delete |
| 87 |
| 88 /** |
| 89 * Constructor for cbegin() |
| 90 */ |
| 91 TokenSequenceConstIterator(const TokenSequence<T>& sequence, BeginMarker) |
| 92 : sequence(sequence) |
| 93 { |
| 94 Advance(0); |
| 95 } |
| 96 |
| 97 /** |
| 98 * Constructor for cend() |
| 99 */ |
| 100 TokenSequenceConstIterator(const TokenSequence<T>& sequence, EndMarker) |
| 101 : sequence(sequence), posFirstCharInToken(T::npos) |
| 102 { |
| 103 } |
| 104 |
| 105 /** |
| 106 * Find the next token beginning at or after the given position |
| 107 */ |
| 108 void Advance(size_t posStart) |
| 109 { |
| 110 posFirstCharInToken = sequence.value.find_first_not_of(sequence.delimiters,
posStart); |
| 111 if (posFirstCharInToken == T::npos) |
| 112 { |
| 113 // If we've become an "end()" iterator, we're done. |
| 114 return; |
| 115 } |
| 116 posFirstCharNotInToken = sequence.value.find_first_of(sequence.delimiters, p
osFirstCharInToken + 1); |
| 117 if (posFirstCharNotInToken == T::npos) |
| 118 { |
| 119 posFirstCharNotInToken = sequence.value.length(); |
| 120 } |
| 121 } |
| 122 |
| 123 public: |
| 124 bool operator==(const TokenSequenceConstIterator& other) const |
| 125 { |
| 126 if (&sequence != &other.sequence) |
| 127 { |
| 128 return false; // iterators to different sequences cannot be equal |
| 129 } |
| 130 // Iterators with the same initial token position will also have the same fi
nal final position |
| 131 // "end()" iterators use "npos" as their position |
| 132 return posFirstCharInToken == other.posFirstCharInToken; |
| 133 } |
| 134 |
| 135 bool operator!=(const TokenSequenceConstIterator& other) const |
| 136 { |
| 137 return !operator==(other); |
| 138 } |
| 139 |
| 140 void operator++() |
| 141 { |
| 142 if (posFirstCharInToken == T::npos) |
| 143 { |
| 144 return; // Incrementing "end()" yields "end()" |
| 145 } |
| 146 if (posFirstCharNotInToken >= sequence.value.length()) |
| 147 { |
| 148 // We are already at the end of the sequence. No need to search more. |
| 149 posFirstCharInToken = T::npos; |
| 150 return; |
| 151 } |
| 152 Advance(posFirstCharNotInToken + 1); |
| 153 } |
| 154 |
| 155 T operator*() const |
| 156 { |
| 157 if (posFirstCharInToken == T::npos) |
| 158 { |
| 159 throw std::runtime_error("Cannot dereference end() iterator"); |
| 160 } |
| 161 return sequence.value.substr(posFirstCharInToken, posFirstCharNotInToken - p
osFirstCharInToken); |
| 162 } |
| 163 }; |
OLD | NEW |