📄 qcstring.cpp
字号:
/*! \fn QCString &QCString::operator=( const QCString &s ) Assigns a shallow copy of \e s to this string and returns a reference to this string.*//*! \fn 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. 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. 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 \e 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 \e len bytes, including the '\0'-terminator. A \0-terminator is set at position <code>len - 1</code> unless <code>len == 0</code>. Example: \code QCString s = "resize this string"; s.resize( 7 ); // s == "resize" \endcode \sa truncate()*/bool QCString::resize( uint len ){ detach(); if ( !QByteArray::resize(len) ) return FALSE; if ( len ) *(data()+len-1) = '\0'; return TRUE;}/*! Implemented as a call to the native vsprintf() (see your C-library manual). If your 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 natural 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 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 begging for trouble. Sooner or later someone \e will paste a 3000-character 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 \e len bytes of value \e c, followed by a '\0'-terminator. If \e len is negative, then the current string length is used. Returns FALSE is \e len is nonnegative and there is no memory to resize the string, otherwise TRUE is returned.*/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 \e c, starting at position \e index. The search is case sensitive if \e cs is TRUE, or case insensitive if \e cs is FALSE. Returns the position of \e c, or -1 if \e c could not be found.*/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;}/*! Finds the first occurrence of the string \e str, starting at position \e index. The search is case sensitive if \e cs is TRUE, or case insensitive if \e cs is FALSE. Returns the position of \e str, or -1 if \e str could not be found.*/int QCString::find( const char *str, int index, bool cs ) const{ if ( (uint)index >= size() ) // index outside string return -1; if ( !str ) // no search string return -1; if ( !*str ) // zero-length search string return index; register const char *d; if ( cs ) { // case sensitive d = strstr( data()+index, str ); } else { // case insensitive d = data()+index; int len = qstrlen( str ); while ( *d ) { if ( qstrnicmp(d, str, len) == 0 ) break; d++; } if ( !*d ) // not found d = 0; } return d ? (int)(d - data()) : -1;}/*! Finds the first occurrence of the character \e c, starting at position \e index and searching backwards. The search is case sensitive if \e cs is TRUE, or case insensitive if \e cs is FALSE. Returns the position of \e c, or -1 if \e c could not be found.*/int QCString::findRev( char c, int index, bool cs ) const{ const char *b = data(); const char *d; if ( index < 0 ) { // neg index ==> start from end if ( size() == 0 ) return -1; if ( cs ) { d = strrchr( b, c ); return d ? (int)(d - b) : -1; } index = length(); } else if ( (uint)index >= size() ) { // bad index return -1; } d = b+index; if ( cs ) { // case sensitive while ( d >= b && *d != c ) d--; } else { // case insensitive c = tolower( (uchar) c ); while ( d >= b && tolower((uchar) *d) != c ) d--; } return d >= b ? (int)(d - b) : -1;}/*! Finds the first occurrence of the string \e str, starting at position \e index and searching backwards. The search is case sensitive if \e cs is TRUE, or case insensitive if \e cs is FALSE. Returns the position of \e str, or -1 if \e str could not be found.*/int QCString::findRev( const char *str, int index, bool cs ) const{ int slen = qstrlen(str); if ( index < 0 ) // neg index ==> start from end index = length()-slen; else if ( (uint)index >= size() ) // bad index return -1; else if ( (uint)(index + slen) > length() ) // str would be too long index = length() - slen; if ( index < 0 ) return -1; register char *d = data() + index; if ( cs ) { // case sensitive for ( int i=index; i>=0; i-- ) if ( qstrncmp(d--,str,slen)==0 ) return i; } else { // case insensitive for ( int i=index; i>=0; i-- ) if ( qstrnicmp(d--,str,slen)==0 ) return i; } return -1;}/*! Returns the number of times the character \e c occurs in the string. The match is case sensitive if \e cs is TRUE, or case insensitive if \e cs if FALSE.*/int QCString::contains( char c, bool cs ) const{ int count = 0; char *d = data(); if ( !d ) return 0; if ( cs ) { // case sensitive while ( *d ) if ( *d++ == c ) count++; } else { // case insensitive c = tolower( (uchar) c ); while ( *d ) { if ( tolower((uchar) *d) == c ) count++; d++; } } return count;}/*! Returns the number of times \e str occurs in the string. The match is case sensitive if \e cs is TRUE, or case insensitive if \e cs if FALSE. This function counts overlapping substrings, for example, "banana" contains two occurrences of "ana". \sa findRev()*/int QCString::contains( const char *str, bool cs ) const{ int count = 0; char *d = data(); if ( !d ) return 0; int len = qstrlen( str ); while ( *d ) { // counts overlapping strings if ( cs ) { if ( qstrncmp( d, str, len ) == 0 ) count++; } else { if ( qstrnicmp(d, str, len) == 0 ) count++; } d++; } return count;}/*! Returns a substring that contains the \e len leftmost characters of the string. The whole string is returned if \e len exceeds the length of the string. Example: \code QCString s = "Pineapple"; QCString t = s.left( 4 ); // t == "Pine" \endcode \sa right(), mid()*/QCString QCString::left( uint len ) const{ if ( isEmpty() ) { QCString empty; return empty; } else if ( len >= size() ) { QCString same( data() ); return same; } else { QCString s( len+1 ); strncpy( s.data(), data(), len ); *(s.data()+len) = '\0'; return s; }}/*! Returns a substring that contains the \e len rightmost characters of the string. The whole string is returned if \e len exceeds the length of the string. Example: \code QCString s = "Pineapple"; QCString t = s.right( 5 ); // t == "apple" \endcode \sa left(), mid()*/QCString QCString::right( uint len ) const{ if ( isEmpty() ) { QCString empty; return empty; } else { uint l = length(); if ( len > l ) len = l; char *p = data() + (l - len); return QCString( p ); }}/*! Returns a substring that contains the \e len characters of this string, starting at position \e index. Returns a null string if the string is empty or \e index is out of range. Returns the whole string from \e index if \e index+len exceeds the length of the string. Example: \code QCString s = "Two pineapples"; QCString t = s.mid( 4, 4 ); // t == "pine" \endcode \sa left(), right()*/QCString QCString::mid( uint index, uint len ) const{ if ( len == 0xffffffff ) len = length()-index; uint slen = qstrlen( data() ); if ( isEmpty() || index >= slen ) { QCString empty; return empty; } else { register char *p = data()+index; QCString s( len+1 ); strncpy( s.data(), p, len ); *(s.data()+len) = '\0'; return s; }}/*! Returns a string of length \e width (plus '\0') that contains this string and padded by the \e fill character. If the length of the string exceeds \e width and \e truncate is FALSE, then the returned string is a copy of the string. If the length of the string exceeds \e width and \e truncate is TRUE, then the returned string is a left(\e width). Example: \code QCString s("apple"); QCString t = s.leftJustify(8, '.'); // t == "apple..." \endcode \sa rightJustify()*/QCString QCString::leftJustify( uint width, char fill, bool truncate ) const{ QCString result; int len = qstrlen(data());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -