📄 qcstring.cpp
字号:
/***************************************************************************** QByteArray stream functions *****************************************************************************//*! \relates QMemArray Writes byte array \a a to the stream \a s and returns a reference to the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/#ifndef QT_NO_DATASTREAMQDataStream &operator<<( QDataStream &s, const QByteArray &a ){ return s.writeBytes( a.data(), a.size() );}/*! \relates QMemArray Reads a byte array into \a a from the stream \a s and returns a reference to the stream. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator>>( QDataStream &s, QByteArray &a ){ Q_UINT32 len; s >> len; // read size of array if ( len == 0 || s.eof() ) { // end of file reached a.resize( 0 ); return s; } if ( !a.resize( (uint)len ) ) { // resize array#if defined(QT_CHECK_NULL) qWarning( "QDataStream: Not enough memory to read QByteArray" );#endif len = 0; } if ( len > 0 ) // not null array s.readRawBytes( a.data(), (uint)len ); return s;}#endif //QT_NO_DATASTREAM/***************************************************************************** QCString member functions *****************************************************************************//*! \class QCString qcstring.h \reentrant \brief The QCString class provides an abstraction of the classic C zero-terminated char array (char *). \ingroup text \ingroup collection \ingroup tools \ingroup shared QCString inherits QByteArray, which is defined as QMemArray\<char\>. Since QCString is a QMemArray, it uses \link shclass.html explicit sharing\endlink with a reference count. QCString tries to behave like a more convenient \c{const char *}. The price of doing this is that some algorithms will perform badly. For example, append() is O(length()) since it scans for a null terminator. Although you might use QCString for text that is never exposed to the user, for most purposes, and especially for user-visible text, you should use QString. QString provides implicit sharing, Unicode and other internationalization support, and is well optimized. Note that for the QCString methods that take a \c{const char *} parameter the \c{const char *} must either be 0 (null) or not-null and '\0' (NUL byte) terminated; otherwise the results are undefined. A QCString that has not been assigned to anything is \e null, i.e. both the length and the data pointer is 0. A QCString that references the empty string ("", a single '\0' char) is \e empty. Both null and empty QCStrings are legal parameters to the methods. Assigning \c{const char *} 0 to QCString produces a null QCString. The length() function returns the length of the string; resize() resizes the string and truncate() truncates the string. A string can be filled with a character using fill(). Strings can be left or right padded with characters using leftJustify() and rightJustify(). Characters, strings and regular expressions can be searched for using find() and findRev(), and counted using contains(). Strings and characters can be inserted with insert() and appended with append(). A string can be prepended with prepend(). Characters can be removed from the string with remove() and replaced with replace(). Portions of a string can be extracted using left(), right() and mid(). Whitespace can be removed using stripWhiteSpace() and simplifyWhiteSpace(). Strings can be converted to uppercase or lowercase with upper() and lower() respectively. Strings that contain numbers can be converted to numbers with toShort(), toInt(), toLong(), toULong(), toFloat() and toDouble(). Numbers can be converted to strings with setNum(). Many operators are overloaded to work with QCStrings. QCString also supports some more obscure functions, e.g. sprintf(), setStr() and setExpand(). \target asciinotion \sidebar Note on Character Comparisons In QCString the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects functions which support a case insensitive option or which compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only ASCII characters. (If \c $LC_CTYPE is set, most Unix systems do "the right thing".) Functions that this affects include contains(), find(), findRev(), \l operator<(), \l operator<=(), \l operator>(), \l operator>=(), lower() and upper(). This issue does not apply to \l{QString}s since they represent characters using Unicode. \endsidebar Performance note: The QCString methods for QRegExp searching are implemented by converting the QCString to a QString and performing the search on that. This implies a deep copy of the QCString data. If you are going to perform many QRegExp searches on a large QCString, you will get better performance by converting the QCString to a QString yourself, and then searching in the QString.*//*! \fn QCString::QCString() Constructs a null string. \sa isNull()*//*! \fn QCString::QCString( const QCString &s ) Constructs a shallow copy \a s. \sa assign()*//*! Constructs a string with room for \a size characters, including the '\0'-terminator. Makes a null string if \a size == 0. If \a size \> 0, then the first and last characters in the string are initialized to '\0'. All other characters are uninitialized. \sa resize(), isNull()*/QCString::QCString( int size ) : QByteArray( size ){ if ( size > 0 ) { *data() = '\0'; // set terminator *(data()+(size-1)) = '\0'; }}/*! Constructs a string that is a deep copy of \a str. If \a str is 0 a null string is created. \sa isNull()*/QCString::QCString( const char *str ){ duplicate( str, qstrlen(str) + 1 );}/*! Constructs a string that is a deep copy of \a str. The copy will be at most \a maxsize bytes long including the '\0'-terminator. Example: \code QCString str( "helloworld", 6 ); // assigns "hello" to str \endcode If \a str contains a 0 byte within the first \a maxsize bytes, the resulting QCString will be terminated by this 0. If \a str is 0 a null string is created. \sa isNull()*/QCString::QCString( const char *str, uint maxsize ){ if ( str == 0 ) return; uint len; // index of first '\0' for ( len = 0; len < maxsize - 1; len++ ) { if ( str[len] == '\0' ) break; } QByteArray::resize( len + 1 ); memcpy( data(), str, len ); data()[len] = 0;}/*! \reimp*/QCString::~QCString(){}/*! \fn QCString &QCString::operator=( const QCString &s ) Assigns a shallow copy of \a s to this string and returns a reference to this string.*//*! \overload QCString &QCString::operator=( const char *str ) Assigns a deep copy of \a str to this string and returns a reference to this string. If \a str is 0 a null string is created. \sa isNull()*//*! \fn bool QCString::isNull() const Returns TRUE if the string is null, i.e. if data() == 0; otherwise returns FALSE. A null string is also an empty string. Example: \code QCString a; // a.data() == 0, a.size() == 0, a.length() == 0 QCString b == ""; // b.data() == "", b.size() == 1, b.length() == 0 a.isNull(); // TRUE because a.data() == 0 a.isEmpty(); // TRUE because a.length() == 0 b.isNull(); // FALSE because b.data() == "" b.isEmpty(); // TRUE because b.length() == 0 \endcode \sa isEmpty(), length(), size()*//*! \fn bool QCString::isEmpty() const Returns TRUE if the string is empty, i.e. if length() == 0; otherwise returns FALSE. An empty string is not always a null string. See example in isNull(). \sa isNull(), length(), size()*//*! \fn uint QCString::length() const Returns the length of the string, excluding the '\0'-terminator. Equivalent to calling \c strlen(data()). Null strings and empty strings have zero length. \sa size(), isNull(), isEmpty()*//*! \fn bool QCString::truncate( uint pos ) Truncates the string at position \a pos. Equivalent to calling \c resize(pos+1). Example: \code QCString s = "truncate this string"; s.truncate( 5 ); // s == "trunc" \endcode \sa resize()*//*! Extends or shrinks the string to \a len bytes, including the '\0'-terminator. A '\0'-terminator is set at position \c{len - 1} unless \c{len == 0}. Example: \code QCString s = "resize this string"; s.resize( 7 ); // s == "resize" \endcode \sa truncate()*/bool QCString::resize( uint len ){ detach(); uint wasNull = isNull(); if ( !QByteArray::resize(len) ) return FALSE; if ( len ) data()[len - 1] = '\0'; if ( len > 0 && wasNull ) data()[0] = '\0'; return TRUE;}/*! Implemented as a call to the native vsprintf() (see the manual for your C library). If the string is shorter than 256 characters, this sprintf() calls resize(256) to decrease the chance of memory corruption. The string is resized back to its actual length before sprintf() returns. Example: \code QCString s; s.sprintf( "%d - %s", 1, "first" ); // result < 256 chars QCString big( 25000 ); // very long string big.sprintf( "%d - %s", 2, longString ); // result < 25000 chars \endcode \warning All vsprintf() implementations will write past the end of the target string (*this) if the \a format specification and arguments happen to be longer than the target string, and some will also fail if the target string is longer than some arbitrary implementation limit. Giving user-supplied arguments to sprintf() is risky: Sooner or later someone will paste a huge line into your application.*/QCString &QCString::sprintf( const char *format, ... ){ detach(); va_list ap; va_start( ap, format ); if ( size() < 256 ) QByteArray::resize( 256 ); // make string big enough vsprintf( data(), format, ap ); resize( qstrlen(data()) + 1 ); // truncate va_end( ap ); return *this;}/*! Fills the string with \a len bytes of character \a c, followed by a '\0'-terminator. If \a len is negative, then the current string length is used. Returns FALSE is \a len is nonnegative and there is not enough memory to resize the string; otherwise returns TRUE.*/bool QCString::fill( char c, int len ){ detach(); if ( len < 0 ) len = length(); if ( !QByteArray::fill(c,len+1) ) return FALSE; *(data()+len) = '\0'; return TRUE;}/*! \fn QCString QCString::copy() const Returns a deep copy of this string. \sa detach()*//*! Finds the first occurrence of the character \a c, starting at position \a index. The search is case sensitive if \a cs is TRUE, or case insensitive if \a cs is FALSE. Returns the position of \a c, or -1 if \a c could not be found. \sa \link #asciinotion Note on character comparisons \endlink*/int QCString::find( char c, int index, bool cs ) const{ if ( (uint)index >= size() ) // index outside string return -1; register const char *d; if ( cs ) { // case sensitive d = strchr( data()+index, c ); } else { d = data()+index; c = tolower( (uchar) c ); while ( *d && tolower((uchar) *d) != c ) d++; if ( !*d && c ) // not found d = 0; } return d ? (int)(d - data()) : -1;}#define REHASH( a ) \ if ( sl_minus_1 < sizeof(uint) * CHAR_BIT ) \ hashHaystack -= (a) << sl_minus_1; \ hashHaystack <<= 1/*! \overload Finds the first occurrence of the string \a str, starting at position \a index. The search is case sensitive if \a cs is TRUE, or case insensitive if \a cs is FALSE. Returns the position of \a str, or -1 if \a str could not be found. \sa \link #asciinotion Note on character comparisons \endlink*/int QCString::find( const char *str, int index, bool cs ) const{ return find( str, index, cs, length() );}int QCString::find( const char *str, int index, bool cs, uint l ) const{ if ( (uint)index >= size() ) return -1; if ( !str ) return -1; if ( !*str ) return index; const uint sl = qstrlen( str ); if ( sl + index > l ) return -1; if ( sl == 1 ) return find( *str, index, cs ); /* See QString::find() for details. */ const char* needle = str; const char* haystack = data() + index; const char* end = data() + (l-sl); const uint sl_minus_1 = sl-1; uint hashNeedle = 0, hashHaystack = 0,i; if ( cs ) { for ( i = 0; i < sl; ++i ) { hashNeedle = ((hashNeedle<<1) + needle[i] ); hashHaystack = ((hashHaystack<<1) + haystack[i] ); } hashHaystack -= *(haystack+sl_minus_1); while ( haystack <= end ) { hashHaystack += *(haystack+sl_minus_1); if ( hashHaystack == hashNeedle && *needle == *haystack && qstrncmp( needle, haystack, sl ) == 0 ) return haystack - data(); REHASH( *haystack ); ++haystack; } } else { for ( i = 0; i < sl; ++i ) { hashNeedle = ((hashNeedle<<1) + tolower( needle[i] ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -