io/qurlquery.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2012 Intel Corporation. -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qurlquery.h" -
43#include "qurl_p.h" -
44 -
45#include <QtCore/qstringlist.h> -
46 -
47QT_BEGIN_NAMESPACE -
48 -
49/*! -
50 \class QUrlQuery -
51 \inmodule QtCore -
52 -
53 \brief The QUrlQuery class provides a way to manipulate a key-value pairs in -
54 a URL's query. -
55 -
56 \reentrant -
57 \ingroup io -
58 \ingroup network -
59 \ingroup shared -
60 -
61 It is used to parse the query strings found in URLs like the following: -
62 -
63 \image qurl-querystring.png -
64 -
65 Query strings like the above are used to transmit options in the URL and are -
66 usually decoded into multiple key-value pairs. The one above would contain -
67 two entries in its list, with keys "type" and "color". QUrlQuery can also be -
68 used to create a query string suitable for use in QUrl::setQuery() from the -
69 individual components of the query. -
70 -
71 The most common way of parsing a query string is to initialize it in the -
72 constructor by passing it the query string. Otherwise, the setQuery() method -
73 can be used to set the query to be parsed. That method can also be used to -
74 parse a query with non-standard delimiters, after having set them using the -
75 setQueryDelimiters() function. -
76 -
77 The encoded query string can be obtained again using query(). This will take -
78 all the internally-stored items and encode the string using the delimiters. -
79 -
80 \section1 Encoding -
81 -
82 All of the getter methods in QUrlQuery support an optional parameter of type -
83 QUrl::ComponentFormattingOptions, including query(), which dictate how to -
84 encode the data in question. Regardless of the mode, the returned value must -
85 still be considered a percent-encoded string, as there are certain values -
86 which cannot be expressed in decoded form (like control characters, byte -
87 sequences not decodable to UTF-8). For that reason, the percent character is -
88 always represented by the string "%25". -
89 -
90 \section2 Handling of spaces and plus ("+") -
91 -
92 Web browsers usually encode spaces found in HTML FORM elements to a plus sign -
93 ("+") and plus signs to its percent-encoded form (%2B). However, the Internet -
94 specifications governing URLs do not consider spaces and the plus character -
95 equivalent. -
96 -
97 For that reason, QUrlQuery never encodes the space character to "+" and will -
98 never decode "+" to a space character. Instead, space characters will be -
99 rendered "%20" in encoded form. -
100 -
101 To support encoding like that of HTML forms, QUrlQuery also never decodes the -
102 "%2B" sequence to a plus sign nor encode a plus sign. In fact, any "%2B" or -
103 "+" sequences found in the keys, values, or query string are left exactly -
104 like written (except for the uppercasing of "%2b" to "%2B"). -
105 -
106 \section1 Non-standard delimiters -
107 -
108 By default, QUrlQuery uses an equal sign ("=") to separate a key from its -
109 value, and an ampersand ("&") to separate key-value pairs from each other. It -
110 is possible to change the delimiters that QUrlQuery uses for parsing and for -
111 reconstructing the query by calling setQueryDelimiters(). -
112 -
113 Non-standard delimiters should be chosen from among what RFC 3986 calls -
114 "sub-delimiters". They are: -
115 -
116 \code -
117 sub-delims = "!" / "$" / "&" / "'" / "(" / ")" -
118 / "*" / "+" / "," / ";" / "=" -
119 \endcode -
120 -
121 Use of other characters is not supported and may result in unexpected -
122 behaviour. QUrlQuery does not verify that you passed a valid delimiter. -
123 -
124 \sa QUrl -
125*/ -
126 -
127typedef QList<QPair<QString, QString> > Map; -
128 -
129class QUrlQueryPrivate : public QSharedData -
130{ -
131public: -
132 QUrlQueryPrivate(const QString &query = QString()) -
133 : valueDelimiter(QUrlQuery::defaultQueryValueDelimiter()), -
134 pairDelimiter(QUrlQuery::defaultQueryPairDelimiter()) -
135 { if (!query.isEmpty()) setQuery(query); }
executed: setQuery(query);
Execution Count:46
executed: }
Execution Count:101
evaluated: !query.isEmpty()
TRUEFALSE
yes
Evaluation Count:46
yes
Evaluation Count:55
46-101
136 -
137 QString recodeFromUser(const QString &input) const; -
138 QString recodeToUser(const QString &input, QUrl::ComponentFormattingOptions encoding) const; -
139 -
140 void setQuery(const QString &query); -
141 -
142 void addQueryItem(const QString &key, const QString &value) -
143 { itemList.append(qMakePair(recodeFromUser(key), recodeFromUser(value))); }
executed: }
Execution Count:104
104
144 int findRecodedKey(const QString &key, int from = 0) const -
145 { -
146 for (int i = from; i < itemList.size(); ++i)
evaluated: i < itemList.size()
TRUEFALSE
yes
Evaluation Count:96
yes
Evaluation Count:17
17-96
147 if (itemList.at(i).first == key)
evaluated: itemList.at(i).first == key
TRUEFALSE
yes
Evaluation Count:52
yes
Evaluation Count:44
44-52
148 return i;
executed: return i;
Execution Count:52
52
149 return itemList.size();
executed: return itemList.size();
Execution Count:17
17
150 } -
151 Map::const_iterator findKey(const QString &key) const -
152 { return itemList.constBegin() + findRecodedKey(recodeFromUser(key)); }
executed: return itemList.constBegin() + findRecodedKey(recodeFromUser(key));
Execution Count:32
32
153 Map::iterator findKey(const QString &key) -
154 { return itemList.begin() + findRecodedKey(recodeFromUser(key)); }
executed: return itemList.begin() + findRecodedKey(recodeFromUser(key));
Execution Count:6
6
155 -
156 // use QMap so we end up sorting the items by key -
157 Map itemList; -
158 QChar valueDelimiter; -
159 QChar pairDelimiter; -
160}; -
161 -
162template<> void QSharedDataPointer<QUrlQueryPrivate>::detach() -
163{ -
164 if (d && d->ref.load() == 1)
evaluated: d
TRUEFALSE
yes
Evaluation Count:111
yes
Evaluation Count:55
evaluated: d->ref.load() == 1
TRUEFALSE
yes
Evaluation Count:107
yes
Evaluation Count:4
4-111
165 return;
executed: return;
Execution Count:107
107
166 QUrlQueryPrivate *x = (d ? new QUrlQueryPrivate(*d)
evaluated: d
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:55
4-55
167 : new QUrlQueryPrivate);
executed (the execution status of this line is deduced): : new QUrlQueryPrivate);
-
168 x->ref.ref();
executed (the execution status of this line is deduced): x->ref.ref();
-
169 if (d && !d->ref.deref())
evaluated: d
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:55
partially evaluated: !d->ref.deref()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4
0-55
170 delete d;
never executed: delete d;
0
171 d = x;
executed (the execution status of this line is deduced): d = x;
-
172}
executed: }
Execution Count:59
59
173 -
174// Here's how we do the encoding in QUrlQuery -
175// The RFC says these are the delimiters: -
176// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" -
177// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" -
178// / "*" / "+" / "," / ";" / "=" -
179// And the definition of query is: -
180// query = *( pchar / "/" / "?" ) -
181// pchar = unreserved / pct-encoded / sub-delims / ":" / "@" -
182// -
183// The strict definition of query says that it can have unencoded any -
184// unreserved, sub-delim, ":", "@", "/" and "?". Or, by exclusion, excluded -
185// delimiters are "#", "[" and "]" -- if those are present, they must be -
186// percent-encoded. The fact that "[" and "]" should be encoded is probably a -
187// mistake in the spec, so we ignore it and leave the decoded. -
188// -
189// The internal storage in the Map is equivalent to PrettyDecoded. That means -
190// the getter methods, when called with the default encoding value, will not -
191// have to recode anything (except for toString()). -
192// -
193// The "+" sub-delimiter is always left untouched. We never encode "+" to "%2B" -
194// nor do we decode "%2B" to "+", no matter what the user asks. -
195// -
196// The rest of the delimiters are kept in their decoded forms and that's -
197// considered non-ambiguous. That includes the pair and value delimiters -
198// themselves. -
199// -
200// But when recreating the query string, in toString(), we must take care of -
201// the special delimiters: the pair and value delimiters, as well as the "#" -
202// character if unambiguous decoding is requested. -
203 -
204#define decode(x) ushort(x) -
205#define leave(x) ushort(0x100 | (x)) -
206#define encode(x) ushort(0x200 | (x)) -
207static const ushort prettyDecodedActions[] = { leave('+'), 0 }; -
208 -
209inline QString QUrlQueryPrivate::recodeFromUser(const QString &input) const -
210{ -
211 // note: duplicated in setQuery() -
212 QString output;
executed (the execution status of this line is deduced): QString output;
-
213 if (qt_urlRecode(output, input.constData(), input.constData() + input.length(),
evaluated: qt_urlRecode(output, input.constData(), input.constData() + input.length(), QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:26
yes
Evaluation Count:235
26-235
214 QUrl::DecodeReserved,
evaluated: qt_urlRecode(output, input.constData(), input.constData() + input.length(), QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:26
yes
Evaluation Count:235
26-235
215 prettyDecodedActions))
evaluated: qt_urlRecode(output, input.constData(), input.constData() + input.length(), QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:26
yes
Evaluation Count:235
26-235
216 return output;
executed: return output;
Execution Count:26
26
217 return input;
executed: return input;
Execution Count:235
235
218} -
219 -
220inline bool idempotentRecodeToUser(QUrl::ComponentFormattingOptions encoding) -
221{ -
222 return encoding == QUrl::PrettyDecoded;
executed: return encoding == QUrl::PrettyDecoded;
Execution Count:146
146
223} -
224 -
225inline QString QUrlQueryPrivate::recodeToUser(const QString &input, QUrl::ComponentFormattingOptions encoding) const -
226{ -
227 // our internal formats are stored in "PrettyDecoded" form -
228 // and there are no ambiguous characters -
229 if (idempotentRecodeToUser(encoding))
evaluated: idempotentRecodeToUser(encoding)
TRUEFALSE
yes
Evaluation Count:35
yes
Evaluation Count:24
24-35
230 return input;
executed: return input;
Execution Count:35
35
231 -
232 if (!(encoding & QUrl::EncodeDelimiters)) {
evaluated: !(encoding & QUrl::EncodeDelimiters)
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:16
8-16
233 QString output;
executed (the execution status of this line is deduced): QString output;
-
234 if (qt_urlRecode(output, input.constData(), input.constData() + input.length(),
evaluated: qt_urlRecode(output, input.constData(), input.constData() + input.length(), encoding, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:4
4
235 encoding, prettyDecodedActions))
evaluated: qt_urlRecode(output, input.constData(), input.constData() + input.length(), encoding, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:4
4
236 return output;
executed: return output;
Execution Count:4
4
237 return input;
executed: return input;
Execution Count:4
4
238 } -
239 -
240 // re-encode the "#" character and the query delimiter pair -
241 ushort actions[] = { encode(pairDelimiter.unicode()), encode(valueDelimiter.unicode()),
executed (the execution status of this line is deduced): ushort actions[] = { ushort(0x200 | (pairDelimiter.unicode())), ushort(0x200 | (valueDelimiter.unicode())),
-
242 encode('#'), 0 };
executed (the execution status of this line is deduced): ushort(0x200 | ('#')), 0 };
-
243 QString output;
executed (the execution status of this line is deduced): QString output;
-
244 if (qt_urlRecode(output, input.constData(), input.constData() + input.length(), encoding, actions))
evaluated: qt_urlRecode(output, input.constData(), input.constData() + input.length(), encoding, actions)
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:10
6-10
245 return output;
executed: return output;
Execution Count:6
6
246 return input;
executed: return input;
Execution Count:10
10
247} -
248 -
249void QUrlQueryPrivate::setQuery(const QString &query) -
250{ -
251 itemList.clear();
executed (the execution status of this line is deduced): itemList.clear();
-
252 const QChar *pos = query.constData();
executed (the execution status of this line is deduced): const QChar *pos = query.constData();
-
253 const QChar *const end = pos + query.size();
executed (the execution status of this line is deduced): const QChar *const end = pos + query.size();
-
254 while (pos != end) {
evaluated: pos != end
TRUEFALSE
yes
Evaluation Count:72
yes
Evaluation Count:49
49-72
255 const QChar *begin = pos;
executed (the execution status of this line is deduced): const QChar *begin = pos;
-
256 const QChar *delimiter = 0;
executed (the execution status of this line is deduced): const QChar *delimiter = 0;
-
257 while (pos != end) {
evaluated: pos != end
TRUEFALSE
yes
Evaluation Count:530
yes
Evaluation Count:48
48-530
258 // scan for the component parts of this pair -
259 if (!delimiter && pos->unicode() == valueDelimiter)
evaluated: !delimiter
TRUEFALSE
yes
Evaluation Count:309
yes
Evaluation Count:221
evaluated: pos->unicode() == valueDelimiter
TRUEFALSE
yes
Evaluation Count:62
yes
Evaluation Count:247
62-309
260 delimiter = pos;
executed: delimiter = pos;
Execution Count:62
62
261 if (pos->unicode() == pairDelimiter)
evaluated: pos->unicode() == pairDelimiter
TRUEFALSE
yes
Evaluation Count:24
yes
Evaluation Count:506
24-506
262 break;
executed: break;
Execution Count:24
24
263 ++pos;
executed (the execution status of this line is deduced): ++pos;
-
264 }
executed: }
Execution Count:506
506
265 if (!delimiter)
evaluated: !delimiter
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:62
10-62
266 delimiter = pos;
executed: delimiter = pos;
Execution Count:10
10
267 -
268 // pos is the end of this pair (the end of the string or the pair delimiter) -
269 // delimiter points to the value delimiter or to the end of this pair -
270 -
271 QString key;
executed (the execution status of this line is deduced): QString key;
-
272 if (!qt_urlRecode(key, begin, delimiter,
evaluated: !qt_urlRecode(key, begin, delimiter, QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:60
yes
Evaluation Count:12
12-60
273 QUrl::DecodeReserved,
evaluated: !qt_urlRecode(key, begin, delimiter, QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:60
yes
Evaluation Count:12
12-60
274 prettyDecodedActions))
evaluated: !qt_urlRecode(key, begin, delimiter, QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:60
yes
Evaluation Count:12
12-60
275 key = QString(begin, delimiter - begin);
executed: key = QString(begin, delimiter - begin);
Execution Count:60
60
276 -
277 if (delimiter == pos) {
evaluated: delimiter == pos
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:62
10-62
278 // the value delimiter wasn't found, store a null value -
279 itemList.append(qMakePair(key, QString()));
executed (the execution status of this line is deduced): itemList.append(qMakePair(key, QString()));
-
280 } else if (delimiter + 1 == pos) {
executed: }
Execution Count:10
evaluated: delimiter + 1 == pos
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:57
5-57
281 // if the delimiter was found but the value is empty, store empty-but-not-null -
282 itemList.append(qMakePair(key, QString(0, Qt::Uninitialized)));
executed (the execution status of this line is deduced): itemList.append(qMakePair(key, QString(0, Qt::Uninitialized)));
-
283 } else {
executed: }
Execution Count:5
5
284 QString value;
executed (the execution status of this line is deduced): QString value;
-
285 if (!qt_urlRecode(value, delimiter + 1, pos,
evaluated: !qt_urlRecode(value, delimiter + 1, pos, QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:45
yes
Evaluation Count:12
12-45
286 QUrl::DecodeReserved,
evaluated: !qt_urlRecode(value, delimiter + 1, pos, QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:45
yes
Evaluation Count:12
12-45
287 prettyDecodedActions))
evaluated: !qt_urlRecode(value, delimiter + 1, pos, QUrl::DecodeReserved, prettyDecodedActions)
TRUEFALSE
yes
Evaluation Count:45
yes
Evaluation Count:12
12-45
288 value = QString(delimiter + 1, pos - delimiter - 1);
executed: value = QString(delimiter + 1, pos - delimiter - 1);
Execution Count:45
45
289 itemList.append(qMakePair(key, value));
executed (the execution status of this line is deduced): itemList.append(qMakePair(key, value));
-
290 }
executed: }
Execution Count:57
57
291 -
292 if (pos != end)
evaluated: pos != end
TRUEFALSE
yes
Evaluation Count:24
yes
Evaluation Count:48
24-48
293 ++pos;
executed: ++pos;
Execution Count:24
24
294 }
executed: }
Execution Count:72
72
295}
executed: }
Execution Count:49
49
296 -
297// allow QUrlQueryPrivate to detach from null -
298template <> inline QUrlQueryPrivate * -
299QSharedDataPointer<QUrlQueryPrivate>::clone() -
300{ -
301 return d ? new QUrlQueryPrivate(*d) : new QUrlQueryPrivate;
never executed: return d ? new QUrlQueryPrivate(*d) : new QUrlQueryPrivate;
0
302} -
303 -
304/*! -
305 Constructs an empty QUrlQuery object. A query can be set afterwards by -
306 calling setQuery() or items can be added by using addQueryItem(). -
307 -
308 \sa setQuery(), addQueryItem() -
309*/ -
310QUrlQuery::QUrlQuery() -
311 : d(0) -
312{ -
313}
executed: }
Execution Count:60
60
314 -
315/*! -
316 Constructs a QUrlQuery object and parses the \a queryString query string, -
317 using the default query delimiters. To parse a query string using other -
318 delimiters, you should first set them using setQueryDelimiters() and then -
319 set the query with setQuery(). -
320*/ -
321QUrlQuery::QUrlQuery(const QString &queryString) -
322 : d(queryString.isEmpty() ? 0 : new QUrlQueryPrivate(queryString)) -
323{ -
324}
executed: }
Execution Count:49
49
325 -
326/*! -
327 Constructs a QUrlQuery object and parses the query string found in the \a -
328 url URL, using the default query delimiters. To parse a query string using -
329 other delimiters, you should first set them using setQueryDelimiters() and -
330 then set the query with setQuery(). -
331 -
332 \sa QUrl::query() -
333*/ -
334QUrlQuery::QUrlQuery(const QUrl &url) -
335 : d(0) -
336{ -
337 // use internals to avoid unnecessary recoding -
338 // ### FIXME: actually do it -
339 if (url.hasQuery())
never evaluated: url.hasQuery()
0
340 d = new QUrlQueryPrivate(url.query());
never executed: d = new QUrlQueryPrivate(url.query());
0
341}
never executed: }
0
342 -
343/*! -
344 Copies the contents of the \a other QUrlQuery object, including the query -
345 delimiters. -
346*/ -
347QUrlQuery::QUrlQuery(const QUrlQuery &other) -
348 : d(other.d) -
349{ -
350}
executed: }
Execution Count:5
5
351 -
352/*! -
353 Copies the contents of the \a other QUrlQuery object, including the query -
354 delimiters. -
355*/ -
356QUrlQuery &QUrlQuery::operator =(const QUrlQuery &other) -
357{ -
358 d = other.d;
executed (the execution status of this line is deduced): d = other.d;
-
359 return *this;
executed: return *this;
Execution Count:4
4
360} -
361 -
362/*! -
363 \fn void QUrlQuery::swap(QUrlQuery &other) -
364 -
365 Swaps this URL query instance with \a other. This function is very -
366 fast and never fails. -
367*/ -
368 -
369/*! -
370 Destroys this QUrlQuery object. -
371*/ -
372QUrlQuery::~QUrlQuery() -
373{ -
374 // d auto-deletes -
375} -
376 -
377/*! -
378 Returns true if this object and the \a other object contain the same -
379 contents, in the same order, and use the same query delimiters. -
380*/ -
381bool QUrlQuery::operator ==(const QUrlQuery &other) const -
382{ -
383 if (d == other.d)
evaluated: d == other.d
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:10
7-10
384 return true;
executed: return true;
Execution Count:7
7
385 if (d && other.d)
partially evaluated: d
TRUEFALSE
yes
Evaluation Count:10
no
Evaluation Count:0
evaluated: other.d
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:2
0-10
386 return d->valueDelimiter == other.d->valueDelimiter &&
executed: return d->valueDelimiter == other.d->valueDelimiter && d->pairDelimiter == other.d->pairDelimiter && d->itemList == other.d->itemList;
Execution Count:8
8
387 d->pairDelimiter == other.d->pairDelimiter &&
executed: return d->valueDelimiter == other.d->valueDelimiter && d->pairDelimiter == other.d->pairDelimiter && d->itemList == other.d->itemList;
Execution Count:8
8
388 d->itemList == other.d->itemList;
executed: return d->valueDelimiter == other.d->valueDelimiter && d->pairDelimiter == other.d->pairDelimiter && d->itemList == other.d->itemList;
Execution Count:8
8
389 return false;
executed: return false;
Execution Count:2
2
390} -
391 -
392/*! -
393 Returns true if this QUrlQUery object contains no key-value pairs, such as -
394 after being default-constructed or after parsing an empty query string. -
395 -
396 \sa setQuery(), clear() -
397*/ -
398bool QUrlQuery::isEmpty() const -
399{ -
400 return d ? d->itemList.isEmpty() : true;
executed: return d ? d->itemList.isEmpty() : true;
Execution Count:37
37
401} -
402 -
403/*! -
404 \internal -
405*/ -
406bool QUrlQuery::isDetached() const -
407{ -
408 return d && d->ref.load() == 1;
executed: return d && d->ref.load() == 1;
Execution Count:4
4
409} -
410 -
411/*! -
412 Clears this QUrlQuery object by removing all of the key-value pairs -
413 currently stored. If the query delimiters have been changed, this function -
414 will leave them with their changed values. -
415 -
416 \sa isEmpty(), setQueryDelimiters() -
417*/ -
418void QUrlQuery::clear() -
419{ -
420 if (d.constData())
evaluated: d.constData()
TRUEFALSE
yes
Evaluation Count:16
yes
Evaluation Count:5
5-16
421 d->itemList.clear();
executed: d->itemList.clear();
Execution Count:16
16
422}
executed: }
Execution Count:21
21
423 -
424/*! -
425 Parses the query string in \a queryString and sets the internal items to -
426 the values found there. If any delimiters have been specified with -
427 setQueryDelimiters(), this function will use them instead of the default -
428 delimiters to parse the string. -
429*/ -
430void QUrlQuery::setQuery(const QString &queryString) -
431{ -
432 d->setQuery(queryString);
executed (the execution status of this line is deduced): d->setQuery(queryString);
-
433}
executed: }
Execution Count:3
3
434 -
435static void recodeAndAppend(QString &to, const QString &input, -
436 QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications) -
437{ -
438 if (!qt_urlRecode(to, input.constData(), input.constData() + input.length(), encoding, tableModifications))
evaluated: !qt_urlRecode(to, input.constData(), input.constData() + input.length(), encoding, tableModifications)
TRUEFALSE
yes
Evaluation Count:123
yes
Evaluation Count:32
32-123
439 to += input;
executed: to += input;
Execution Count:123
123
440}
executed: }
Execution Count:155
155
441 -
442/*! -
443 Returns the reconstructed query string, formed from the key-value pairs -
444 currently stored in this QUrlQuery object and separated by the query -
445 delimiters chosen for this object. The keys and values are encoded using -
446 the options given by the \a encoding parameter. -
447 -
448 For this function, the only ambiguous delimiter is the hash ("#"), as in -
449 URLs it is used to separate the query string from the fragment that may -
450 follow. -
451 -
452 The order of the key-value pairs in the returned string is exactly the same -
453 as in the original query. -
454 -
455 \sa setQuery(), QUrl::setQuery(), QUrl::fragment(), {encoding}{Encoding} -
456*/ -
457QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const -
458{ -
459 if (!d)
evaluated: !d
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:66
2-66
460 return QString();
executed: return QString();
Execution Count:2
2
461 -
462 // unlike the component encoding, for the whole query we need to modify a little: -
463 // - the "#" character is ambiguous, so we decode it only in DecodeAllDelimiters mode -
464 // - the query delimiter pair must always be encoded -
465 // - the non-delimiters vary on DecodeUnambiguousDelimiters -
466 // so: -
467 // - full encoding: encode the non-delimiters, the pair, "#", "[" and "]" -
468 // - pretty decode: decode the non-delimiters, "[" and "]"; encode the pair and "#" -
469 // - decode all: decode the non-delimiters, "[", "]", "#"; encode the pair -
470 -
471 // start with what's always encoded -
472 ushort tableActions[] = {
executed (the execution status of this line is deduced): ushort tableActions[] = {
-
473 leave('+'), // 0
executed (the execution status of this line is deduced): ushort(0x100 | ('+')),
-
474 encode(d->pairDelimiter.unicode()), // 1
executed (the execution status of this line is deduced): ushort(0x200 | (d->pairDelimiter.unicode())),
-
475 encode(d->valueDelimiter.unicode()), // 2
executed (the execution status of this line is deduced): ushort(0x200 | (d->valueDelimiter.unicode())),
-
476 decode('#'), // 3
executed (the execution status of this line is deduced): ushort('#'),
-
477 0
executed (the execution status of this line is deduced): 0
-
478 };
executed (the execution status of this line is deduced): };
-
479 if (encoding & QUrl::EncodeDelimiters) {
evaluated: encoding & QUrl::EncodeDelimiters
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:60
6-60
480 tableActions[3] = encode('#');
executed (the execution status of this line is deduced): tableActions[3] = ushort(0x200 | ('#'));
-
481 }
executed: }
Execution Count:6
6
482 -
483 QString result;
executed (the execution status of this line is deduced): QString result;
-
484 Map::const_iterator it = d->itemList.constBegin();
executed (the execution status of this line is deduced): Map::const_iterator it = d->itemList.constBegin();
-
485 Map::const_iterator end = d->itemList.constEnd();
executed (the execution status of this line is deduced): Map::const_iterator end = d->itemList.constEnd();
-
486 -
487 { -
488 int size = 0;
executed (the execution status of this line is deduced): int size = 0;
-
489 for ( ; it != end; ++it)
evaluated: it != end
TRUEFALSE
yes
Evaluation Count:81
yes
Evaluation Count:66
66-81
490 size += it->first.length() + 1 + it->second.length() + 1;
executed: size += it->first.length() + 1 + it->second.length() + 1;
Execution Count:81
81
491 result.reserve(size + size / 4);
executed (the execution status of this line is deduced): result.reserve(size + size / 4);
-
492 } -
493 -
494 for (it = d->itemList.constBegin(); it != end; ++it) {
evaluated: it != end
TRUEFALSE
yes
Evaluation Count:81
yes
Evaluation Count:66
66-81
495 if (!result.isEmpty())
evaluated: !result.isEmpty()
TRUEFALSE
yes
Evaluation Count:15
yes
Evaluation Count:66
15-66
496 result += QChar(d->pairDelimiter);
executed: result += QChar(d->pairDelimiter);
Execution Count:15
15
497 recodeAndAppend(result, it->first, encoding, tableActions);
executed (the execution status of this line is deduced): recodeAndAppend(result, it->first, encoding, tableActions);
-
498 if (!it->second.isNull()) {
evaluated: !it->second.isNull()
TRUEFALSE
yes
Evaluation Count:74
yes
Evaluation Count:7
7-74
499 result += QChar(d->valueDelimiter);
executed (the execution status of this line is deduced): result += QChar(d->valueDelimiter);
-
500 recodeAndAppend(result, it->second, encoding, tableActions);
executed (the execution status of this line is deduced): recodeAndAppend(result, it->second, encoding, tableActions);
-
501 }
executed: }
Execution Count:74
74
502 }
executed: }
Execution Count:81
81
503 return result;
executed: return result;
Execution Count:66
66
504} -
505 -
506/*! -
507 Sets the characters used for delimiting between keys and values, -
508 and between key-value pairs in the URL's query string. The default -
509 value delimiter is '=' and the default pair delimiter is '&'. -
510 -
511 \image qurl-querystring.png -
512 -
513 \a valueDelimiter will be used for separating keys from values, -
514 and \a pairDelimiter will be used to separate key-value pairs. -
515 Any occurrences of these delimiting characters in the encoded -
516 representation of the keys and values of the query string are -
517 percent encoded when returned in query(). -
518 -
519 If \a valueDelimiter is set to '(' and \a pairDelimiter is ')', -
520 the above query string would instead be represented like this: -
521 -
522 \snippet code/src_corelib_io_qurl.cpp 4 -
523 -
524 \note Non-standard delimiters should be chosen from among what RFC 3986 calls -
525 "sub-delimiters". They are: -
526 -
527 \code -
528 sub-delims = "!" / "$" / "&" / "'" / "(" / ")" -
529 / "*" / "+" / "," / ";" / "=" -
530 \endcode -
531 -
532 Use of other characters is not supported and may result in unexpected -
533 behaviour. This method does not verify that you passed a valid delimiter. -
534 -
535 \sa queryValueDelimiter(), queryPairDelimiter() -
536*/ -
537void QUrlQuery::setQueryDelimiters(QChar valueDelimiter, QChar pairDelimiter) -
538{ -
539 d->valueDelimiter = valueDelimiter.unicode();
executed (the execution status of this line is deduced): d->valueDelimiter = valueDelimiter.unicode();
-
540 d->pairDelimiter = pairDelimiter.unicode();
executed (the execution status of this line is deduced): d->pairDelimiter = pairDelimiter.unicode();
-
541}
executed: }
Execution Count:3
3
542 -
543/*! -
544 Returns the character used to delimit between keys and values when -
545 reconstructing the query string in query() or when parsing in setQuery(). -
546 -
547 \sa setQueryDelimiters(), queryPairDelimiter() -
548*/ -
549QChar QUrlQuery::queryValueDelimiter() const -
550{ -
551 return d ? d->valueDelimiter : defaultQueryValueDelimiter();
executed: return d ? d->valueDelimiter : defaultQueryValueDelimiter();
Execution Count:2
2
552} -
553 -
554/*! -
555 Returns the character used to delimit between keys-value pairs when -
556 reconstructing the query string in query() or when parsing in setQuery(). -
557 -
558 \sa setQueryDelimiters(), queryValueDelimiter() -
559*/ -
560QChar QUrlQuery::queryPairDelimiter() const -
561{ -
562 return d ? d->pairDelimiter : defaultQueryPairDelimiter();
executed: return d ? d->pairDelimiter : defaultQueryPairDelimiter();
Execution Count:2
2
563} -
564 -
565/*! -
566 Sets the items in this QUrlQuery object to \a query. The order of the -
567 elements in \a query is preserved. -
568 -
569 \note This method does not treat spaces (ASCII 0x20) and plus ("+") signs -
570 as the same, like HTML forms do. If you need spaces to be represented as -
571 plus signs, use actual plus signs. -
572 -
573 \sa queryItems(), isEmpty() -
574*/ -
575void QUrlQuery::setQueryItems(const QList<QPair<QString, QString> > &query) -
576{ -
577 clear();
executed (the execution status of this line is deduced): clear();
-
578 if (query.isEmpty())
evaluated: query.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:9
1-9
579 return;
executed: return;
Execution Count:1
1
580 -
581 QUrlQueryPrivate *dd = d;
executed (the execution status of this line is deduced): QUrlQueryPrivate *dd = d;
-
582 QList<QPair<QString, QString> >::const_iterator it = query.constBegin(),
executed (the execution status of this line is deduced): QList<QPair<QString, QString> >::const_iterator it = query.constBegin(),
-
583 end = query.constEnd();
executed (the execution status of this line is deduced): end = query.constEnd();
-
584 for ( ; it != end; ++it)
evaluated: it != end
TRUEFALSE
yes
Evaluation Count:24
yes
Evaluation Count:9
9-24
585 dd->addQueryItem(it->first, it->second);
executed: dd->addQueryItem(it->first, it->second);
Execution Count:24
24
586}
executed: }
Execution Count:9
9
587 -
588/*! -
589 Returns the query string of the URL, as a map of keys and values, using the -
590 options specified in \a encoding to encode the items. The order of the -
591 elements is the same as the one found in the query string or set with -
592 setQueryItems(). -
593 -
594 \sa setQueryItems(), {encoding}{Encoding} -
595*/ -
596QList<QPair<QString, QString> > QUrlQuery::queryItems(QUrl::ComponentFormattingOptions encoding) const -
597{ -
598 if (!d)
evaluated: !d
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:87
5-87
599 return QList<QPair<QString, QString> >();
executed: return QList<QPair<QString, QString> >();
Execution Count:5
5
600 if (idempotentRecodeToUser(encoding))
evaluated: idempotentRecodeToUser(encoding)
TRUEFALSE
yes
Evaluation Count:76
yes
Evaluation Count:11
11-76
601 return d->itemList;
executed: return d->itemList;
Execution Count:76
76
602 -
603 QList<QPair<QString, QString> > result;
executed (the execution status of this line is deduced): QList<QPair<QString, QString> > result;
-
604 Map::const_iterator it = d->itemList.constBegin();
executed (the execution status of this line is deduced): Map::const_iterator it = d->itemList.constBegin();
-
605 Map::const_iterator end = d->itemList.constEnd();
executed (the execution status of this line is deduced): Map::const_iterator end = d->itemList.constEnd();
-
606 for ( ; it != end; ++it)
evaluated: it != end
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:11
11-12
607 result << qMakePair(d->recodeToUser(it->first, encoding),
executed: result << qMakePair(d->recodeToUser(it->first, encoding), d->recodeToUser(it->second, encoding));
Execution Count:12
12
608 d->recodeToUser(it->second, encoding));
executed: result << qMakePair(d->recodeToUser(it->first, encoding), d->recodeToUser(it->second, encoding));
Execution Count:12
12
609 return result;
executed: return result;
Execution Count:11
11
610} -
611 -
612/*! -
613 Returns true if there is a query string pair whose key is equal -
614 to \a key from the URL. -
615 -
616 \sa addQueryItem(), queryItemValue() -
617*/ -
618bool QUrlQuery::hasQueryItem(const QString &key) const -
619{ -
620 if (!d)
evaluated: !d
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:16
4-16
621 return false;
executed: return false;
Execution Count:4
4
622 return d->findKey(key) != d->itemList.constEnd();
executed: return d->findKey(key) != d->itemList.constEnd();
Execution Count:16
16
623} -
624 -
625/*! -
626 Appends the pair \a key = \a value to the end of the query string of the -
627 URL. This method does not overwrite existing items that might exist with -
628 the same key. -
629 -
630 \note This method does not treat spaces (ASCII 0x20) and plus ("+") signs -
631 as the same, like HTML forms do. If you need spaces to be represented as -
632 plus signs, use actual plus signs. -
633 -
634 \sa hasQueryItem(), queryItemValue() -
635*/ -
636void QUrlQuery::addQueryItem(const QString &key, const QString &value) -
637{ -
638 d->addQueryItem(key, value);
executed (the execution status of this line is deduced): d->addQueryItem(key, value);
-
639}
executed: }
Execution Count:80
80
640 -
641/*! -
642 Returns the query value associated with key \a key from the URL, using the -
643 options specified in \a encoding to encode the return value. If the key \a -
644 key is not found, this function returns an empty string. If you need to -
645 distinguish between an empty value and a non-existent key, you should check -
646 for the key's presence first using hasQueryItem(). -
647 -
648 If the key \a key is multiply defined, this function will return the first -
649 one found, in the order they were present in the query string or added -
650 using addQueryItem(). -
651 -
652 \sa addQueryItem(), allQueryItemValues(), {encoding}{Encoding} -
653*/ -
654QString QUrlQuery::queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const -
655{ -
656 QString result;
executed (the execution status of this line is deduced): QString result;
-
657 if (d) {
evaluated: d
TRUEFALSE
yes
Evaluation Count:16
yes
Evaluation Count:3
3-16
658 Map::const_iterator it = d->findKey(key);
executed (the execution status of this line is deduced): Map::const_iterator it = d->findKey(key);
-
659 if (it != d->itemList.constEnd())
partially evaluated: it != d->itemList.constEnd()
TRUEFALSE
yes
Evaluation Count:16
no
Evaluation Count:0
0-16
660 result = d->recodeToUser(it->second, encoding);
executed: result = d->recodeToUser(it->second, encoding);
Execution Count:16
16
661 }
executed: }
Execution Count:16
16
662 return result;
executed: return result;
Execution Count:19
19
663} -
664 -
665/*! -
666 Returns the a list of query string values whose key is equal to \a key from -
667 the URL, using the options specified in \a encoding to encode the return -
668 value. If the key \a key is not found, this function returns an empty list. -
669 -
670 \sa queryItemValue(), addQueryItem() -
671*/ -
672QStringList QUrlQuery::allQueryItemValues(const QString &key, QUrl::ComponentFormattingOptions encoding) const -
673{ -
674 QStringList result;
executed (the execution status of this line is deduced): QStringList result;
-
675 if (d) {
evaluated: d
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:3
3-12
676 QString encodedKey = d->recodeFromUser(key);
executed (the execution status of this line is deduced): QString encodedKey = d->recodeFromUser(key);
-
677 int idx = d->findRecodedKey(encodedKey);
executed (the execution status of this line is deduced): int idx = d->findRecodedKey(encodedKey);
-
678 while (idx < d->itemList.size()) {
evaluated: idx < d->itemList.size()
TRUEFALSE
yes
Evaluation Count:19
yes
Evaluation Count:12
12-19
679 result << d->recodeToUser(d->itemList.at(idx).second, encoding);
executed (the execution status of this line is deduced): result << d->recodeToUser(d->itemList.at(idx).second, encoding);
-
680 idx = d->findRecodedKey(encodedKey, idx + 1);
executed (the execution status of this line is deduced): idx = d->findRecodedKey(encodedKey, idx + 1);
-
681 }
executed: }
Execution Count:19
19
682 }
executed: }
Execution Count:12
12
683 return result;
executed: return result;
Execution Count:15
15
684} -
685 -
686/*! -
687 Removes the query string pair whose key is equal to \a key from the URL. If -
688 there are multiple items with a key equal to \a key, it removes the first -
689 item in the order they were present in the query string or added with -
690 addQueryItem(). -
691 -
692 \sa removeAllQueryItems() -
693*/ -
694void QUrlQuery::removeQueryItem(const QString &key) -
695{ -
696 if (d) {
partially evaluated: d
TRUEFALSE
yes
Evaluation Count:6
no
Evaluation Count:0
0-6
697 Map::iterator it = d->findKey(key);
executed (the execution status of this line is deduced): Map::iterator it = d->findKey(key);
-
698 if (it != d->itemList.end())
evaluated: it != d->itemList.end()
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:1
1-5
699 d->itemList.erase(it);
executed: d->itemList.erase(it);
Execution Count:5
5
700 }
executed: }
Execution Count:6
6
701}
executed: }
Execution Count:6
6
702 -
703/*! -
704 Removes all the query string pairs whose key is equal to \a key -
705 from the URL. -
706 -
707 \sa removeQueryItem() -
708*/ -
709void QUrlQuery::removeAllQueryItems(const QString &key) -
710{ -
711 if (d.constData()) {
partially evaluated: d.constData()
TRUEFALSE
yes
Evaluation Count:3
no
Evaluation Count:0
0-3
712 QString encodedKey = d->recodeFromUser(key);
executed (the execution status of this line is deduced): QString encodedKey = d->recodeFromUser(key);
-
713 Map::iterator it = d->itemList.begin();
executed (the execution status of this line is deduced): Map::iterator it = d->itemList.begin();
-
714 while (it != d->itemList.end()) {
evaluated: it != d->itemList.end()
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:3
3-14
715 if (it->first == encodedKey)
evaluated: it->first == encodedKey
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:8
6-8
716 it = d->itemList.erase(it);
executed: it = d->itemList.erase(it);
Execution Count:6
6
717 else -
718 ++it;
executed: ++it;
Execution Count:8
8
719 } -
720 }
executed: }
Execution Count:3
3
721}
executed: }
Execution Count:3
3
722 -
723/*! -
724 \fn QChar QUrlQuery::defaultQueryValueDelimiter() -
725 Returns the default character for separating keys from values in the query, -
726 an equal sign ("="). -
727 -
728 \sa setQueryDelimiters(), queryValueDelimiter(), defaultQueryPairDelimiter() -
729*/ -
730 -
731/*! -
732 \fn QChar QUrlQuery::defaultQueryPairDelimiter() -
733 Returns the default character for separating keys-value pairs from each -
734 other, an ampersand ("&"). -
735 -
736 \sa setQueryDelimiters(), queryPairDelimiter(), defaultQueryValueDelimiter() -
737*/ -
738 -
739/*! -
740 \typedef QUrlQuery::DataPtr -
741 \internal -
742*/ -
743 -
744/*! -
745 \fn DataPtr &QUrlQuery::data_ptr() -
746 \internal -
747*/ -
748 -
749/*! -
750 \fn QString QUrlQuery::toString(QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const -
751 -
752 Returns this QUrlQuery as a QString. \a encoding can be used to specify the URL string encoding of the return value. -
753*/ -
754 -
755/*! -
756 \fn bool QUrlQuery::operator!=(const QUrlQuery &other) const -
757 -
758 Returns true if \a other is not equal to this QUrlQuery. Otherwise, returns false. -
759 -
760 \sa operator==() -
761*/ -
762QT_END_NAMESPACE -
763 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial