📄 qstring.cpp
字号:
QString("").isNull(); // returns false QString("").isEmpty(); // returns true QString("abc").isNull(); // returns false QString("abc").isEmpty(); // returns false \endcode All functions except isNull() treat null strings the same as empty strings. For example, toAscii().constData() returns a pointer to a '\\0' character for a null string (\e not a null pointer), and QString() compares equal to QString(""). We recommend that you always use isEmpty() and avoid isNull(). \sa fromRawData(), QChar, QLatin1String, QByteArray*//*! \enum QString::SplitBehavior This enum specifies how split() should behave with respect to empty strings. \value KeepEmptyParts If a field is empty, keep it in the result. \value SkipEmptyParts If a field is empty, don't include it in the result. \sa split()*/QString::Data QString::shared_null = { Q_ATOMIC_INIT(1), 0, 0, shared_null.array, 0, 0, 0, 0, 0, {0} };QString::Data QString::shared_empty = { Q_ATOMIC_INIT(1), 0, 0, shared_empty.array, 0, 0, 0, 0, 0, {0} };inline int QString::grow(int size){ return qAllocMore(size * sizeof(QChar), sizeof(Data)) / sizeof(QChar);}/*! \typedef QString::ConstIterator \internal Qt-style synonym for QString::const_iterator.*//*! \typedef QString::Iterator \internal Qt-style synonym for QString::iterator.*//*! \typedef QString::const_iterator \internal The QString::const_iterator typedef provides an STL-style const iterator for QString. \sa QString::iterator*//*! \typedef QString::iterator \internal The QString::iterator typedef provides an STL-style non-const iterator for QString. \sa QString::const_iterator*//*! \fn QString::iterator QString::begin() \internal*//*! \fn QString::const_iterator QString::begin() const \internal*//*! \fn QString::const_iterator QString::constBegin() const \internal*//*! \fn QString::iterator QString::end() \internal*//*! \fn QString::const_iterator QString::end() const \internal*//*! \fn QString::const_iterator QString::constEnd() const \internal*//*! \fn QString::QString() Constructs a null string. Null strings are also empty. \sa isEmpty()*//*! \fn QString::QString(const char *str) Constructs a string initialized with the ASCII string \a str. \a str is converted to Unicode using fromAscii(). You can disable this constructor by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example. \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()*//*! \fn QString QString::fromStdString(const std::string &str) Returns a copy of \a str. \a str is converted to Unicode using fromAscii(). This constructor is only available if Qt is configured with STL compatibility enabled. \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()*/#ifndef QT_NO_STL/*! \fn QString QString::fromStdWString(const std::wstring &str) Returns a copy of \a str. \a str is assumed to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix systems). This constructor is only available if Qt is configured with STL compatibility enabled. \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8()*//*! \internal*/QString QString::fromWCharArray(const wchar_t *a, int l){ QString s; if (sizeof(wchar_t) == sizeof(QChar)) { s = fromUtf16((ushort *)a); } else { s.resize(l*2); // worst case QChar *uc = s.data(); for (int i = 0; i < l; ++i) { uint u = a[i]; if (u > 0xffff) { // decompose into a surrogate pair u -= 0x10000; *uc = QChar(u/0x400 + 0xd800); ++uc; u = u%0x400 + 0xdc00; } *uc = QChar(u); ++uc; } s.resize(uc - s.data()); } return s;}/*! \fn std::wstring QString::toStdWString() const Returns a std::wstring object with the data contained in this QString. The std::wstring is encoded in utf16 on platforms where wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms where wchar_t is 4 bytes wide (most Unix systems). This operator is mostly useful to pass a QString to a function that accepts a std::wstring object. This operator is only available if Qt is configured with STL compatibility enabled. \sa utf16(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit()*//*! \internal*/int QString::toWCharArray(wchar_t *array) const{ if (sizeof(wchar_t) == sizeof(QChar)) { memcpy(array, utf16(), sizeof(wchar_t)*length()); return length(); } else { wchar_t *a = array; const unsigned short *uc = utf16(); for (int i = 0; i < length(); ++i) { uint u = uc[i]; if (u >= 0xd800 && u < 0xdc00 && i < length()-1) { ushort low = uc[i+1]; if (low >= 0xdc00 && low < 0xe000) { ++i; u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000; } } *a = wchar_t(u); ++a; } return a - array; }}#endif/*! \fn QString::QString(const QString &other) Constructs a copy of \a other. This operation takes \l{constant time}, because QString is \l{implicitly shared}. This makes returning a QString from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes \l{linear time}. \sa operator=()*//*! Constructs a string initialized with the first \a size characters of the QChar array \a unicode. QString makes a deep copy of the string data.*/QString::QString(const QChar *unicode, int size){ if (!unicode) { d = &shared_null; d->ref.ref(); } else if (size <= 0) { d = &shared_empty; d->ref.ref(); } else { d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar)); d->ref.init(1); d->alloc = d->size = size; d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0; d->data = d->array; memcpy(d->array, unicode, size * sizeof(QChar)); d->array[size] = '\0'; }}/*! Constructs a string of size \a size with every character set to \a ch. \sa fill()*/QString::QString(int size, QChar ch){ if (size <= 0) { d = &shared_empty; d->ref.ref(); } else { d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar)); d->ref.init(1); d->alloc = d->size = size; d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0; d->data = d->array; d->array[size] = '\0'; QChar *i = (QChar*)d->array + size; QChar *b = (QChar*)d->array; while (i != b) *--i = ch; }}/*! \fn QString::QString(const QLatin1String &str) Constructs a copy of the Latin-1 string \a str. \sa fromLatin1()*//*! Constructs a string of size 1 containing the character \a ch.*/QString::QString(QChar ch){ d = (Data *)qMalloc(sizeof(Data) + sizeof(QChar)); d->ref.init(1); d->alloc = d->size = 1; d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0; d->data = d->array; d->array[0] = ch.unicode(); d->array[1] = '\0';}/*! \fn QString::QString(const QByteArray &ba) Constructs a string initialized with the byte array \a ba. \a ba is converted to Unicode using fromAscii(). Stops copying at the first 0 character, otherwise copies the entire byte array. You can disable this constructor by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn QString::QString(const Null &) \internal*//*! \fn QString &QString::operator=(const Null &) \internal*//*! \fn QString::~QString() Destroys the string.*//*! \fn void QString::detach() \internal*//*! \fn void QString::isDetached() const \internal*/void QString::free(Data *d){#ifdef QT3_SUPPORT if (d->asciiCache) { Q_ASSERT(asciiCache); asciiCache->remove(d); }#endif qFree(d);}/*! Sets the size of the string to \a size characters. If \a size is greater than the current size, the string is extended to make it \a size characters long with the extra characters added to the end. The new characters are uninitialized. If \a size is less than the current size, characters are removed from the end. Example: \code QString str = "Hello world"; str.resize(5); // str == "Hello" str.resize(8); // str == "Hello???" (where ? stands for any character) \endcode If you want to append a certain number of identical characters to the string, use operator+=() as follows rather than resize(): \code QString str = "Hello"; str += QString(10, 'X'); // str == "HelloXXXXXXXXXX" \endcode If you want to expand the string so that it reaches a certain width and fill the new positions with a particular character, use leftJustified(): \code QString str = "Hello"; str = str.leftJustified(10, ' '); // str == "Hello " \endcode \sa truncate(), reserve()*/void QString::resize(int size){ if (size <= 0) { Data *x = &shared_empty; x->ref.ref(); x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) free(x); } else { if (d->ref != 1 || size > d->alloc || (size < d->size && size < d->alloc >> 1)) realloc(grow(size)); if (d->alloc >= size) { d->size = size; d->data = d->array; d->array[size] = '\0'; } }}/*! \fn int QString::capacity() const Returns the maximum number of characters that can be stored in the string without forcing a reallocation. The sole purpose of this function is to provide a means of fine tuning QString's memory usage. In general, you will rarely ever need to call this function. If you want to know how many characters are in the string, call size(). \sa reserve(), squeeze()*//*! \fn void QString::reserve(int size) Attempts to allocate memory for at least \a size characters. If you know in advance how large the string will be, you can call this function, and if you resize the string often you are likely to get better performance. If \a size is an underestimate, the worst that will happen is that the QString will be a bit slower. The sole purpose of this function is to provide a means of fine tuning QString's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the string, call resize(). This function is useful for code that needs to build up a long string and wants to avoid repeated reallocation. In this example, we want to add to the string until some condition is true, and we're fairly sure that size is large enough to make a call to reserve() worthwhile: \code QString result; int len = 0; result.reserve(maxSize); while (...) { result[len++] = getNextChar(); // fill part of the space } result.squeeze();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -