📄 qstring.cpp
字号:
numeric Unicode values of the characters. It is very fast, but is not what a human would expect; the QString::localeAwareCompare() function is a better choice for sorting user-interface strings. To obtain a pointer to the actual character data, call data() or constData(). These functions return a pointer to the beginning of the QChar data. The pointer is guaranteed to remain valid until a non-const function is called on the QString. \section1 Converting Between 8-Bit Strings and Unicode Strings QString provides the following four functions that return a \c{const char *} version of the string as QByteArray: toAscii(), toLatin1(), toUtf8(), and toLocal8Bit(). \list \o toAscii() returns an ASCII encoded 8-bit string. \o toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string. \o toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a superset of ASCII that supports the entire Unicode character set through multibyte sequences. \o toLocal8Bit() returns an 8-bit string using the system's local encoding. \endlist To convert from one of these encodings, QString provides fromAscii(), fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other encodings are supported through the QTextCodec class. As mentioned above, QString provides a lot of functions and operators that make it easy to interoperate with \c{const char *} strings. But this functionality is a double-edged sword: It makes QString more convenient to use if all strings are ASCII or Latin-1, but there is always the risk that an implicit conversion from or to \c{const char *} is done using the wrong 8-bit encoding. To minimize these risks, you can turn off these implicit conversions by defining the following two preprocessor symbols: \list \o \c QT_NO_CAST_FROM_ASCII disables automatic conversions from ASCII to Unicode. \o \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString to ASCII. \endlist One way to define these preprocessor symbols globally for your application is to add the following entry to your \l{qmake Project Files}{qmake project file}: \code DEFINES += QT_NO_CAST_FROM_ASCII \ QT_NO_CAST_TO_ASCII \endcode You then need to explicitly call fromAscii(), fromLatin1(), fromUtf8(), or fromLocal8Bit() to construct a QString from an 8-bit string, or use the lightweight QLatin1String class, for example: \code QString url = QLatin1String("http://www.unicode.org/"); \endcode Similarly, you must call toAscii(), toLatin1(), toUtf8(), or toLocal8Bit() explicitly to convert the QString to an 8-bit string. (Other encodings are supported through the QTextCodec class.) \table 100 % \row \o \section1 Note for C Programmers Due to C++'s type system and the fact that QString is \l{implicitly shared}, QStrings may be treated like \c{int}s or other basic types. For example: \skipto boolToString(bool b) \printuntil } The \c result variable, is a normal variable allocated on the stack. When \c return is called, and because we're returning by value, the copy constructor is called and a copy of the string is returned. No actual copying takes place thanks to the implicit sharing. \endtable \section1 Distinction Between Null and Empty Strings For historical reasons, QString distinguishes between a null string and an empty string. A \e null string is a string that is initialized using QString's default constructor or by passing (const char *)0 to the constructor. An \e empty string is any string with size 0. A null string is always empty, but an empty string isn't necessarily null: \quotefromfile snippets/qstring/main.cpp \skipto QString().isNull() \printuntil QString("abc").isEmpty() 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 the isEmpty() function and avoid isNull(). \sa fromRawData(), QChar, QLatin1String, QByteArray, QStringRef*//*! \enum QString::SplitBehavior This enum specifies how the split() function 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, {0} };QString::Data QString::shared_empty = { Q_ATOMIC_INIT(1), 0, 0, shared_empty.array, 0, 0, 0, 0, 0, 0, {0} };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. The given const char pointer is converted to Unicode using the fromAscii() function. 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 the \a str string. The given string is converted to Unicode using the fromAscii() function. This constructor is only available if Qt is configured with STL compatibility enabled. \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()*//*! \fn QString QString::fromStdWString(const std::wstring &str) Returns a copy of the \a str string. The given string 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 method is only available if Qt is configured with STL compatibility enabled. \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4()*//*! \since 4.2 Returns a copy of the \a string string encoded in ucs4. If \a size is -1 (the default), the \a string has to be 0 terminated. \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdWString()*/QString QString::fromWCharArray(const wchar_t *string, int size){ if (sizeof(wchar_t) == sizeof(QChar)) { return fromUtf16((ushort *)string, size); } else { return fromUcs4((uint *)string, size); }}/*! \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()*//*! \since 4.2 Fills the \a array with the data contained in this QString object. The array 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). \a array has to be allocated by the caller and contain enough space to hold the complete string (allocating the array with the same length as the string is always sufficient). returns the actual length of the string in \a array. \sa utf16(), toUcs4(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString()*/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; }}/*! \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 = d->capacity = 0; d->data = d->array; memcpy(d->array, unicode, size * sizeof(QChar)); d->array[size] = '\0'; }}/*! Constructs a string of the given \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 = d->capacity = 0; d->data = d->array; d->array[size] = '\0'; ushort *i = d->array + size; ushort *b = d->array; const ushort value = ch.unicode(); while (i != b) *--i = value; }}/*! \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 = d->capacity = 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. The given byte array 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. \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()*//*! \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); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -