📄 qcstring.cpp
字号:
hashHaystack = ((hashHaystack<<1) + tolower( haystack[i] ) ); } hashHaystack -= tolower(*(haystack+sl_minus_1)); while ( haystack <= end ) { hashHaystack += tolower(*(haystack+sl_minus_1)); if ( hashHaystack == hashNeedle && qstrnicmp( needle, haystack, sl ) == 0 ) return haystack - data(); REHASH( tolower(*haystack) ); ++haystack; } } return -1;}/*! Finds the first occurrence of the character \a c, starting at position \a index and searching backwards. 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::findRev( char c, int index, bool cs ) const{ register const char *b = data(); register const char *d; if ( index < 0 ) index = length(); if ( (uint)index >= size() ) return -1; d = b + index; if ( cs ) { while ( d >= b && *d != c ) d--; } else { c = tolower( (uchar) c ); while ( d >= b && tolower((uchar) *d) != c ) d--; } return d >= b ? (int)(d - b) : -1;}/*! \overload Finds the first occurrence of the string \a str, starting at position \a index and searching backwards. 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::findRev( const char *str, int index, bool cs ) const{ /* See QString::find() for explanations. */ const uint sl = qstrlen( str ); const uint l = length(); int delta = l-sl; if ( index < 0 ) index = delta; if ( index < 0 || index > (int)l ) return -1; if ( index > delta ) index = delta; if ( sl == 1 ) return findRev( *str, index, cs ); const char* needle = str; const char* haystack = data() + index; const char* end = data(); const uint sl_minus_1 = sl-1; const char* n = needle+sl_minus_1; const char* h = haystack+sl_minus_1; uint hashNeedle = 0, hashHaystack = 0, i; if ( cs ) { for ( i = 0; i < sl; ++i ) { hashNeedle = ((hashNeedle<<1) + *(n-i) ); hashHaystack = ((hashHaystack<<1) + *(h-i) ); } hashHaystack -= *haystack; while ( haystack >= end ) { hashHaystack += *haystack; if ( hashHaystack == hashNeedle && qstrncmp( needle, haystack, sl ) == 0 ) return haystack-data(); --haystack; REHASH( *(haystack+sl) ); } } else { for ( i = 0; i < sl; ++i ) { hashNeedle = ((hashNeedle<<1) + tolower( *(n-i) ) ); hashHaystack = ((hashHaystack<<1) + tolower( *(h-i) ) ); } hashHaystack -= tolower(*haystack); while ( haystack >= end ) { hashHaystack += tolower(*haystack); if ( hashHaystack == hashNeedle && qstrnicmp( needle, haystack, sl ) == 0 ) return haystack-data(); --haystack; REHASH( tolower(*(haystack+sl)) ); } } return -1;}/*! Returns the number of times the character \a c occurs in the string. The match is case sensitive if \a cs is TRUE, or case insensitive if \a cs if FALSE. \sa \link #asciinotion Note on character comparisons \endlink*/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;}/*! \overload Returns the number of times \a str occurs in the string. The match is case sensitive if \a cs is TRUE, or case insensitive if \a cs if FALSE. This function counts overlapping substrings, for example, "banana" contains two occurrences of "ana". \sa findRev() \link #asciinotion Note on character comparisons \endlink*/int QCString::contains( const char *str, bool cs ) const{ int count = 0; int i = -1; uint l = length(); // use find for the faster hashing algorithm while ( ( i = find ( str, i+1, cs, l ) ) != -1 ) count++; return count;}/*! Returns a substring that contains the \a len leftmost characters of the string. The whole string is returned if \a 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 \a len rightmost characters of the string. The whole string is returned if \a 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 at most \a len characters from this string, starting at position \a index. Returns a null string if the string is empty or if \a index is out of range. Returns the whole string from \a index if \a index+len exceeds the length of the string. Example: \code QCString s = "Two pineapples"; QCString t = s.mid( 4, 3 ); // t == "pin" \endcode \sa left(), right()*/QCString QCString::mid( uint index, uint len ) const{ uint slen = qstrlen( data() ); if ( isEmpty() || index >= slen ) { QCString empty; return empty; } else { if ( len > slen-index ) len = slen - index; 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 \a width (plus one for the terminating '\0') that contains this string padded with the \a fill character. If the length of the string exceeds \a width and \a truncate is FALSE (the default), then the returned string is a copy of the string. If the length of the string exceeds \a width and \a truncate is TRUE, then the returned string is a left(\a 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()); int padlen = width - len; if ( padlen > 0 ) { result.QByteArray::resize( len+padlen+1 ); memcpy( result.data(), data(), len ); memset( result.data()+len, fill, padlen ); result[len+padlen] = '\0'; } else { if ( truncate ) result = left( width ); else result = copy(); } return result;}/*! Returns a string of length \a width (plus one for the terminating '\0') that contains zero or more of the \a fill character followed by this string. If the length of the string exceeds \a width and \a truncate is FALSE (the default), then the returned string is a copy of the string. If the length of the string exceeds \a width and \a truncate is TRUE, then the returned string is a left(\a width). Example: \code QCString s("pie"); QCString t = s.rightJustify(8, '.'); // t == ".....pie" \endcode \sa leftJustify()*/QCString QCString::rightJustify( uint width, char fill, bool truncate ) const{ QCString result; int len = qstrlen(data()); int padlen = width - len; if ( padlen > 0 ) { result.QByteArray::resize( len+padlen+1 ); memset( result.data(), fill, padlen ); memcpy( result.data()+padlen, data(), len ); result[len+padlen] = '\0'; } else { if ( truncate ) result = left( width ); else result = copy(); } return result;}/*! Returns a new string that is a copy of this string converted to lower case. Example: \code QCString s("Credit"); QCString t = s.lower(); // t == "credit" \endcode \sa upper() \link #asciinotion Note on character comparisons \endlink*/QCString QCString::lower() const{ QCString s( data() ); register char *p = s.data(); if ( p ) { while ( *p ) { *p = tolower( (uchar) *p ); p++; } } return s;}/*! Returns a new string that is a copy of this string converted to upper case. Example: \code QCString s( "Debit" ); QCString t = s.upper(); // t == "DEBIT" \endcode \sa lower() \link #asciinotion Note on character comparisons \endlink*/QCString QCString::upper() const{ QCString s( data() ); register char *p = s.data(); if ( p ) { while ( *p ) { *p = toupper(*p); p++; } } return s;}/*! Returns a new string that has white space removed from the start and the end. White space means the decimal ASCII codes 9, 10, 11, 12, 13 and 32. Example: \code QCString s = " space "; QCString t = s.stripWhiteSpace(); // t == "space" \endcode \sa simplifyWhiteSpace()*/QCString QCString::stripWhiteSpace() const{ if ( isEmpty() ) // nothing to do return copy(); register char *s = data(); QCString result = s; int reslen = result.length(); if ( !isspace((uchar) s[0]) && !isspace((uchar) s[reslen-1]) ) return result; // returns a copy s = result.data(); int start = 0; int end = reslen - 1; while ( isspace((uchar) s[start]) ) // skip white space from start start++; if ( s[start] == '\0' ) { // only white space result.resize( 1 ); return result; } while ( end && isspace((uchar) s[end]) ) // skip white space from end end--; end -= start - 1; memmove( result.data(), &s[start], end ); result.resize( end + 1 ); return result;}/*! Returns a new string that has white space removed from the start and the end, plus any sequence of internal white space replaced with a single space (ASCII 32). White space means the decimal ASCII codes 9, 10, 11, 12, 13 and 32. \code QCString s = " lots\t of\nwhite space "; QCString t = s.simplifyWhiteSpace(); // t == "lots of white space" \endcode \sa stripWhiteSpace()*/QCString QCString::simplifyWhiteSpace() const{ if ( isEmpty() ) // nothing to do return copy(); QCString result( size() ); char *from = data(); char *to = result.data(); char *first = to; for ( ;; ) { while ( isspace((uchar) *from) ) from++; while ( *from && !isspace((uchar) *from) ) *to++ = *from++; if ( *from ) *to++ = 0x20; // ' ' else break; } if ( to > first && *(to-1) == 0x20 ) to--; *to = '\0'; result.resize( (int)(to - result.data()) + 1 ); return result;}/*! \overload Inserts string \a s into the string at position \a index. If \a index is beyond the end of the string, the string is padded with spaces (ASCII 32) to length \a index and then \a s is appended. \code QCString s = "I like fish"; s.insert( 2, "don't "); // s == "I don't like fish" s = "x"; // index 01234 s.insert( 3, "yz" ); // s == "x yz"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -