qurlquery.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qurlquery.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6typedef QList<QPair<QString, QString> > Map;-
7-
8class QUrlQueryPrivate : public QSharedData-
9{-
10public:-
11 QUrlQueryPrivate(const QString &query = QString())-
12 : valueDelimiter(QUrlQuery::defaultQueryValueDelimiter()),-
13 pairDelimiter(QUrlQuery::defaultQueryPairDelimiter())-
14 { if (!query.isEmpty()) setQuery(query); }-
15-
16 QString recodeFromUser(const QString &input) const;-
17 QString recodeToUser(const QString &input, QUrl::ComponentFormattingOptions encoding) const;-
18-
19 void setQuery(const QString &query);-
20-
21 void addQueryItem(const QString &key, const QString &value)-
22 { itemList.append(qMakePair(recodeFromUser(key), recodeFromUser(value))); }-
23 int findRecodedKey(const QString &key, int from = 0) const-
24 {-
25 for (int i = from; i < itemList.size(); ++i)-
26 if (itemList.at(i).first == key)-
27 return i;-
28 return itemList.size();-
29 }-
30 Map::const_iterator findKey(const QString &key) const-
31 { return itemList.constBegin() + findRecodedKey(recodeFromUser(key)); }-
32 Map::iterator findKey(const QString &key)-
33 { return itemList.begin() + findRecodedKey(recodeFromUser(key)); }-
34-
35 Map itemList;-
36 QChar valueDelimiter;-
37 QChar pairDelimiter;-
38};-
39-
40template<> void QSharedDataPointer<QUrlQueryPrivate>::detach()-
41{-
42 if (d && d->ref.load() == 1)-
43 return;-
44 QUrlQueryPrivate *x = (d ? new QUrlQueryPrivate(*d)-
45 : new QUrlQueryPrivate);-
46 x->ref.ref();-
47 if (d && !d->ref.deref())-
48 delete d;-
49 d = x;-
50}-
51inline QString QUrlQueryPrivate::recodeFromUser(const QString &input) const-
52{-
53-
54 QString output;-
55 ushort prettyDecodedActions[] = {-
56 ushort(pairDelimiter.unicode()),-
57 ushort(valueDelimiter.unicode()),-
58 ushort('#'),-
59 0-
60 };-
61 if (qt_urlRecode(output, input.constData(), input.constData() + input.length(),-
62 QUrl::DecodeReserved,-
63 prettyDecodedActions))-
64 return output;-
65 return input;-
66}-
67-
68inline bool idempotentRecodeToUser(QUrl::ComponentFormattingOptions encoding)-
69{-
70 return encoding == QUrl::PrettyDecoded;-
71}-
72-
73inline QString QUrlQueryPrivate::recodeToUser(const QString &input, QUrl::ComponentFormattingOptions encoding) const-
74{-
75-
76-
77 if (idempotentRecodeToUser(encoding))-
78 return input;-
79-
80 if (!(encoding & QUrl::EncodeDelimiters)) {-
81 QString output;-
82 if (qt_urlRecode(output, input.constData(), input.constData() + input.length(),-
83 encoding, 0))-
84 return output;-
85 return input;-
86 }-
87-
88-
89 ushort actions[] = { ushort(0x200 | (pairDelimiter.unicode())), ushort(0x200 | (valueDelimiter.unicode())),-
90 ushort(0x200 | ('#')), 0 };-
91 QString output;-
92 if (qt_urlRecode(output, input.constData(), input.constData() + input.length(), encoding, actions))-
93 return output;-
94 return input;-
95}-
96-
97void QUrlQueryPrivate::setQuery(const QString &query)-
98{-
99 ushort prettyDecodedActions[] = {-
100 ushort(pairDelimiter.unicode()),-
101 ushort(valueDelimiter.unicode()),-
102 ushort('#'),-
103 0-
104 };-
105-
106 itemList.clear();-
107 const QChar *pos = query.constData();-
108 const QChar *const end = pos + query.size();-
109 while (pos != end) {-
110 const QChar *begin = pos;-
111 const QChar *delimiter = 0;-
112 while (pos != end) {-
113-
114 if (!delimiter && pos->unicode() == valueDelimiter)-
115 delimiter = pos;-
116 if (pos->unicode() == pairDelimiter)-
117 break;-
118 ++pos;-
119 }-
120 if (!delimiter)-
121 delimiter = pos;-
122-
123-
124-
125-
126 QString key;-
127 if (!qt_urlRecode(key, begin, delimiter,-
128 QUrl::DecodeReserved,-
129 prettyDecodedActions))-
130 key = QString(begin, delimiter - begin);-
131-
132 if (delimiter == pos) {-
133-
134 itemList.append(qMakePair(key, QString()));-
135 } else if (delimiter + 1 == pos) {-
136-
137 itemList.append(qMakePair(key, QString(0, Qt::Uninitialized)));-
138 } else {-
139 QString value;-
140 if (!qt_urlRecode(value, delimiter + 1, pos,-
141 QUrl::DecodeReserved,-
142 prettyDecodedActions))-
143 value = QString(delimiter + 1, pos - delimiter - 1);-
144 itemList.append(qMakePair(key, value));-
145 }-
146-
147 if (pos != end)-
148 ++pos;-
149 }-
150}-
151-
152-
153template <> inline QUrlQueryPrivate *-
154QSharedDataPointer<QUrlQueryPrivate>::clone()-
155{-
156 return d ? new QUrlQueryPrivate(*d) : new QUrlQueryPrivate;-
157}-
158-
159-
160-
161-
162-
163-
164-
165QUrlQuery::QUrlQuery()-
166 : d(0)-
167{-
168}-
169-
170-
171-
172-
173-
174-
175-
176QUrlQuery::QUrlQuery(const QString &queryString)-
177 : d(queryString.isEmpty() ? 0 : new QUrlQueryPrivate(queryString))-
178{-
179}-
180QUrlQuery::QUrlQuery(const QUrl &url)-
181 : d(0)-
182{-
183-
184-
185 if (url.hasQuery())-
186 d = new QUrlQueryPrivate(url.query());-
187}-
188-
189-
190-
191-
192-
193QUrlQuery::QUrlQuery(const QUrlQuery &other)-
194 : d(other.d)-
195{-
196}-
197-
198-
199-
200-
201-
202QUrlQuery &QUrlQuery::operator =(const QUrlQuery &other)-
203{-
204 d = other.d;-
205 return *this;-
206}-
207QUrlQuery::~QUrlQuery()-
208{-
209-
210}-
211-
212-
213-
214-
215-
216bool QUrlQuery::operator ==(const QUrlQuery &other) const-
217{-
218 if (d == other.d)-
219 return true;-
220 if (d && other.d)-
221-
222 return d->valueDelimiter == other.d->valueDelimiter &&-
223 d->pairDelimiter == other.d->pairDelimiter &&-
224 d->itemList == other.d->itemList;-
225 return false;-
226}-
227uint qHash(const QUrlQuery &key, uint seed) noexcept-
228{-
229 if (const
const QUrlQuer...ate *d = key.dDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QUrlQuery
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QUrlQuery
QUrlQueryPrivate *d = key.d
const QUrlQuer...ate *d = key.dDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QUrlQuery
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QUrlQuery
) {
2-4
230 QtPrivate::QHashCombine hash;-
231-
232 seed = hash(seed, d->valueDelimiter);-
233 seed = hash(seed, d->pairDelimiter);-
234 seed = hash(seed, d->itemList);-
235 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_QUrlQuery
2
236 return
executed 6 times by 1 test: return seed;
Executed by:
  • tst_QUrlQuery
seed;
executed 6 times by 1 test: return seed;
Executed by:
  • tst_QUrlQuery
6
237}-
238-
239-
240-
241-
242-
243-
244-
245bool QUrlQuery::isEmpty() const-
246{-
247 return d ? d->itemList.isEmpty() : true;-
248}-
249-
250-
251-
252-
253bool QUrlQuery::isDetached() const-
254{-
255 return d && d->ref.load() == 1;-
256}-
257void QUrlQuery::clear()-
258{-
259 if (d.constData())-
260 d->itemList.clear();-
261}-
262-
263-
264-
265-
266-
267-
268-
269void QUrlQuery::setQuery(const QString &queryString)-
270{-
271 d->setQuery(queryString);-
272}-
273-
274static void recodeAndAppend(QString &to, const QString &input,-
275 QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications)-
276{-
277 if (!qt_urlRecode(to, input.constData(), input.constData() + input.length(), encoding, tableModifications))-
278 to += input;-
279}-
280QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const-
281{-
282 if (!d)-
283 return QString();-
284-
285-
286-
287-
288-
289-
290 ushort tableActions[] = {-
291 ushort(0x200 | (d->pairDelimiter.unicode())),-
292 ushort(0x200 | (d->valueDelimiter.unicode())),-
293 0,-
294 0-
295 };-
296 if (encoding & QUrl::EncodeDelimiters) {-
297 tableActions[2] = ushort(0x200 | ('#'));-
298 }-
299-
300 QString result;-
301 Map::const_iterator it = d->itemList.constBegin();-
302 Map::const_iterator end = d->itemList.constEnd();-
303-
304 {-
305 int size = 0;-
306 for ( ; it != end; ++it)-
307 size += it->first.length() + 1 + it->second.length() + 1;-
308 result.reserve(size + size / 4);-
309 }-
310-
311 for (it = d->itemList.constBegin(); it != end; ++it) {-
312 if (!result.isEmpty())-
313 result += QChar(d->pairDelimiter);-
314 recodeAndAppend(result, it->first, encoding, tableActions);-
315 if (!it->second.isNull()) {-
316 result += QChar(d->valueDelimiter);-
317 recodeAndAppend(result, it->second, encoding, tableActions);-
318 }-
319 }-
320 return result;-
321}-
322void QUrlQuery::setQueryDelimiters(QChar valueDelimiter, QChar pairDelimiter)-
323{-
324 d->valueDelimiter = valueDelimiter.unicode();-
325 d->pairDelimiter = pairDelimiter.unicode();-
326}-
327-
328-
329-
330-
331-
332-
333-
334QChar QUrlQuery::queryValueDelimiter() const-
335{-
336 return d ? d->valueDelimiter : defaultQueryValueDelimiter();-
337}-
338-
339-
340-
341-
342-
343-
344-
345QChar QUrlQuery::queryPairDelimiter() const-
346{-
347 return d ? d->pairDelimiter : defaultQueryPairDelimiter();-
348}-
349void QUrlQuery::setQueryItems(const QList<QPair<QString, QString> > &query)-
350{-
351 clear();-
352 if (query.isEmpty())-
353 return;-
354-
355 QUrlQueryPrivate *dd = d;-
356 QList<QPair<QString, QString> >::const_iterator it = query.constBegin(),-
357 end = query.constEnd();-
358 for ( ; it != end; ++it)-
359 dd->addQueryItem(it->first, it->second);-
360}-
361QList<QPair<QString, QString> > QUrlQuery::queryItems(QUrl::ComponentFormattingOptions encoding) const-
362{-
363 if (!d)-
364 return QList<QPair<QString, QString> >();-
365 if (idempotentRecodeToUser(encoding))-
366 return d->itemList;-
367-
368 QList<QPair<QString, QString> > result;-
369 Map::const_iterator it = d->itemList.constBegin();-
370 Map::const_iterator end = d->itemList.constEnd();-
371 result.reserve(d->itemList.count());-
372 for ( ; it != end; ++it)-
373 result << qMakePair(d->recodeToUser(it->first, encoding),-
374 d->recodeToUser(it->second, encoding));-
375 return result;-
376}-
377-
378-
379-
380-
381-
382-
383-
384bool QUrlQuery::hasQueryItem(const QString &key) const-
385{-
386 if (!d)-
387 return false;-
388 return d->findKey(key) != d->itemList.constEnd();-
389}-
390void QUrlQuery::addQueryItem(const QString &key, const QString &value)-
391{-
392 d->addQueryItem(key, value);-
393}-
394QString QUrlQuery::queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const-
395{-
396 QString result;-
397 if (d) {-
398 Map::const_iterator it = d->findKey(key);-
399 if (it != d->itemList.constEnd())-
400 result = d->recodeToUser(it->second, encoding);-
401 }-
402 return result;-
403}-
404QStringList QUrlQuery::allQueryItemValues(const QString &key, QUrl::ComponentFormattingOptions encoding) const-
405{-
406 QStringList result;-
407 if (d) {-
408 QString encodedKey = d->recodeFromUser(key);-
409 int idx = d->findRecodedKey(encodedKey);-
410 while (idx < d->itemList.size()) {-
411 result << d->recodeToUser(d->itemList.at(idx).second, encoding);-
412 idx = d->findRecodedKey(encodedKey, idx + 1);-
413 }-
414 }-
415 return result;-
416}-
417void QUrlQuery::removeQueryItem(const QString &key)-
418{-
419 if (d).constData()
d.constData()Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_QUrlQuery
FALSEnever evaluated
) {
0-6
420 Map::iterator it = d->findKey(key);-
421 if (it != d->itemList.end()
it != d->itemList.end()Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • tst_QUrlQuery
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QUrlQuery
)
1-5
422 d->itemList.erase(it);
executed 5 times by 1 test: d->itemList.erase(it);
Executed by:
  • tst_QUrlQuery
5
423 }
executed 6 times by 1 test: end of block
Executed by:
  • tst_QUrlQuery
6
424}
executed 6 times by 1 test: end of block
Executed by:
  • tst_QUrlQuery
6
425-
426-
427-
428-
429-
430-
431-
432void QUrlQuery::removeAllQueryItems(const QString &key)-
433{-
434 if (d.constData()
d.constData()Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • tst_QUrlQuery
FALSEnever evaluated
) {
0-3
435 const QString encodedKey = d->recodeFromUser(key);-
436 Map::iterator itauto firstEqualsEncodedKey = [&encodedKey](const QPair<QString, QString> &item) {-
437 return
executed 14 times by 1 test: return item.first == encodedKey;
Executed by:
  • tst_QUrlQuery
item.first == encodedKey;
executed 14 times by 1 test: return item.first == encodedKey;
Executed by:
  • tst_QUrlQuery
14
438 };-
439 const auto end = d->itemList.beginend();-
440 while (it !=d->itemList.end()) {-
iferase(it->first == encodedKey)
it =std::remove_if(d->itemList.erase(itbegin(), end, firstEqualsEncodedKey), end);else
++it;
}
441 }
executed 3 times by 1 test: end of block
Executed by:
  • tst_QUrlQuery
3
442}
executed 3 times by 1 test: end of block
Executed by:
  • tst_QUrlQuery
3
443-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9