📄 qcstring.cpp
字号:
\endcode*/QCString &QCString::insert( uint index, const char *s ){ int len = qstrlen(s); if ( len == 0 ) return *this; uint olen = length(); int nlen = olen + len; if ( index >= olen ) { // insert after end of string detach(); if ( QByteArray::resize(nlen+index-olen+1, QByteArray::SpeedOptim ) ) { memset( data()+olen, ' ', index-olen ); memcpy( data()+index, s, len+1 ); } } else { detach(); if ( QByteArray::resize(nlen+1, QByteArray::SpeedOptim ) ) { // normal insert memmove( data()+index+len, data()+index, olen-index+1 ); memcpy( data()+index, s, len ); } } return *this;}/*! Inserts character \a c into the string at position \a index and returns a reference to the string. 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 c is appended. Example: \code QCString s = "Yes"; s.insert( 3, '!'); // s == "Yes!" \endcode \sa remove(), replace()*/QCString &QCString::insert( uint index, char c ) // insert char{ char buf[2]; buf[0] = c; buf[1] = '\0'; return insert( index, buf );}/*! \fn QCString &QCString::prepend( const char *s ) Prepend \a s to the string. Equivalent to insert(0, s). \sa insert()*//*! Removes \a len characters from the string, starting at position \a index, and returns a reference to the string. If \a index is out of range, nothing happens. If \a index is valid, but \a index + \a len is larger than the length of the string, the string is truncated at position \a index. \code QCString s = "Montreal"; s.remove( 1, 4 ); // s == "Meal" \endcode \sa insert(), replace()*/QCString &QCString::remove( uint index, uint len ){ uint olen = length(); if ( index + len >= olen ) { // range problems if ( index < olen ) { // index ok detach(); resize( index+1 ); } } else if ( len != 0 ) { detach(); memmove( data()+index, data()+index+len, olen-index-len+1 ); QByteArray::resize(olen-len+1, QByteArray::SpeedOptim ); } return *this;}/*! Replaces \a len characters from the string, starting at position \a index, with \a str, and returns a reference to the string. If \a index is out of range, nothing is removed and \a str is appended at the end of the string. If \a index is valid, but \a index + \a len is larger than the length of the string, \a str replaces the rest of the string from position \a index. \code QCString s = "Say yes!"; s.replace( 4, 3, "NO" ); // s == "Say NO!" \endcode \sa insert(), remove()*/QCString &QCString::replace( uint index, uint len, const char *str ){ remove( index, len ); insert( index, str ); return *this;}/*! \overload Replaces every occurrence of the character \a c in the string with \a after. Returns a reference to the string. Example: \code QCString s = "a,b,c"; s.replace( ',', " or " ); // s == "a or b or c" \endcode*/QCString &QCString::replace( char c, const char *after ){ char str[2]; str[0] = c; str[1] = '\0'; return replace( str, after );}/*! \overload Replaces every occurrence of the string \a before in the string with the string \a after. Returns a reference to the string. Example: \code QCString s = "Greek is Greek"; s.replace( "Greek", "English" ); // s == "English is English" \endcode*/QCString &QCString::replace( const char *before, const char *after ){ if ( before == after || isNull() ) return *this; detach(); int index = 0; const int bl = before ? strlen( before ) : 0; const int al = after ? strlen( after ) : 0; char *d = data(); uint len = length(); if ( bl == al ) { if ( bl ) { while( (index = find( before, index, TRUE, len ) ) != -1 ) { memcpy( d+index, after, al ); index += bl; } } } else if ( al < bl ) { uint to = 0; uint movestart = 0; uint num = 0; while( (index = find( before, index, TRUE, len ) ) != -1 ) { if ( num ) { int msize = index - movestart; if ( msize > 0 ) { memmove( d + to, d + movestart, msize ); to += msize; } } else { to = index; } if ( al ) { memcpy( d + to, after, al ); to += al; } index += bl; movestart = index; num++; } if ( num ) { int msize = len - movestart; if ( msize > 0 ) memmove( d + to, d + movestart, msize ); resize( len - num*(bl-al) + 1 ); } } else { // the most complex case. We don't want to loose performance by doing repeated // copies and reallocs of the string. while( index != -1 ) { uint indices[4096]; uint pos = 0; while( pos < 4095 ) { index = find(before, index, TRUE, len); if ( index == -1 ) break; indices[pos++] = index; index += bl; // avoid infinite loop if ( !bl ) index++; } if ( !pos ) break; // we have a table of replacement positions, use them for fast replacing int adjust = pos*(al-bl); // index has to be adjusted in case we get back into the loop above. if ( index != -1 ) index += adjust; uint newlen = len + adjust; int moveend = len; if ( newlen > len ) { resize( newlen + 1 ); len = newlen; } d = data(); while( pos ) { pos--; int movestart = indices[pos] + bl; int insertstart = indices[pos] + pos*(al-bl); int moveto = insertstart + al; memmove( d + moveto, d + movestart, (moveend - movestart) ); if ( after ) memcpy( d + insertstart, after, al ); moveend = movestart - bl; } } } return *this;}/*! \overload Replaces every occurrence of \a c1 with the char \a c2. Returns a reference to the string.*/QCString &QCString::replace( char c1, char c2 ){ detach(); uint i = 0; char *d = data(); uint len = length(); while ( i < len ) { if ( d[i] == c1 ) d[i] = c2; i++; } return *this;}#ifndef QT_NO_REGEXP_CAPTURE/*! \overload Finds the first occurrence of the regular expression \a rx, starting at position \a index. Returns the position of the next match, or -1 if \a rx was not found. \warning If you want to apply this function repeatedly to the same string it is more efficient to convert the string to a QString and apply the function to that.*/int QCString::find( const QRegExp& rx, int index ) const{ QString d = QString::fromAscii( data() ); return d.find( rx, index );}/*! \overload Finds the first occurrence of the regular expression \a rx, starting at position \a index and searching backwards. Returns the position of the next match (backwards), or -1 if \a rx was not found. \warning If you want to apply this function repeatedly to the same string it is more efficient to convert the string to a QString and apply the function to that.*/int QCString::findRev( const QRegExp& rx, int index ) const{ QString d = QString::fromAscii( data() ); return d.findRev( rx, index );}/*! \overload Counts the number of overlapping occurrences of \a rx in the string. Example: \code QString s = "banana and panama"; QRegExp r = QRegExp( "a[nm]a", TRUE, FALSE ); s.contains( r ); // 4 matches \endcode \sa find(), findRev() \warning If you want to apply this function repeatedly to the same string it is more efficient to convert the string to a QString and apply the function to that.*/int QCString::contains( const QRegExp &rx ) const{ QString d = QString::fromAscii( data() ); return d.contains( rx );}/*! \overload Replaces every occurrence of \a rx in the string with \a str. Returns a reference to the string. Example: \code QString s = "banana"; s.replace( QRegExp("a.*a"), "" ); // becomes "b" s = "banana"; s.replace( QRegExp("^[bn]a"), "X" ); // becomes "Xnana" s = "banana"; s.replace( QRegExp("^[bn]a"), "" ); // becomes "nana" \endcode \warning If you want to apply this function repeatedly to the same string it is more efficient to convert the string to a QString and apply the function to that.*/QCString &QCString::replace( const QRegExp &rx, const char *str ){ QString d = QString::fromAscii( data() ); QString r = QString::fromAscii( str ); d.replace( rx, r ); setStr( d.ascii() ); return *this;}#endif //QT_NO_REGEXP/*! Returns the string converted to a \c long value. If \a ok is not 0: \a *ok is set to FALSE if the string is not a number, or if it has trailing garbage; otherwise \a *ok is set to TRUE.*/long QCString::toLong( bool *ok ) const{ char *p = data(); long val=0; const long max_mult = LONG_MAX / 10; bool is_ok = FALSE; int neg = 0; if ( !p ) goto bye; while ( isspace((uchar) *p) ) // skip leading space p++; if ( *p == '-' ) { p++; neg = 1; } else if ( *p == '+' ) { p++; } if ( !isdigit((uchar) *p) ) goto bye; while ( isdigit((uchar) *p) ) { if ( val > max_mult || (val == max_mult && (*p-'0') > 7+neg) ) goto bye; val = 10*val + (*p++ - '0'); } if ( neg ) val = -val; while ( isspace((uchar) *p) ) // skip trailing space p++; if ( *p == '\0' ) is_ok = TRUE;bye: if ( ok ) *ok = is_ok; return is_ok ? val : 0;}/*! Returns the string converted to an \c{unsigned long} value. If \a ok is not 0: \a *ok is set to FALSE if the string is not a number, or if it has trailing garbage; otherwise \a *ok is set to TRUE.*/ulong QCString::toULong( bool *ok ) const{ char *p = data(); ulong val=0; const ulong max_mult = ULONG_MAX / 10; bool is_ok = FALSE; if ( !p ) goto bye; while ( isspace((uchar) *p) ) // skip leading space p++; if ( *p == '+' ) p++; if ( !isdigit((uchar) *p) ) goto bye; while ( isdigit((uchar) *p) ) { if ( val > max_mult || (val == max_mult && (*p-'0') > 5) ) goto bye; val = 10*val + (*p++ - '0'); } while ( isspace((uchar) *p) ) // skip trailing space p++; if ( *p == '\0' ) is_ok = TRUE;bye: if ( ok ) *ok = is_ok; return is_ok ? val : 0;}/*! Returns the string converted to a \c{short} value. If \a ok is not 0: \a *ok is set to FALSE if the string is not a number, is out of range, or if it has trailing garbage; otherwise \a *ok is set to TRUE.*/short QCString::toShort( bool *ok ) const{ long v = toLong( ok ); if ( v < SHRT_MIN || v > SHRT_MAX ) { if ( ok ) *ok = FALSE; v = 0; } return (short)v;}/*! Returns the string converted to an \c{unsigned short} value. If \a ok is not 0: \a *ok is set to FALSE if the string is not a number, is out of range, or if it has trailing garbage; otherwise \a *ok is set to TRUE.*/ushort QCString::toUShort( bool *ok ) const{ ulong v = toULong( ok ); if ( v > USHRT_MAX ) { if ( ok ) *ok = FALSE; v = 0; } return (ushort)v;}/*! Returns the string converted to a \c{int} value. If \a ok is not 0: \a *ok is set to FALSE if the string is not a number, or if it has trailing garbage; otherwise \a *ok is set to TRUE.*/int QCString::toInt( bool *ok ) const{ long v = toLong( ok ); if ( v < INT_MIN || v > INT_MAX ) { if ( ok ) *ok = FALSE; v = 0; } return (int)v;}/*! Returns the string converted to an \c{unsigned int} value.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -