QtBase  v6.3.1
proitems.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the qmake application of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "proitems.h"
30 
31 #include <qfileinfo.h>
32 #include <qset.h>
33 #include <qstringlist.h>
34 #include <qtextstream.h>
35 #include <private/qduplicatetracker_p.h>
36 
38 
39 // from qhash.cpp
40 size_t ProString::hash(const QChar *p, int n)
41 {
42  size_t h = 0;
43 
44  while (n--) {
45  h = (h << 4) + (*p++).unicode();
46  h ^= (h & 0xf0000000) >> 23;
47  h &= 0x0fffffff;
48  }
49  return h;
50 }
51 
53  m_offset(0), m_length(0), m_file(0), m_hash(0x80000000)
54 {
55 }
56 
58  m_string(other.m_string), m_offset(other.m_offset), m_length(other.m_length), m_file(other.m_file), m_hash(other.m_hash)
59 {
60 }
61 
62 ProString::ProString(const ProString &other, OmitPreHashing) :
63  m_string(other.m_string), m_offset(other.m_offset), m_length(other.m_length), m_file(other.m_file), m_hash(0x80000000)
64 {
65 }
66 
67 ProString::ProString(const QString &str, DoPreHashing) :
68  m_string(str), m_offset(0), m_length(str.length()), m_file(0)
69 {
70  updatedHash();
71 }
72 
74  m_string(str), m_offset(0), m_length(str.length()), m_file(0), m_hash(0x80000000)
75 {
76 }
77 
79  m_string(str.toString()), m_offset(0), m_length(str.size()), m_file(0), m_hash(0x80000000)
80 {
81 }
82 
83 ProString::ProString(const char *str, DoPreHashing) :
84  m_string(QString::fromLatin1(str)), m_offset(0), m_length(int(qstrlen(str))), m_file(0)
85 {
86  updatedHash();
87 }
88 
89 ProString::ProString(const char *str) :
90  m_string(QString::fromLatin1(str)), m_offset(0), m_length(int(qstrlen(str))), m_file(0), m_hash(0x80000000)
91 {
92 }
93 
94 ProString::ProString(const QString &str, int offset, int length, DoPreHashing) :
95  m_string(str), m_offset(offset), m_length(length), m_file(0)
96 {
97  updatedHash();
98 }
99 
101  m_string(str), m_offset(offset), m_length(length), m_file(0), m_hash(hash)
102 {
103 }
104 
106  m_string(str), m_offset(offset), m_length(length), m_file(0), m_hash(0x80000000)
107 {
108 }
109 
111 {
112  m_string = str, m_offset = 0, m_length = str.length(), m_hash = 0x80000000;
113 }
114 
115 size_t ProString::updatedHash() const
116 {
117  return (m_hash = hash(m_string.constData() + m_offset, m_length));
118 }
119 
120 size_t qHash(const ProString &str)
121 {
122  if (!(str.m_hash & 0x80000000))
123  return str.m_hash;
124  return str.updatedHash();
125 }
126 
128  ProString(str, DoHash)
129 {
130 }
131 
132 ProKey::ProKey(const char *str) :
133  ProString(str, DoHash)
134 {
135 }
136 
137 ProKey::ProKey(const QString &str, int off, int len) :
138  ProString(str, off, len, DoHash)
139 {
140 }
141 
142 ProKey::ProKey(const QString &str, int off, int len, uint hash) :
143  ProString(str, off, len, hash)
144 {
145 }
146 
148 {
149  m_string = str, m_offset = 0, m_length = str.length();
150  updatedHash();
151 }
152 
154 {
155  return m_string.mid(m_offset, m_length);
156 }
157 
159 {
160  tmp = m_string.mid(m_offset, m_length);
161  return tmp;
162 }
163 
165 {
166  if (other.m_length) {
167  if (!m_length) {
168  *this = other;
169  } else {
170  m_string = other.toQStringView() + toQStringView();
171  m_offset = 0;
172  m_length = m_string.length();
173  if (!m_file)
174  m_file = other.m_file;
175  m_hash = 0x80000000;
176  }
177  }
178  return *this;
179 }
180 
182 {
183  if (other.size()) {
184  if (m_length != m_string.length()) {
185  m_string = toQStringView() + other;
186  m_offset = 0;
187  m_length = m_string.length();
188  } else {
189  Q_ASSERT(m_offset == 0);
190  m_string.append(other);
191  m_length += other.size();
192  }
193  m_hash = 0x80000000;
194  }
195  return *this;
196 }
197 
199 {
200  if (m_length != m_string.length()) {
201  m_string = toQStringView() + other;
202  m_offset = 0;
203  m_length = m_string.length();
204  } else {
205  Q_ASSERT(m_offset == 0);
206  m_string.append(other);
207  ++m_length;
208  }
209  m_hash = 0x80000000;
210  return *this;
211 }
212 
213 // If pending != 0, prefix with space if appending to non-empty non-pending
215 {
216  if (other.m_length) {
217  if (!m_length) {
218  *this = other;
219  } else {
220  if (m_length != m_string.length())
221  m_string = toQString();
222  if (pending && !*pending) {
223  m_string += QLatin1Char(' ') + other.toQStringView();
224  } else {
225  m_string += other.toQStringView();
226  }
227  m_length = m_string.length();
228  m_offset = 0;
229  if (other.m_file)
230  m_file = other.m_file;
231  m_hash = 0x80000000;
232  }
233  if (pending)
234  *pending = true;
235  }
236  return *this;
237 }
238 
239 ProString &ProString::append(const ProStringList &other, bool *pending, bool skipEmpty1st)
240 {
241  if (const int sz = other.size()) {
242  int startIdx = 0;
243  if (pending && !*pending && skipEmpty1st && other.at(0).isEmpty()) {
244  if (sz == 1)
245  return *this;
246  startIdx = 1;
247  }
248  if (!m_length && sz == startIdx + 1) {
249  *this = other.at(startIdx);
250  } else {
251  int totalLength = sz - startIdx;
252  for (int i = startIdx; i < sz; ++i)
253  totalLength += other.at(i).size();
254  bool putSpace = false;
255  if (pending && !*pending && m_length)
256  putSpace = true;
257  else
258  totalLength--;
259 
260  m_string = toQString();
261  m_offset = 0;
262  for (int i = startIdx; i < sz; ++i) {
263  if (putSpace)
264  m_string += QLatin1Char(' ');
265  else
266  putSpace = true;
267  const ProString &str = other.at(i);
268  m_string += str.toQStringView();
269  }
270  m_length = m_string.length();
271  if (other.last().m_file)
272  m_file = other.last().m_file;
273  m_hash = 0x80000000;
274  }
275  if (pending)
276  *pending = true;
277  }
278  return *this;
279 }
280 
281 QString operator+(const ProString &one, const ProString &two)
282 {
283  if (two.m_length) {
284  if (!one.m_length) {
285  return two.toQString();
286  } else {
287  QString neu(one.m_length + two.m_length, Qt::Uninitialized);
288  ushort *ptr = (ushort *)neu.constData();
289  memcpy(ptr, one.m_string.constData() + one.m_offset, one.m_length * 2);
290  memcpy(ptr + one.m_length, two.m_string.constData() + two.m_offset, two.m_length * 2);
291  return neu;
292  }
293  }
294  return one.toQString();
295 }
296 
297 
298 ProString ProString::mid(int off, int len) const
299 {
300  ProString ret(*this, NoHash);
301  if (off > m_length)
302  off = m_length;
303  ret.m_offset += off;
304  ret.m_length -= off;
305  if ((uint)ret.m_length > (uint)len) // Unsigned comparison to interpret < 0 as infinite
306  ret.m_length = len;
307  return ret;
308 }
309 
311 {
312  ProString ret(*this, NoHash);
313  int cur = m_offset;
314  int end = cur + m_length;
315  const QChar *data = m_string.constData();
316  for (; cur < end; cur++)
317  if (!data[cur].isSpace()) {
318  // No underrun check - we know there is at least one non-whitespace
319  while (data[end - 1].isSpace())
320  end--;
321  break;
322  }
323  ret.m_offset = cur;
324  ret.m_length = end - cur;
325  return ret;
326 }
327 
329 {
330  t << str.toQStringView();
331  return t;
332 }
333 
334 static QString ProStringList_join(const ProStringList &this_, const QChar *sep, const int sepSize)
335 {
336  int totalLength = 0;
337  const int sz = this_.size();
338 
339  for (int i = 0; i < sz; ++i)
340  totalLength += this_.at(i).size();
341 
342  if (sz)
343  totalLength += sepSize * (sz - 1);
344 
345  QString res(totalLength, Qt::Uninitialized);
346  QChar *ptr = (QChar *)res.constData();
347  for (int i = 0; i < sz; ++i) {
348  if (i) {
349  memcpy(ptr, sep, sepSize * sizeof(QChar));
350  ptr += sepSize;
351  }
352  const ProString &str = this_.at(i);
353  memcpy(ptr, str.constData(), str.size() * sizeof(QChar));
354  ptr += str.size();
355  }
356  return res;
357 }
358 
360 {
361  return ProStringList_join(*this, sep.constData(), sep.size());
362 }
363 
365 {
366  return ProStringList_join(*this, sep.constData(), sep.size());
367 }
368 
370 {
371  return ProStringList_join(*this, &sep, 1);
372 }
373 
375 {
376  for (int i = size(); --i >= 0; )
377  if (at(i) == str)
378  remove(i);
379 }
380 
381 void ProStringList::removeAll(const char *str)
382 {
383  for (int i = size(); --i >= 0; )
384  if (at(i) == str)
385  remove(i);
386 }
387 
389 {
390  for (const ProString &str : value) {
391  if (isEmpty())
392  break;
393  if (!str.isEmpty())
394  removeAll(str);
395  }
396 }
397 
399 {
400  for (int i = size(); --i >= 0;)
401  if (at(i).isEmpty())
402  remove(i);
403 }
404 
406 {
408  removeIf([&](const ProString &s) { return seen.hasSeen(s); });
409 }
410 
412 {
413  for (const ProString &str : value)
414  if (!str.isEmpty() && !contains(str))
415  append(str);
416 }
417 
419 {
420  reserve(list.size());
421  for (const QString &str : list)
422  *this << ProString(str);
423 }
424 
426 {
428  ret.reserve(size());
429  for (const auto &e : *this)
430  ret.append(e.toQString());
431  return ret;
432 }
433 
435 {
436  for (int i = 0; i < size(); i++)
437  if (!at(i).compare(str, cs))
438  return true;
439  return false;
440 }
441 
443 {
444  for (int i = 0; i < size(); i++)
445  if (!at(i).toQStringView().compare(str, cs))
446  return true;
447  return false;
448 }
449 
451 {
452  for (int i = 0; i < size(); i++)
453  if (!at(i).compare(str, cs))
454  return true;
455  return false;
456 }
457 
459  : m_refCount(1),
460  m_fileName(fileName),
461  m_id(id),
462  m_ok(true),
463  m_hostBuild(false)
464 {
465  if (!fileName.startsWith(QLatin1Char('(')))
466  m_directoryName = QFileInfo( // qmake sickness: canonicalize only the directory!
468 }
469 
471 {
472 }
473 
475 {
476  uint len = *tPtr++;
477  ProString ret(items(), tPtr - tokPtr(), len);
478  ret.setSource(m_id);
479  tPtr += len;
480  return ret;
481 }
482 
484 {
485  uint hash = *tPtr++;
486  hash |= (uint)*tPtr++ << 16;
487  uint len = *tPtr++;
488  ProKey ret(items(), tPtr - tokPtr(), len, hash);
489  tPtr += len;
490  return ret;
491 }
492 
494 {
495  return debug << str.toQString();
496 }
497 
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
QString fileName() const
Definition: proitems.h:437
const ushort * tokPtr() const
Definition: proitems.h:441
ProFile(int id, const QString &fileName)
Definition: proitems.cpp:458
ProString getStr(const ushort *&tPtr)
Definition: proitems.cpp:474
const QString & items() const
Definition: proitems.h:439
ProKey getHashStr(const ushort *&tPtr)
Definition: proitems.cpp:483
~ProFile()
Definition: proitems.cpp:470
void setValue(const QString &str)
Definition: proitems.cpp:147
ALWAYS_INLINE ProKey()
Definition: proitems.h:198
ProString mid(int off, int len=-1) const
Definition: proitems.cpp:298
QString toQString() const
Definition: proitems.cpp:153
ALWAYS_INLINE QStringView toQStringView() const
Definition: proitems.h:161
void setValue(const QString &str)
Definition: proitems.cpp:110
const QChar * constData() const
Definition: proitems.h:125
int size() const
Definition: proitems.h:123
size_t hash() const
Definition: proitems.h:158
int compare(const ProString &sub, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: proitems.h:130
ProString & prepend(const ProString &other)
Definition: proitems.cpp:164
ProString trimmed() const
Definition: proitems.cpp:310
ProString & append(const ProString &other, bool *pending=nullptr)
Definition: proitems.cpp:214
void removeAll(const ProString &str)
Definition: proitems.cpp:374
QString join(const ProString &sep) const
Definition: proitems.cpp:359
void insertUnique(const ProStringList &value)
Definition: proitems.cpp:411
void removeEmpty()
Definition: proitems.cpp:398
void removeEach(const ProStringList &value)
Definition: proitems.cpp:388
void removeDuplicates()
Definition: proitems.cpp:405
bool contains(const ProString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: proitems.cpp:434
QStringList toQStringList() const
Definition: proitems.cpp:425
size_t qstrlen(const char *str)
const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:575
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:84
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
bool hasSeen(const T &s)
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:57
QString canonicalFilePath() const
Definition: qfileinfo.cpp:573
template< typename Enum > size_t qHash(QFlags< Enum > flags, size_t seed=0) noexcept
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
qsizetype size() const noexcept
Definition: qlist.h:414
bool isEmpty() const noexcept
Definition: qlist.h:418
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
void remove(qsizetype i, qsizetype n=1)
Definition: qlist.h:798
qsizetype removeIf(Predicate pred)
Definition: qlist.h:602
void reserve(qsizetype size)
Definition: qlist.h:757
void append(parameter_type t)
Definition: qlist.h:469
The QString class provides a Unicode character string.
Definition: qstring.h:388
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition: qstring.h:514
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:5092
const QChar * constData() const
Definition: qstring.h:1234
qsizetype size() const
Definition: qstring.h:413
QString mid(qsizetype position, qsizetype n=-1) const
Definition: qstring.cpp:4994
bool isEmpty() const
Definition: qstring.h:1216
QString & append(QChar c)
Definition: qstring.cpp:3152
QString left(qsizetype n) const
Definition: qstring.cpp:4951
qsizetype length() const
Definition: qstring.h:415
The QStringList class provides a list of strings.
The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QStrin...
Definition: qstringview.h:122
int compare(QStringView other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition: qstringview.h:304
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:62
QHash< int, QWidget * > hash
[35multi]
QString str
[2]
double e
#define true
Definition: ftrandom.c:51
CaseSensitivity
Definition: qnamespace.h:1282
constexpr Initialization Uninitialized
Definition: qnamespace.h:1613
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall * pending
EGLOutputLayerEXT EGLint EGLAttrib value
unsigned int uint
Definition: qglobal.h:334
unsigned short ushort
Definition: qglobal.h:333
GLenum GLuint GLenum GLsizei length
Definition: qopengl.h:270
GLenum GLuint id
[6]
Definition: qopengl.h:270
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLuint end
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLfloat n
GLfloat GLfloat GLfloat GLfloat h
GLuint res
Definition: qopenglext.h:8867
GLenum GLsizei len
Definition: qopenglext.h:3292
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLdouble s
[6]
Definition: qopenglext.h:235
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
QSharedPointer< T > other(t)
[5]
QStringList list
[0]
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:53