📄 qstring.cpp
字号:
} } ligature.next(); } cindex++; index++; } *this = composed;#endif}// These macros are used for efficient allocation of QChar strings.// IMPORTANT! If you change these, make sure you also change the// "delete unicode" statement in ~QStringData() in qstring.h correspondingly!#define QT_ALLOC_QCHAR_VEC( N ) (QChar*) new char[ sizeof(QChar)*( N ) ]#define QT_DELETE_QCHAR_VEC( P ) delete[] ((char*)( P ))/*! This utility function converts the 8-bit string \a ba to Unicode, returning the result. The caller is responsible for deleting the return value with delete[].*/QChar* QString::latin1ToUnicode( const QByteArray& ba, uint* len ){ if ( ba.isNull() ) { *len = 0; return 0; } int l = 0; while ( l < (int)ba.size() && ba[l] ) l++; char* str = ba.data(); QChar *uc = new QChar[ l ]; // Can't use macro, since function is public QChar *result = uc; if ( len ) *len = l; while (l--) *uc++ = *str++; return result;}static QChar* internalLatin1ToUnicode( const QByteArray& ba, uint* len ){ if ( ba.isNull() ) { *len = 0; return 0; } int l = 0; while ( l < (int)ba.size() && ba[l] ) l++; char* str = ba.data(); QChar *uc = QT_ALLOC_QCHAR_VEC( l ); QChar *result = uc; if ( len ) *len = l; while (l--) *uc++ = *str++; return result;}/*! \overload This utility function converts the '\0'-terminated 8-bit string \a str to Unicode, returning the result and setting \a *len to the length of the Unicode string. The caller is responsible for deleting the return value with delete[].*/QChar* QString::latin1ToUnicode( const char *str, uint* len, uint maxlen ){ QChar* result = 0; uint l = 0; if ( str ) { if ( maxlen != (uint)-1 ) { while ( l < maxlen && str[l] ) l++; } else { // Faster? l = strlen( str ); } QChar *uc = new QChar[ l ]; // Can't use macro since function is public result = uc; uint i = l; while ( i-- ) *uc++ = *str++; } if ( len ) *len = l; return result;}static QChar* internalLatin1ToUnicode( const char *str, uint* len, uint maxlen = (uint)-1 ){ QChar* result = 0; uint l = 0; if ( str ) { if ( maxlen != (uint)-1 ) { while ( l < maxlen && str[l] ) l++; } else { // Faster? l = strlen( str ); } QChar *uc = QT_ALLOC_QCHAR_VEC( l ); result = uc; uint i = l; while ( i-- ) *uc++ = *str++; } if ( len ) *len = l; return result;}/*! This utility function converts \a l 16-bit characters from \a uc to ASCII, returning a '\0'-terminated string. The caller is responsible for deleting the resultant string with delete[].*/char* QString::unicodeToLatin1(const QChar *uc, uint l){ if (!uc) { return 0; } char *a = new char[l+1]; char *result = a; while (l--) { *a++ = (uc->unicode() > 0xff) ? '?' : (char)uc->unicode(); uc++; } *a = '\0'; return result;}/***************************************************************************** QString member functions *****************************************************************************//*! \class QString qstring.h \reentrant \brief The QString class provides an abstraction of Unicode text and the classic C '\0'-terminated char array. \ingroup tools \ingroup shared \ingroup text \mainclass QString uses \link shclass.html implicit sharing\endlink, which makes it very efficient and easy to use. In all of the QString methods that take \c {const char *} parameters, the \c {const char *} is interpreted as a classic C-style '\0'-terminated ASCII string. It is legal for the \c {const char *} parameter to be 0. If the \c {const char *} is not '\0'-terminated, the results are undefined. Functions that copy classic C strings into a QString will not copy the terminating '\0' character. The QChar array of the QString (as returned by unicode()) is generally not terminated by a '\0'. If you need to pass a QString to a function that requires a C '\0'-terminated string use latin1(). \keyword QString::null A QString that has not been assigned to anything is \e null, i.e. both the length and data pointer is 0. A QString that references the empty string ("", a single '\0' char) is \e empty. Both null and empty QStrings are legal parameters to the methods. Assigning \c{(const char *) 0} to QString gives a null QString. For convenience, \c QString::null is a null QString. When sorting, empty strings come first, followed by non-empty strings, followed by null strings. We recommend using \c{if ( !str.isNull() )} to check for a non-null string rather than \c{if ( !str )}; see \l operator!() for an explanation. Note that if you find that you are mixing usage of \l QCString, QString, and \l QByteArray, this causes lots of unnecessary copying and might indicate that the true nature of the data you are dealing with is uncertain. If the data is '\0'-terminated 8-bit data, use \l QCString; if it is unterminated (i.e. contains '\0's) 8-bit data, use \l QByteArray; if it is text, use QString. Lists of strings are handled by the QStringList class. You can split a string into a list of strings using QStringList::split(), and join a list of strings into a single string with an optional separator using QStringList::join(). You can obtain a list of strings from a string list that contain a particular substring or that match a particular \link qregexp.html regex\endlink using QStringList::grep(). <b>Note for C programmers</b> Due to C++'s type system and the fact that QString is implicitly shared, QStrings may be treated like ints or other simple base types. For example: \code QString boolToString( bool b ) { QString result; if ( b ) result = "True"; else result = "False"; return result; } \endcode The variable, result, is an auto variable allocated on the stack. When return is called, 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, see below.) Throughout Qt's source code you will encounter QString usages like this: \code QString func( const QString& input ) { QString output = input; // process output return output; } \endcode The 'copying' of input to output is almost as fast as copying a pointer because behind the scenes copying is achieved by incrementing a reference count. QString (like all Qt's implicitly shared classes) operates on a copy-on-write basis, only copying if an instance is actually changed. If you wish to create a deep copy of a QString without losing any Unicode information then you should use QDeepCopy. \sa QChar QCString QByteArray QConstString*//*! \enum Qt::ComparisonFlags\internal*//*! \enum Qt::StringComparisonMode This enum type is used to set the string comparison mode when searching for an item. It is used by QListBox, QListView and QIconView, for example. We'll refer to the string being searched as the 'target' string. \value CaseSensitive The strings must match case sensitively. \value ExactMatch The target and search strings must match exactly. \value BeginsWith The target string begins with the search string. \value EndsWith The target string ends with the search string. \value Contains The target string contains the search string. If you OR these flags together (excluding \c CaseSensitive), the search criteria be applied in the following order: \c ExactMatch, \c BeginsWith, \c EndsWith, \c Contains. Matching is case-insensitive unless \c CaseSensitive is set. \c CaseSensitive may be OR-ed with any combination of the other flags.*/Q_EXPORT QStringData *QString::shared_null = 0;QT_STATIC_CONST_IMPL QString QString::null;QT_STATIC_CONST_IMPL QChar QChar::null;QT_STATIC_CONST_IMPL QChar QChar::replacement((ushort)0xfffd);QT_STATIC_CONST_IMPL QChar QChar::byteOrderMark((ushort)0xfeff);QT_STATIC_CONST_IMPL QChar QChar::byteOrderSwapped((ushort)0xfffe);QT_STATIC_CONST_IMPL QChar QChar::nbsp((ushort)0x00a0);QStringData* QString::makeSharedNull(){ QString::shared_null = new QStringData;#if defined( Q_OS_MAC ) QString *that = const_cast<QString *>(&QString::null); that->d = QString::shared_null;#endif return QString::shared_null;}/*! \fn QString::QString() Constructs a null string, i.e. both the length and data pointer are 0. \sa isNull()*//*! Constructs a string of length one, containing the character \a ch.*/QString::QString( QChar ch ){ d = new QStringData( QT_ALLOC_QCHAR_VEC( 1 ), 1, 1 ); d->unicode[0] = ch;}/*! Constructs an implicitly shared copy of \a s. This is very fast since it only involves incrementing a reference count.*/QString::QString( const QString &s ) : d(s.d){ d->ref();}/*! \internal Private function. Constructs a string with preallocated space for \a size characters. The string is empty. \sa isNull()*/QString::QString( int size, bool /*dummy*/ ){ if ( size ) { int l = size; QChar* uc = QT_ALLOC_QCHAR_VEC( l ); d = new QStringData( uc, 0, l ); } else { d = shared_null ? shared_null : (shared_null=new QStringData); d->ref(); }}/*! Constructs a string that is a deep copy of \a ba interpreted as a classic C string.*/QString::QString( const QByteArray& ba ){#ifndef QT_NO_TEXTCODEC if ( QTextCodec::codecForCStrings() ) { d = 0; *this = fromAscii( ba.data(), ba.size() ); return; }#endif uint l; QChar *uc = internalLatin1ToUnicode(ba,&l); d = new QStringData(uc,l,l);}/*! Constructs a string that is a deep copy of the first \a length characters in the QChar array. If \a unicode and \a length are 0, then a null string is created. If only \a unicode is 0, the string is empty but has \a length characters of space preallocated: QString expands automatically anyway, but this may speed up some cases a little. We recommend using the plain constructor and setLength() for this purpose since it will result in more readable code. \sa isNull() setLength()*/QString::QString( const QChar* unicode, uint length ){ if ( !unicode && !length ) { d = shared_null ? shared_null : makeSharedNull(); d->ref(); } else { QChar* uc = QT_ALLOC_QCHAR_VEC( length ); if ( unicode ) memcpy(uc, unicode, length*sizeof(QChar)); d = new QStringData(uc,unicode ? length : 0,length); }}/*! Constructs a string that is a deep copy of \a str, interpreted as a classic C string. If \a str is 0, then a null string is created. This is a cast constructor, but it is perfectly safe: converting a Latin-1 \c{const char *} to QString preserves all the information. You can disable this constructor by defining \c QT_NO_CAST_ASCII when you compile your applications. You can also make QString objects by using setLatin1(), fromLatin1(), fromLocal8Bit(), and fromUtf8(). Or whatever encoding is appropriate for the 8-bit data you have. \sa isNull()*/QString::QString( const char *str ){#ifndef QT_NO_TEXTCODEC if ( QTextCodec::codecForCStrings() ) { d = 0; *this = fromAscii( str ); return; }#endif uint l; QChar *uc = internalLatin1ToUnicode(str,&l); d = new QStringData(uc,l,l);}#ifndef QT_NO_STL/*! Constructs a string that is a deep copy of \a str. This is the same as fromAscii(\a str).*/QString::QString( const std::string &str ){#ifndef QT_NO_TEXTCODEC if ( QTextCodec::codecForCStrings() ) { d = 0; *this = fromAscii( str.c_str() ); return; }#endif uint l; QChar *uc = internalLatin1ToUnicode(str.c_str(),&l); d = new QStringData(uc,l,l);}#endif/*! \fn QString::~QString() Destroys the string and frees the string's data if this is the last reference to the string.*//*! Deallocates any space reserved solely by this QString. If the string does not share its data with another QString instance, nothing happens; otherwise the function creates a new, unique copy of this string. This function is called whenever the string is modified.*/void QString::real_detach(){ setLength( length() );}void QString::deref(){ if ( d && d->deref() ) { if ( d != shared_null ) delete d; d = 0; }}void QStringData::deleteSelf(){ delete this;}/*! \fn QString& QString::operator=( QChar c ) Sets the string to contain just the single character \a c.*//*! \fn QString& QString::operator=( const std::string& s ) \overload Makes a deep copy of \a s and returns a reference to the deep copy.*//*! \fn QString& QString::operator=( char c ) \overload Sets the string to contain just the single character \a c.*//*! \overload Assigns a shallow copy of \a s to this string and returns a reference to this string. This is very fast because the string isn't actually copied.*/QString &QString::operator=( const QString &s ){ s.d->ref(); deref(); d = s.d; return *this;}/*! \overload Assigns a deep copy of \a cstr, interpreted as a classic C string, to this string. Returns a reference to this string.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -