QtBase  v6.3.1
qstringiterator_p.h
Go to the documentation of this file.
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 
59 
61 {
62  QString::const_iterator i, pos, e;
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 
87  {
88  return pos;
89  }
90 
91  inline int index() const
92  {
93  return int(pos - i);
94  }
95 
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  Q_ASSERT(pos < e && pos->isLowSurrogate());
125  ++pos;
126  }
127  }
128 
129  inline char32_t peekNextUnchecked() const
130  {
131  Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item");
132 
133  if (Q_UNLIKELY(pos->isHighSurrogate())) {
134  Q_ASSERT(pos + 1 < e && pos[1].isLowSurrogate());
135  return QChar::surrogateToUcs4(pos[0], pos[1]);
136  }
137 
138  return pos->unicode();
139  }
140 
141  inline char32_t peekNext(char32_t invalidAs = QChar::ReplacementCharacter) const
142  {
143  Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item");
144 
145  if (Q_UNLIKELY(pos->isSurrogate())) {
146  if (Q_LIKELY(pos->isHighSurrogate())) {
147  const QChar *low = pos + 1;
148  if (Q_LIKELY(low != e && low->isLowSurrogate()))
149  return QChar::surrogateToUcs4(*pos, *low);
150  }
151  return invalidAs;
152  }
153 
154  return pos->unicode();
155  }
156 
157  inline char32_t nextUnchecked()
158  {
159  Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item");
160 
161  const QChar cur = *pos++;
162  if (Q_UNLIKELY(cur.isHighSurrogate())) {
163  Q_ASSERT(pos < e && pos->isLowSurrogate());
164  return QChar::surrogateToUcs4(cur, *pos++);
165  }
166  return cur.unicode();
167  }
168 
169  inline char32_t next(char32_t invalidAs = QChar::ReplacementCharacter)
170  {
171  Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item");
172 
173  const QChar uc = *pos++;
174  if (Q_UNLIKELY(uc.isSurrogate())) {
175  if (Q_LIKELY(uc.isHighSurrogate() && pos < e && pos->isLowSurrogate()))
176  return QChar::surrogateToUcs4(uc, *pos++);
177  return invalidAs;
178  }
179 
180  return uc.unicode();
181  }
182 
183  // backwards iteration
184 
185  inline bool hasPrevious() const
186  {
187  return pos > i;
188  }
189 
190  inline void recede()
191  {
192  Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item");
193 
194  if (Q_UNLIKELY((--pos)->isLowSurrogate())) {
195  const QChar *high = pos - 1;
196  if (Q_LIKELY(high != i - 1 && high->isHighSurrogate()))
197  --pos;
198  }
199  }
200 
201  inline void recedeUnchecked()
202  {
203  Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item");
204 
205  if (Q_UNLIKELY((--pos)->isLowSurrogate())) {
206  Q_ASSERT(pos > i && pos[-1].isHighSurrogate());
207  --pos;
208  }
209  }
210 
211  inline char32_t peekPreviousUnchecked() const
212  {
213  Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item");
214 
215  if (Q_UNLIKELY(pos[-1].isLowSurrogate())) {
216  Q_ASSERT(pos > i + 1 && pos[-2].isHighSurrogate());
217  return QChar::surrogateToUcs4(pos[-2], pos[-1]);
218  }
219  return pos[-1].unicode();
220  }
221 
222  inline char32_t peekPrevious(char32_t invalidAs = QChar::ReplacementCharacter) const
223  {
224  Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item");
225 
226  if (Q_UNLIKELY(pos[-1].isSurrogate())) {
227  if (Q_LIKELY(pos[-1].isLowSurrogate())) {
228  const QChar *high = pos - 2;
229  if (Q_LIKELY(high != i - 1 && high->isHighSurrogate()))
230  return QChar::surrogateToUcs4(*high, pos[-1]);
231  }
232  return invalidAs;
233  }
234 
235  return pos[-1].unicode();
236  }
237 
238  inline char32_t previousUnchecked()
239  {
240  Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item");
241 
242  const QChar cur = *--pos;
243  if (Q_UNLIKELY(cur.isLowSurrogate())) {
244  Q_ASSERT(pos > i && pos[-1].isHighSurrogate());
245  return QChar::surrogateToUcs4(*--pos, cur);
246  }
247  return cur.unicode();
248  }
249 
250  inline char32_t previous(char32_t invalidAs = QChar::ReplacementCharacter)
251  {
252  Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item");
253 
254  const QChar uc = *--pos;
255  if (Q_UNLIKELY(uc.isSurrogate())) {
256  if (Q_LIKELY(uc.isLowSurrogate() && pos > i && pos[-1].isHighSurrogate()))
257  return QChar::surrogateToUcs4(*--pos, uc);
258  return invalidAs;
259  }
260 
261  return uc.unicode();
262  }
263 };
264 
266 
267 #endif // QSTRINGITERATOR_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
#define value
[5]
FT_UInt idx
Definition: cffcmap.c:135
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:84
static constexpr char32_t surrogateToUcs4(char16_t high, char16_t low) noexcept
Definition: qchar.h:539
@ ReplacementCharacter
Definition: qchar.h:95
constexpr bool isLowSurrogate() const noexcept
Definition: qchar.h:511
constexpr bool isSurrogate() const noexcept
Definition: qchar.h:512
constexpr char16_t unicode() const noexcept
Definition: qchar.h:489
constexpr bool isHighSurrogate() const noexcept
Definition: qchar.h:510
char32_t peekNext(char32_t invalidAs=QChar::ReplacementCharacter) const
char32_t nextUnchecked()
char32_t next(char32_t invalidAs=QChar::ReplacementCharacter)
QStringIterator(const QChar *begin, int idx, const QChar *end)
QString::const_iterator position() const
char32_t previous(char32_t invalidAs=QChar::ReplacementCharacter)
QStringIterator(QStringView string, qsizetype idx=0)
bool hasNext() const
char32_t peekPreviousUnchecked() const
QStringIterator(const QChar *begin, const QChar *end)
char32_t peekNextUnchecked() const
bool hasPrevious() const
void setPosition(QString::const_iterator position)
char32_t peekPrevious(char32_t invalidAs=QChar::ReplacementCharacter) const
char32_t previousUnchecked()
The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QStrin...
Definition: qstringview.h:122
double e
#define Q_UNLIKELY(x)
#define Q_LIKELY(x)
#define Q_FUNC_INFO
ptrdiff_t qsizetype
Definition: qglobal.h:308
GLuint GLuint end
GLsizei const GLchar *const * string
[0]
Definition: qopenglext.h:694
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
#define Q_ASSERT_X(cond, x, msg)
Definition: qrandom.cpp:85
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)