| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #ifndef QSTRINGITERATOR_H |
| 42 | #define QSTRINGITERATOR_H |
| 43 | |
| 44 | // |
| 45 | // W A R N I N G |
| 46 | // ------------- |
| 47 | // |
| 48 | // This file is not part of the Qt API. It exists purely as an |
| 49 | // implementation detail. This header file may change from version to |
| 50 | // version without notice, or even be removed. |
| 51 | // |
| 52 | // We mean it. |
| 53 | // |
| 54 | |
| 55 | #include <QtCore/private/qglobal_p.h> |
| 56 | #include <QtCore/qstring.h> |
| 57 | |
| 58 | QT_BEGIN_NAMESPACE |
| 59 | |
| 60 | class QStringIterator |
| 61 | { |
| 62 | QString::const_iterator i, pos, e; |
| 63 | Q_STATIC_ASSERT((std::is_same<QString::const_iterator, const QChar *>::value)); |
| 64 | public: |
| 65 | explicit QStringIterator(QStringView string, qsizetype idx = 0) |
| 66 | : i(string.begin()), |
| 67 | pos(i + idx), |
| 68 | e(string.end()) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | inline explicit QStringIterator(const QChar *begin, const QChar *end) |
| 73 | : i(begin), |
| 74 | pos(begin), |
| 75 | e(end) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | inline explicit QStringIterator(const QChar *begin, int idx, const QChar *end) |
| 80 | : i(begin), |
| 81 | pos(begin + idx), |
| 82 | e(end) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | inline QString::const_iterator position() const |
| 87 | { |
| 88 | return pos; |
| 89 | } |
| 90 | |
| 91 | inline int index() const |
| 92 | { |
| 93 | return pos - i; |
| 94 | } |
| 95 | |
| 96 | inline void setPosition(QString::const_iterator position) |
| 97 | { |
| 98 | Q_ASSERT_X(i <= position && position <= e, Q_FUNC_INFO, "position out of bounds" ); |
| 99 | pos = position; |
| 100 | } |
| 101 | |
| 102 | // forward iteration |
| 103 | |
| 104 | inline bool hasNext() const |
| 105 | { |
| 106 | return pos < e; |
| 107 | } |
| 108 | |
| 109 | inline void advance() |
| 110 | { |
| 111 | Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item" ); |
| 112 | |
| 113 | if (Q_UNLIKELY((pos++)->isHighSurrogate())) { |
| 114 | if (Q_LIKELY(pos != e && pos->isLowSurrogate())) |
| 115 | ++pos; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | inline void advanceUnchecked() |
| 120 | { |
| 121 | Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item" ); |
| 122 | |
| 123 | if (Q_UNLIKELY((pos++)->isHighSurrogate())) |
| 124 | ++pos; |
| 125 | } |
| 126 | |
| 127 | inline uint peekNextUnchecked() const |
| 128 | { |
| 129 | Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item" ); |
| 130 | |
| 131 | if (Q_UNLIKELY(pos->isHighSurrogate())) |
| 132 | return QChar::surrogateToUcs4(high: pos[0], low: pos[1]); |
| 133 | |
| 134 | return pos->unicode(); |
| 135 | } |
| 136 | |
| 137 | inline uint peekNext(uint invalidAs = QChar::ReplacementCharacter) const |
| 138 | { |
| 139 | Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item" ); |
| 140 | |
| 141 | if (Q_UNLIKELY(pos->isSurrogate())) { |
| 142 | if (Q_LIKELY(pos->isHighSurrogate())) { |
| 143 | const QChar *low = pos + 1; |
| 144 | if (Q_LIKELY(low != e && low->isLowSurrogate())) |
| 145 | return QChar::surrogateToUcs4(high: *pos, low: *low); |
| 146 | } |
| 147 | return invalidAs; |
| 148 | } |
| 149 | |
| 150 | return pos->unicode(); |
| 151 | } |
| 152 | |
| 153 | inline uint nextUnchecked() |
| 154 | { |
| 155 | Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item" ); |
| 156 | |
| 157 | const QChar cur = *pos++; |
| 158 | if (Q_UNLIKELY(cur.isHighSurrogate())) |
| 159 | return QChar::surrogateToUcs4(high: cur, low: *pos++); |
| 160 | return cur.unicode(); |
| 161 | } |
| 162 | |
| 163 | inline uint next(uint invalidAs = QChar::ReplacementCharacter) |
| 164 | { |
| 165 | Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item" ); |
| 166 | |
| 167 | const QChar uc = *pos++; |
| 168 | if (Q_UNLIKELY(uc.isSurrogate())) { |
| 169 | if (Q_LIKELY(uc.isHighSurrogate() && pos < e && pos->isLowSurrogate())) |
| 170 | return QChar::surrogateToUcs4(high: uc, low: *pos++); |
| 171 | return invalidAs; |
| 172 | } |
| 173 | |
| 174 | return uc.unicode(); |
| 175 | } |
| 176 | |
| 177 | // backwards iteration |
| 178 | |
| 179 | inline bool hasPrevious() const |
| 180 | { |
| 181 | return pos > i; |
| 182 | } |
| 183 | |
| 184 | inline void recede() |
| 185 | { |
| 186 | Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item" ); |
| 187 | |
| 188 | if (Q_UNLIKELY((--pos)->isLowSurrogate())) { |
| 189 | const QChar *high = pos - 1; |
| 190 | if (Q_LIKELY(high != i - 1 && high->isHighSurrogate())) |
| 191 | --pos; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | inline void recedeUnchecked() |
| 196 | { |
| 197 | Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item" ); |
| 198 | |
| 199 | if (Q_UNLIKELY((--pos)->isLowSurrogate())) |
| 200 | --pos; |
| 201 | } |
| 202 | |
| 203 | inline uint peekPreviousUnchecked() const |
| 204 | { |
| 205 | Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item" ); |
| 206 | |
| 207 | if (Q_UNLIKELY(pos[-1].isLowSurrogate())) |
| 208 | return QChar::surrogateToUcs4(high: pos[-2], low: pos[-1]); |
| 209 | return pos[-1].unicode(); |
| 210 | } |
| 211 | |
| 212 | inline uint peekPrevious(uint invalidAs = QChar::ReplacementCharacter) const |
| 213 | { |
| 214 | Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item" ); |
| 215 | |
| 216 | if (Q_UNLIKELY(pos[-1].isSurrogate())) { |
| 217 | if (Q_LIKELY(pos[-1].isLowSurrogate())) { |
| 218 | const QChar *high = pos - 2; |
| 219 | if (Q_LIKELY(high != i - 1 && high->isHighSurrogate())) |
| 220 | return QChar::surrogateToUcs4(high: *high, low: pos[-1]); |
| 221 | } |
| 222 | return invalidAs; |
| 223 | } |
| 224 | |
| 225 | return pos[-1].unicode(); |
| 226 | } |
| 227 | |
| 228 | inline uint previousUnchecked() |
| 229 | { |
| 230 | Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item" ); |
| 231 | |
| 232 | const QChar cur = *--pos; |
| 233 | if (Q_UNLIKELY(cur.isLowSurrogate())) |
| 234 | return QChar::surrogateToUcs4(high: *--pos, low: cur); |
| 235 | return cur.unicode(); |
| 236 | } |
| 237 | |
| 238 | inline uint previous(uint invalidAs = QChar::ReplacementCharacter) |
| 239 | { |
| 240 | Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item" ); |
| 241 | |
| 242 | const QChar uc = *--pos; |
| 243 | if (Q_UNLIKELY(uc.isSurrogate())) { |
| 244 | if (Q_LIKELY(uc.isLowSurrogate() && pos > i && pos[-1].isHighSurrogate())) |
| 245 | return QChar::surrogateToUcs4(high: *--pos, low: uc); |
| 246 | return invalidAs; |
| 247 | } |
| 248 | |
| 249 | return uc.unicode(); |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | QT_END_NAMESPACE |
| 254 | |
| 255 | #endif // QSTRINGITERATOR_H |
| 256 | |