Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qtextboundaryfinder.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | #include <QtCore/qtextboundaryfinder.h> | - | ||||||||||||||||||
34 | #include <QtCore/qvarlengtharray.h> | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #include <private/qunicodetools_p.h> | - | ||||||||||||||||||
37 | - | |||||||||||||||||||
38 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | class QTextBoundaryFinderPrivate | - | ||||||||||||||||||
41 | { | - | ||||||||||||||||||
42 | public: | - | ||||||||||||||||||
43 | QCharAttributes attributes[1]; | - | ||||||||||||||||||
44 | }; | - | ||||||||||||||||||
45 | - | |||||||||||||||||||
46 | static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int length, QCharAttributes *attributes) | - | ||||||||||||||||||
47 | { | - | ||||||||||||||||||
48 | const ushort *string = reinterpret_cast<const ushort *>(chars); | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | QVarLengthArray<QUnicodeTools::ScriptItem> scriptItems; | - | ||||||||||||||||||
51 | { | - | ||||||||||||||||||
52 | QVarLengthArray<uchar> scripts(length); | - | ||||||||||||||||||
53 | - | |||||||||||||||||||
54 | QUnicodeTools::initScripts(string, length, scripts.data()); | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | int start = 0; | - | ||||||||||||||||||
57 | for (int i = start + 1; i <= length; ++i) {
| 8846-160347 | ||||||||||||||||||
58 | if (i == length || scripts[i] != scripts[start]) {
| 6261-151501 | ||||||||||||||||||
59 | QUnicodeTools::ScriptItem item; | - | ||||||||||||||||||
60 | item.position = start; | - | ||||||||||||||||||
61 | item.script = scripts[start]; | - | ||||||||||||||||||
62 | scriptItems.append(item); | - | ||||||||||||||||||
63 | start = i; | - | ||||||||||||||||||
64 | } executed 15107 times by 3 tests: end of block Executed by:
| 15107 | ||||||||||||||||||
65 | } executed 160347 times by 3 tests: end of block Executed by:
| 160347 | ||||||||||||||||||
66 | } | - | ||||||||||||||||||
67 | - | |||||||||||||||||||
68 | QUnicodeTools::CharAttributeOptions options = 0; | - | ||||||||||||||||||
69 | switch (type) { | - | ||||||||||||||||||
70 | case QTextBoundaryFinder::Grapheme: options |= QUnicodeTools::GraphemeBreaks; break; executed 434 times by 2 tests: break; Executed by:
executed 434 times by 2 tests: case QTextBoundaryFinder::Grapheme: Executed by:
| 434 | ||||||||||||||||||
71 | case QTextBoundaryFinder::Word: options |= QUnicodeTools::WordBreaks; break; executed 1553 times by 3 tests: break; Executed by:
executed 1553 times by 3 tests: case QTextBoundaryFinder::Word: Executed by:
| 1553 | ||||||||||||||||||
72 | case QTextBoundaryFinder::Sentence: options |= QUnicodeTools::SentenceBreaks; break; executed 521 times by 2 tests: break; Executed by:
executed 521 times by 2 tests: case QTextBoundaryFinder::Sentence: Executed by:
| 521 | ||||||||||||||||||
73 | case QTextBoundaryFinder::Line: options |= QUnicodeTools::LineBreaks; break; executed 6338 times by 1 test: break; Executed by:
executed 6338 times by 1 test: case QTextBoundaryFinder::Line: Executed by:
| 6338 | ||||||||||||||||||
74 | default: break; never executed: break; never executed: default: | 0 | ||||||||||||||||||
75 | } | - | ||||||||||||||||||
76 | QUnicodeTools::initCharAttributes(string, length, scriptItems.data(), scriptItems.count(), attributes, options); | - | ||||||||||||||||||
77 | } executed 8846 times by 3 tests: end of block Executed by:
| 8846 | ||||||||||||||||||
78 | - | |||||||||||||||||||
79 | /*! | - | ||||||||||||||||||
80 | \class QTextBoundaryFinder | - | ||||||||||||||||||
81 | \inmodule QtCore | - | ||||||||||||||||||
82 | - | |||||||||||||||||||
83 | \brief The QTextBoundaryFinder class provides a way of finding Unicode text boundaries in a string. | - | ||||||||||||||||||
84 | - | |||||||||||||||||||
85 | \since 4.4 | - | ||||||||||||||||||
86 | \ingroup tools | - | ||||||||||||||||||
87 | \ingroup shared | - | ||||||||||||||||||
88 | \ingroup string-processing | - | ||||||||||||||||||
89 | \reentrant | - | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | QTextBoundaryFinder allows to find Unicode text boundaries in a | - | ||||||||||||||||||
92 | string, accordingly to the Unicode text boundary specification (see | - | ||||||||||||||||||
93 | \l{http://www.unicode.org/reports/tr14/}{Unicode Standard Annex #14} and | - | ||||||||||||||||||
94 | \l{http://www.unicode.org/reports/tr29/}{Unicode Standard Annex #29}). | - | ||||||||||||||||||
95 | - | |||||||||||||||||||
96 | QTextBoundaryFinder can operate on a QString in four possible | - | ||||||||||||||||||
97 | modes depending on the value of \a BoundaryType. | - | ||||||||||||||||||
98 | - | |||||||||||||||||||
99 | Units of Unicode characters that make up what the user thinks of | - | ||||||||||||||||||
100 | as a character or basic unit of the language are here called | - | ||||||||||||||||||
101 | Grapheme clusters. The two unicode characters 'A' + diaeresis do | - | ||||||||||||||||||
102 | for example form one grapheme cluster as the user thinks of them | - | ||||||||||||||||||
103 | as one character, yet it is in this case represented by two | - | ||||||||||||||||||
104 | unicode code points | - | ||||||||||||||||||
105 | (see \l{http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries}). | - | ||||||||||||||||||
106 | - | |||||||||||||||||||
107 | Word boundaries are there to locate the start and end of what a | - | ||||||||||||||||||
108 | language considers to be a word | - | ||||||||||||||||||
109 | (see \l{http://www.unicode.org/reports/tr29/#Word_Boundaries}). | - | ||||||||||||||||||
110 | - | |||||||||||||||||||
111 | Line break boundaries give possible places where a line break | - | ||||||||||||||||||
112 | might happen and sentence boundaries will show the beginning and | - | ||||||||||||||||||
113 | end of whole sentences | - | ||||||||||||||||||
114 | (see \l{http://www.unicode.org/reports/tr29/#Sentence_Boundaries} and | - | ||||||||||||||||||
115 | \l{http://www.unicode.org/reports/tr14/}). | - | ||||||||||||||||||
116 | - | |||||||||||||||||||
117 | The first position in a string is always a valid boundary and | - | ||||||||||||||||||
118 | refers to the position before the first character. The last | - | ||||||||||||||||||
119 | position at the length of the string is also valid and refers | - | ||||||||||||||||||
120 | to the position after the last character. | - | ||||||||||||||||||
121 | */ | - | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | /*! | - | ||||||||||||||||||
124 | \enum QTextBoundaryFinder::BoundaryType | - | ||||||||||||||||||
125 | - | |||||||||||||||||||
126 | \value Grapheme Finds a grapheme which is the smallest boundary. It | - | ||||||||||||||||||
127 | including letters, punctuation marks, numerals and more. | - | ||||||||||||||||||
128 | \value Word Finds a word. | - | ||||||||||||||||||
129 | \value Line Finds possible positions for breaking the text into multiple | - | ||||||||||||||||||
130 | lines. | - | ||||||||||||||||||
131 | \value Sentence Finds sentence boundaries. These include periods, question | - | ||||||||||||||||||
132 | marks etc. | - | ||||||||||||||||||
133 | */ | - | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | /*! | - | ||||||||||||||||||
136 | \enum QTextBoundaryFinder::BoundaryReason | - | ||||||||||||||||||
137 | - | |||||||||||||||||||
138 | \value NotAtBoundary The boundary finder is not at a boundary position. | - | ||||||||||||||||||
139 | \value BreakOpportunity The boundary finder is at a break opportunity position. | - | ||||||||||||||||||
140 | Such a break opportunity might also be an item boundary | - | ||||||||||||||||||
141 | (either StartOfItem, EndOfItem, or combination of both), | - | ||||||||||||||||||
142 | a mandatory line break, or a soft hyphen. | - | ||||||||||||||||||
143 | \value StartOfItem Since 5.0. The boundary finder is at the start of | - | ||||||||||||||||||
144 | a grapheme, a word, a sentence, or a line. | - | ||||||||||||||||||
145 | \value EndOfItem Since 5.0. The boundary finder is at the end of | - | ||||||||||||||||||
146 | a grapheme, a word, a sentence, or a line. | - | ||||||||||||||||||
147 | \value MandatoryBreak Since 5.0. The boundary finder is at the end of line | - | ||||||||||||||||||
148 | (can occur for a Line boundary type only). | - | ||||||||||||||||||
149 | \value SoftHyphen The boundary finder is at the soft hyphen | - | ||||||||||||||||||
150 | (can occur for a Line boundary type only). | - | ||||||||||||||||||
151 | */ | - | ||||||||||||||||||
152 | - | |||||||||||||||||||
153 | /*! | - | ||||||||||||||||||
154 | Constructs an invalid QTextBoundaryFinder object. | - | ||||||||||||||||||
155 | */ | - | ||||||||||||||||||
156 | QTextBoundaryFinder::QTextBoundaryFinder() | - | ||||||||||||||||||
157 | : t(Grapheme) | - | ||||||||||||||||||
158 | , chars(0) | - | ||||||||||||||||||
159 | , length(0) | - | ||||||||||||||||||
160 | , freePrivate(true) | - | ||||||||||||||||||
161 | , d(0) | - | ||||||||||||||||||
162 | { | - | ||||||||||||||||||
163 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
164 | - | |||||||||||||||||||
165 | /*! | - | ||||||||||||||||||
166 | Copies the QTextBoundaryFinder object, \a other. | - | ||||||||||||||||||
167 | */ | - | ||||||||||||||||||
168 | QTextBoundaryFinder::QTextBoundaryFinder(const QTextBoundaryFinder &other) | - | ||||||||||||||||||
169 | : t(other.t) | - | ||||||||||||||||||
170 | , s(other.s) | - | ||||||||||||||||||
171 | , chars(other.chars) | - | ||||||||||||||||||
172 | , length(other.length) | - | ||||||||||||||||||
173 | , pos(other.pos) | - | ||||||||||||||||||
174 | , freePrivate(true) | - | ||||||||||||||||||
175 | , d(0) | - | ||||||||||||||||||
176 | { | - | ||||||||||||||||||
177 | if (other.d) {
| 0-15 | ||||||||||||||||||
178 | Q_ASSERT(length > 0); | - | ||||||||||||||||||
179 | d = (QTextBoundaryFinderPrivate *) malloc((length + 1) * sizeof(QCharAttributes)); | - | ||||||||||||||||||
180 | Q_CHECK_PTR(d); never executed: qBadAlloc();
| 0 | ||||||||||||||||||
181 | memcpy(d, other.d, (length + 1) * sizeof(QCharAttributes)); | - | ||||||||||||||||||
182 | } never executed: end of block | 0 | ||||||||||||||||||
183 | } executed 15 times by 1 test: end of block Executed by:
| 15 | ||||||||||||||||||
184 | - | |||||||||||||||||||
185 | /*! | - | ||||||||||||||||||
186 | Assigns the object, \a other, to another QTextBoundaryFinder object. | - | ||||||||||||||||||
187 | */ | - | ||||||||||||||||||
188 | QTextBoundaryFinder &QTextBoundaryFinder::operator=(const QTextBoundaryFinder &other) | - | ||||||||||||||||||
189 | { | - | ||||||||||||||||||
190 | if (&other == this)
| 0-4 | ||||||||||||||||||
191 | return *this; never executed: return *this; | 0 | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | if (other.d) {
| 2 | ||||||||||||||||||
194 | Q_ASSERT(other.length > 0); | - | ||||||||||||||||||
195 | uint newCapacity = (other.length + 1) * sizeof(QCharAttributes); | - | ||||||||||||||||||
196 | QTextBoundaryFinderPrivate *newD = (QTextBoundaryFinderPrivate *) realloc(freePrivate ? d : 0, newCapacity); | - | ||||||||||||||||||
197 | Q_CHECK_PTR(newD); never executed: qBadAlloc();
| 0-2 | ||||||||||||||||||
198 | freePrivate = true; | - | ||||||||||||||||||
199 | d = newD; | - | ||||||||||||||||||
200 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
201 | - | |||||||||||||||||||
202 | t = other.t; | - | ||||||||||||||||||
203 | s = other.s; | - | ||||||||||||||||||
204 | chars = other.chars; | - | ||||||||||||||||||
205 | length = other.length; | - | ||||||||||||||||||
206 | pos = other.pos; | - | ||||||||||||||||||
207 | - | |||||||||||||||||||
208 | if (other.d) {
| 2 | ||||||||||||||||||
209 | memcpy(d, other.d, (length + 1) * sizeof(QCharAttributes)); | - | ||||||||||||||||||
210 | } else { executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
211 | if (freePrivate)
| 0-2 | ||||||||||||||||||
212 | free(d); executed 2 times by 1 test: free(d); Executed by:
| 2 | ||||||||||||||||||
213 | d = 0; | - | ||||||||||||||||||
214 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
215 | - | |||||||||||||||||||
216 | return *this; executed 4 times by 1 test: return *this; Executed by:
| 4 | ||||||||||||||||||
217 | } | - | ||||||||||||||||||
218 | - | |||||||||||||||||||
219 | /*! | - | ||||||||||||||||||
220 | Destructs the QTextBoundaryFinder object. | - | ||||||||||||||||||
221 | */ | - | ||||||||||||||||||
222 | QTextBoundaryFinder::~QTextBoundaryFinder() | - | ||||||||||||||||||
223 | { | - | ||||||||||||||||||
224 | Q_UNUSED(unused); | - | ||||||||||||||||||
225 | if (freePrivate)
| 0-8867 | ||||||||||||||||||
226 | free(d); executed 8867 times by 3 tests: free(d); Executed by:
| 8867 | ||||||||||||||||||
227 | } executed 8867 times by 3 tests: end of block Executed by:
| 8867 | ||||||||||||||||||
228 | - | |||||||||||||||||||
229 | /*! | - | ||||||||||||||||||
230 | Creates a QTextBoundaryFinder object of \a type operating on \a string. | - | ||||||||||||||||||
231 | */ | - | ||||||||||||||||||
232 | QTextBoundaryFinder::QTextBoundaryFinder(BoundaryType type, const QString &string) | - | ||||||||||||||||||
233 | : t(type) | - | ||||||||||||||||||
234 | , s(string) | - | ||||||||||||||||||
235 | , chars(string.unicode()) | - | ||||||||||||||||||
236 | , length(string.length()) | - | ||||||||||||||||||
237 | , pos(0) | - | ||||||||||||||||||
238 | , freePrivate(true) | - | ||||||||||||||||||
239 | , d(0) | - | ||||||||||||||||||
240 | { | - | ||||||||||||||||||
241 | if (length > 0) {
| 1-8843 | ||||||||||||||||||
242 | d = (QTextBoundaryFinderPrivate *) malloc((length + 1) * sizeof(QCharAttributes)); | - | ||||||||||||||||||
243 | Q_CHECK_PTR(d); never executed: qBadAlloc();
| 0-8843 | ||||||||||||||||||
244 | init(t, chars, length, d->attributes); | - | ||||||||||||||||||
245 | } executed 8843 times by 2 tests: end of block Executed by:
| 8843 | ||||||||||||||||||
246 | } executed 8844 times by 2 tests: end of block Executed by:
| 8844 | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | /*! | - | ||||||||||||||||||
249 | Creates a QTextBoundaryFinder object of \a type operating on \a chars | - | ||||||||||||||||||
250 | with \a length. | - | ||||||||||||||||||
251 | - | |||||||||||||||||||
252 | \a buffer is an optional working buffer of size \a bufferSize you can pass to | - | ||||||||||||||||||
253 | the QTextBoundaryFinder. If the buffer is large enough to hold the working | - | ||||||||||||||||||
254 | data required (bufferSize >= length + 1), it will use this | - | ||||||||||||||||||
255 | instead of allocating its own buffer. | - | ||||||||||||||||||
256 | - | |||||||||||||||||||
257 | \warning QTextBoundaryFinder does not create a copy of \a chars. It is the | - | ||||||||||||||||||
258 | application programmer's responsibility to ensure the array is allocated for | - | ||||||||||||||||||
259 | as long as the QTextBoundaryFinder object stays alive. The same applies to | - | ||||||||||||||||||
260 | \a buffer. | - | ||||||||||||||||||
261 | */ | - | ||||||||||||||||||
262 | QTextBoundaryFinder::QTextBoundaryFinder(BoundaryType type, const QChar *chars, int length, unsigned char *buffer, int bufferSize) | - | ||||||||||||||||||
263 | : t(type) | - | ||||||||||||||||||
264 | , chars(chars) | - | ||||||||||||||||||
265 | , length(length) | - | ||||||||||||||||||
266 | , pos(0) | - | ||||||||||||||||||
267 | , freePrivate(true) | - | ||||||||||||||||||
268 | , d(0) | - | ||||||||||||||||||
269 | { | - | ||||||||||||||||||
270 | if (!chars) {
| 2-5 | ||||||||||||||||||
271 | length = 0; | - | ||||||||||||||||||
272 | } else if (length > 0) { executed 2 times by 1 test: end of block Executed by:
| 2-3 | ||||||||||||||||||
273 | if (buffer && (uint)bufferSize >= (length + 1) * sizeof(QCharAttributes)) {
| 0-3 | ||||||||||||||||||
274 | d = (QTextBoundaryFinderPrivate *)buffer; | - | ||||||||||||||||||
275 | freePrivate = false; | - | ||||||||||||||||||
276 | } else { never executed: end of block | 0 | ||||||||||||||||||
277 | d = (QTextBoundaryFinderPrivate *) malloc((length + 1) * sizeof(QCharAttributes)); | - | ||||||||||||||||||
278 | Q_CHECK_PTR(d); never executed: qBadAlloc();
| 0-3 | ||||||||||||||||||
279 | } executed 3 times by 2 tests: end of block Executed by:
| 3 | ||||||||||||||||||
280 | init(t, chars, length, d->attributes); | - | ||||||||||||||||||
281 | } executed 3 times by 2 tests: end of block Executed by:
| 3 | ||||||||||||||||||
282 | } executed 7 times by 2 tests: end of block Executed by:
| 7 | ||||||||||||||||||
283 | - | |||||||||||||||||||
284 | /*! | - | ||||||||||||||||||
285 | Moves the finder to the start of the string. This is equivalent to setPosition(0). | - | ||||||||||||||||||
286 | - | |||||||||||||||||||
287 | \sa setPosition(), position() | - | ||||||||||||||||||
288 | */ | - | ||||||||||||||||||
289 | void QTextBoundaryFinder::toStart() | - | ||||||||||||||||||
290 | { | - | ||||||||||||||||||
291 | pos = 0; | - | ||||||||||||||||||
292 | } never executed: end of block | 0 | ||||||||||||||||||
293 | - | |||||||||||||||||||
294 | /*! | - | ||||||||||||||||||
295 | Moves the finder to the end of the string. This is equivalent to setPosition(string.length()). | - | ||||||||||||||||||
296 | - | |||||||||||||||||||
297 | \sa setPosition(), position() | - | ||||||||||||||||||
298 | */ | - | ||||||||||||||||||
299 | void QTextBoundaryFinder::toEnd() | - | ||||||||||||||||||
300 | { | - | ||||||||||||||||||
301 | pos = length; | - | ||||||||||||||||||
302 | } executed 8797 times by 1 test: end of block Executed by:
| 8797 | ||||||||||||||||||
303 | - | |||||||||||||||||||
304 | /*! | - | ||||||||||||||||||
305 | Returns the current position of the QTextBoundaryFinder. | - | ||||||||||||||||||
306 | - | |||||||||||||||||||
307 | The range is from 0 (the beginning of the string) to the length of | - | ||||||||||||||||||
308 | the string inclusive. | - | ||||||||||||||||||
309 | - | |||||||||||||||||||
310 | \sa setPosition() | - | ||||||||||||||||||
311 | */ | - | ||||||||||||||||||
312 | int QTextBoundaryFinder::position() const | - | ||||||||||||||||||
313 | { | - | ||||||||||||||||||
314 | return pos; executed 195130 times by 3 tests: return pos; Executed by:
| 195130 | ||||||||||||||||||
315 | } | - | ||||||||||||||||||
316 | - | |||||||||||||||||||
317 | /*! | - | ||||||||||||||||||
318 | Sets the current position of the QTextBoundaryFinder to \a position. | - | ||||||||||||||||||
319 | - | |||||||||||||||||||
320 | If \a position is out of bounds, it will be bound to only valid | - | ||||||||||||||||||
321 | positions. In this case, valid positions are from 0 to the length of | - | ||||||||||||||||||
322 | the string inclusive. | - | ||||||||||||||||||
323 | - | |||||||||||||||||||
324 | \sa position() | - | ||||||||||||||||||
325 | */ | - | ||||||||||||||||||
326 | void QTextBoundaryFinder::setPosition(int position) | - | ||||||||||||||||||
327 | { | - | ||||||||||||||||||
328 | pos = qBound(0, position, length); | - | ||||||||||||||||||
329 | } executed 39074 times by 3 tests: end of block Executed by:
| 39074 | ||||||||||||||||||
330 | - | |||||||||||||||||||
331 | /*! \fn QTextBoundaryFinder::BoundaryType QTextBoundaryFinder::type() const | - | ||||||||||||||||||
332 | - | |||||||||||||||||||
333 | Returns the type of the QTextBoundaryFinder. | - | ||||||||||||||||||
334 | */ | - | ||||||||||||||||||
335 | - | |||||||||||||||||||
336 | /*! \fn bool QTextBoundaryFinder::isValid() const | - | ||||||||||||||||||
337 | - | |||||||||||||||||||
338 | Returns \c true if the text boundary finder is valid; otherwise returns \c false. | - | ||||||||||||||||||
339 | A default QTextBoundaryFinder is invalid. | - | ||||||||||||||||||
340 | */ | - | ||||||||||||||||||
341 | - | |||||||||||||||||||
342 | /*! | - | ||||||||||||||||||
343 | Returns the string the QTextBoundaryFinder object operates on. | - | ||||||||||||||||||
344 | */ | - | ||||||||||||||||||
345 | QString QTextBoundaryFinder::string() const | - | ||||||||||||||||||
346 | { | - | ||||||||||||||||||
347 | if (chars == s.unicode() && length == s.length())
| 0-2 | ||||||||||||||||||
348 | return s; executed 2 times by 1 test: return s; Executed by:
| 2 | ||||||||||||||||||
349 | return QString(chars, length); executed 2 times by 1 test: return QString(chars, length); Executed by:
| 2 | ||||||||||||||||||
350 | } | - | ||||||||||||||||||
351 | - | |||||||||||||||||||
352 | - | |||||||||||||||||||
353 | /*! | - | ||||||||||||||||||
354 | Moves the QTextBoundaryFinder to the next boundary position and returns that position. | - | ||||||||||||||||||
355 | - | |||||||||||||||||||
356 | Returns -1 if there is no next boundary. | - | ||||||||||||||||||
357 | */ | - | ||||||||||||||||||
358 | int QTextBoundaryFinder::toNextBoundary() | - | ||||||||||||||||||
359 | { | - | ||||||||||||||||||
360 | if (!d || pos < 0 || pos >= length) {
| 0-24931 | ||||||||||||||||||
361 | pos = -1; | - | ||||||||||||||||||
362 | return pos; executed 8807 times by 2 tests: return pos; Executed by:
| 8807 | ||||||||||||||||||
363 | } | - | ||||||||||||||||||
364 | - | |||||||||||||||||||
365 | ++pos; | - | ||||||||||||||||||
366 | switch(t) { | - | ||||||||||||||||||
367 | case Grapheme: executed 826 times by 2 tests: case Grapheme: Executed by:
| 826 | ||||||||||||||||||
368 | while (pos < length && !d->attributes[pos].graphemeBoundary)
| 295-713 | ||||||||||||||||||
369 | ++pos; executed 295 times by 1 test: ++pos; Executed by:
| 295 | ||||||||||||||||||
370 | break; executed 826 times by 2 tests: break; Executed by:
| 826 | ||||||||||||||||||
371 | case Word: executed 4027 times by 3 tests: case Word: Executed by:
| 4027 | ||||||||||||||||||
372 | while (pos < length && !d->attributes[pos].wordBreak)
| 1543-132701 | ||||||||||||||||||
373 | ++pos; executed 130217 times by 3 tests: ++pos; Executed by:
| 130217 | ||||||||||||||||||
374 | break; executed 4027 times by 3 tests: break; Executed by:
| 4027 | ||||||||||||||||||
375 | case Sentence: executed 663 times by 2 tests: case Sentence: Executed by:
| 663 | ||||||||||||||||||
376 | while (pos < length && !d->attributes[pos].sentenceBoundary)
| 145-1585 | ||||||||||||||||||
377 | ++pos; executed 1440 times by 2 tests: ++pos; Executed by:
| 1440 | ||||||||||||||||||
378 | break; executed 663 times by 2 tests: break; Executed by:
| 663 | ||||||||||||||||||
379 | case Line: executed 10615 times by 1 test: case Line: Executed by:
| 10615 | ||||||||||||||||||
380 | while (pos < length && !d->attributes[pos].lineBreak)
| 4278-14608 | ||||||||||||||||||
381 | ++pos; executed 10330 times by 1 test: ++pos; Executed by:
| 10330 | ||||||||||||||||||
382 | break; executed 10615 times by 1 test: break; Executed by:
| 10615 | ||||||||||||||||||
383 | } | - | ||||||||||||||||||
384 | - | |||||||||||||||||||
385 | return pos; executed 16131 times by 3 tests: return pos; Executed by:
| 16131 | ||||||||||||||||||
386 | } | - | ||||||||||||||||||
387 | - | |||||||||||||||||||
388 | /*! | - | ||||||||||||||||||
389 | Moves the QTextBoundaryFinder to the previous boundary position and returns that position. | - | ||||||||||||||||||
390 | - | |||||||||||||||||||
391 | Returns -1 if there is no previous boundary. | - | ||||||||||||||||||
392 | */ | - | ||||||||||||||||||
393 | int QTextBoundaryFinder::toPreviousBoundary() | - | ||||||||||||||||||
394 | { | - | ||||||||||||||||||
395 | if (!d || pos <= 0 || pos > length) {
| 0-24899 | ||||||||||||||||||
396 | pos = -1; | - | ||||||||||||||||||
397 | return pos; executed 8797 times by 1 test: return pos; Executed by:
| 8797 | ||||||||||||||||||
398 | } | - | ||||||||||||||||||
399 | - | |||||||||||||||||||
400 | --pos; | - | ||||||||||||||||||
401 | switch(t) { | - | ||||||||||||||||||
402 | case Grapheme: executed 809 times by 2 tests: case Grapheme: Executed by:
| 809 | ||||||||||||||||||
403 | while (pos > 0 && !d->attributes[pos].graphemeBoundary)
| 295-700 | ||||||||||||||||||
404 | --pos; executed 295 times by 1 test: --pos; Executed by:
| 295 | ||||||||||||||||||
405 | break; executed 809 times by 2 tests: break; Executed by:
| 809 | ||||||||||||||||||
406 | case Word: executed 4015 times by 2 tests: case Word: Executed by:
| 4015 | ||||||||||||||||||
407 | while (pos > 0 && !d->attributes[pos].wordBreak)
| 1540-4659 | ||||||||||||||||||
408 | --pos; executed 2184 times by 2 tests: --pos; Executed by:
| 2184 | ||||||||||||||||||
409 | break; executed 4015 times by 2 tests: break; Executed by:
| 4015 | ||||||||||||||||||
410 | case Sentence: executed 663 times by 2 tests: case Sentence: Executed by:
| 663 | ||||||||||||||||||
411 | while (pos > 0 && !d->attributes[pos].sentenceBoundary)
| 143-1537 | ||||||||||||||||||
412 | --pos; executed 1394 times by 2 tests: --pos; Executed by:
| 1394 | ||||||||||||||||||
413 | break; executed 663 times by 2 tests: break; Executed by:
| 663 | ||||||||||||||||||
414 | case Line: executed 10615 times by 1 test: case Line: Executed by:
| 10615 | ||||||||||||||||||
415 | while (pos > 0 && !d->attributes[pos].lineBreak)
| 4278-14608 | ||||||||||||||||||
416 | --pos; executed 10330 times by 1 test: --pos; Executed by:
| 10330 | ||||||||||||||||||
417 | break; executed 10615 times by 1 test: break; Executed by:
| 10615 | ||||||||||||||||||
418 | } | - | ||||||||||||||||||
419 | - | |||||||||||||||||||
420 | return pos; executed 16102 times by 2 tests: return pos; Executed by:
| 16102 | ||||||||||||||||||
421 | } | - | ||||||||||||||||||
422 | - | |||||||||||||||||||
423 | /*! | - | ||||||||||||||||||
424 | Returns \c true if the object's position() is currently at a valid text boundary. | - | ||||||||||||||||||
425 | */ | - | ||||||||||||||||||
426 | bool QTextBoundaryFinder::isAtBoundary() const | - | ||||||||||||||||||
427 | { | - | ||||||||||||||||||
428 | if (!d || pos < 0 || pos > length)
| 0-67350 | ||||||||||||||||||
429 | return false; executed 17594 times by 1 test: return false; Executed by:
| 17594 | ||||||||||||||||||
430 | - | |||||||||||||||||||
431 | switch(t) { | - | ||||||||||||||||||
432 | case Grapheme: executed 2400 times by 1 test: case Grapheme: Executed by:
| 2400 | ||||||||||||||||||
433 | return d->attributes[pos].graphemeBoundary; executed 2400 times by 1 test: return d->attributes[pos].graphemeBoundary; Executed by:
| 2400 | ||||||||||||||||||
434 | case Word: executed 11096 times by 1 test: case Word: Executed by:
| 11096 | ||||||||||||||||||
435 | return d->attributes[pos].wordBreak; executed 11096 times by 1 test: return d->attributes[pos].wordBreak; Executed by:
| 11096 | ||||||||||||||||||
436 | case Sentence: executed 2356 times by 1 test: case Sentence: Executed by:
| 2356 | ||||||||||||||||||
437 | return d->attributes[pos].sentenceBoundary; executed 2356 times by 1 test: return d->attributes[pos].sentenceBoundary; Executed by:
| 2356 | ||||||||||||||||||
438 | case Line: executed 33904 times by 1 test: case Line: Executed by:
| 33904 | ||||||||||||||||||
439 | // ### TR#14 LB2 prohibits break at sot | - | ||||||||||||||||||
440 | return d->attributes[pos].lineBreak || pos == 0; executed 33904 times by 1 test: return d->attributes[pos].lineBreak || pos == 0; Executed by:
| 0-33904 | ||||||||||||||||||
441 | } | - | ||||||||||||||||||
442 | return false; never executed: return false; | 0 | ||||||||||||||||||
443 | } | - | ||||||||||||||||||
444 | - | |||||||||||||||||||
445 | /*! | - | ||||||||||||||||||
446 | Returns the reasons for the boundary finder to have chosen the current position as a boundary. | - | ||||||||||||||||||
447 | */ | - | ||||||||||||||||||
448 | QTextBoundaryFinder::BoundaryReasons QTextBoundaryFinder::boundaryReasons() const | - | ||||||||||||||||||
449 | { | - | ||||||||||||||||||
450 | BoundaryReasons reasons = NotAtBoundary; | - | ||||||||||||||||||
451 | if (!d || pos < 0 || pos > length)
| 0-106479 | ||||||||||||||||||
452 | return reasons; executed 17611 times by 2 tests: return reasons; Executed by:
| 17611 | ||||||||||||||||||
453 | - | |||||||||||||||||||
454 | const QCharAttributes attr = d->attributes[pos]; | - | ||||||||||||||||||
455 | switch (t) { | - | ||||||||||||||||||
456 | case Grapheme: executed 3951 times by 2 tests: case Grapheme: Executed by:
| 3951 | ||||||||||||||||||
457 | if (attr.graphemeBoundary) {
| 295-3656 | ||||||||||||||||||
458 | reasons |= BreakOpportunity | StartOfItem | EndOfItem; | - | ||||||||||||||||||
459 | if (pos == 0)
| 1209-2447 | ||||||||||||||||||
460 | reasons &= (~EndOfItem); executed 1209 times by 2 tests: reasons &= (~EndOfItem); Executed by:
| 1209 | ||||||||||||||||||
461 | else if (pos == length)
| 1214-1233 | ||||||||||||||||||
462 | reasons &= (~StartOfItem); executed 1214 times by 2 tests: reasons &= (~StartOfItem); Executed by:
| 1214 | ||||||||||||||||||
463 | } executed 3656 times by 2 tests: end of block Executed by:
| 3656 | ||||||||||||||||||
464 | break; executed 3951 times by 2 tests: break; Executed by:
| 3951 | ||||||||||||||||||
465 | case Word: executed 18853 times by 3 tests: case Word: Executed by:
| 18853 | ||||||||||||||||||
466 | if (attr.wordBreak) {
| 2178-16675 | ||||||||||||||||||
467 | reasons |= BreakOpportunity; | - | ||||||||||||||||||
468 | if (attr.wordStart)
| 4571-12104 | ||||||||||||||||||
469 | reasons |= StartOfItem; executed 4571 times by 3 tests: reasons |= StartOfItem; Executed by:
| 4571 | ||||||||||||||||||
470 | if (attr.wordEnd)
| 4569-12106 | ||||||||||||||||||
471 | reasons |= EndOfItem; executed 4569 times by 3 tests: reasons |= EndOfItem; Executed by:
| 4569 | ||||||||||||||||||
472 | } executed 16675 times by 3 tests: end of block Executed by:
| 16675 | ||||||||||||||||||
473 | break; executed 18853 times by 3 tests: break; Executed by:
| 18853 | ||||||||||||||||||
474 | case Sentence: executed 4892 times by 2 tests: case Sentence: Executed by:
| 4892 | ||||||||||||||||||
475 | if (attr.sentenceBoundary) {
| 1354-3538 | ||||||||||||||||||
476 | reasons |= BreakOpportunity | StartOfItem | EndOfItem; | - | ||||||||||||||||||
477 | if (pos == 0)
| 1554-1984 | ||||||||||||||||||
478 | reasons &= (~EndOfItem); executed 1554 times by 1 test: reasons &= (~EndOfItem); Executed by:
| 1554 | ||||||||||||||||||
479 | else if (pos == length)
| 430-1554 | ||||||||||||||||||
480 | reasons &= (~StartOfItem); executed 1554 times by 1 test: reasons &= (~StartOfItem); Executed by:
| 1554 | ||||||||||||||||||
481 | } executed 3538 times by 2 tests: end of block Executed by:
| 3538 | ||||||||||||||||||
482 | break; executed 4892 times by 2 tests: break; Executed by:
| 4892 | ||||||||||||||||||
483 | case Line: executed 61186 times by 1 test: case Line: Executed by:
| 61186 | ||||||||||||||||||
484 | // ### TR#14 LB2 prohibits break at sot | - | ||||||||||||||||||
485 | if (attr.lineBreak || pos == 0) {
| 10330-31845 | ||||||||||||||||||
486 | reasons |= BreakOpportunity; | - | ||||||||||||||||||
487 | if (attr.mandatoryBreak || pos == 0) {
| 10893-29904 | ||||||||||||||||||
488 | reasons |= MandatoryBreak | StartOfItem | EndOfItem; | - | ||||||||||||||||||
489 | if (pos == 0)
| 19011-20952 | ||||||||||||||||||
490 | reasons &= (~EndOfItem); executed 19011 times by 1 test: reasons &= (~EndOfItem); Executed by:
| 19011 | ||||||||||||||||||
491 | else if (pos == length)
| 1941-19011 | ||||||||||||||||||
492 | reasons &= (~StartOfItem); executed 19011 times by 1 test: reasons &= (~StartOfItem); Executed by:
| 19011 | ||||||||||||||||||
493 | } else if (pos > 0 && chars[pos - 1].unicode() == QChar::SoftHyphen) { executed 39963 times by 1 test: end of block Executed by:
| 0-39963 | ||||||||||||||||||
494 | reasons |= SoftHyphen; | - | ||||||||||||||||||
495 | } executed 45 times by 1 test: end of block Executed by:
| 45 | ||||||||||||||||||
496 | } executed 50856 times by 1 test: end of block Executed by:
| 50856 | ||||||||||||||||||
497 | break; executed 61186 times by 1 test: break; Executed by:
| 61186 | ||||||||||||||||||
498 | default: never executed: default: | 0 | ||||||||||||||||||
499 | break; never executed: break; | 0 | ||||||||||||||||||
500 | } | - | ||||||||||||||||||
501 | - | |||||||||||||||||||
502 | return reasons; executed 88882 times by 3 tests: return reasons; Executed by:
| 88882 | ||||||||||||||||||
503 | } | - | ||||||||||||||||||
504 | - | |||||||||||||||||||
505 | QT_END_NAMESPACE | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |