1 /******************************************************************************
3 * Copyright (C) 1997-2015 by Dimitri van Heesch.
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
25 #include <qdatastream.h>
27 QCString &QCString::sprintf( const char *format, ... )
30 va_start( ap, format );
33 if (l<minlen) { resize(minlen); l=minlen; }
34 int n=vsnprintf( rawData(), l, format, ap);
41 int QCString::find( char c, int index, bool cs ) const
43 if (index<0 || index>=(int)length()) return -1; // index outside string
47 pos = strchr(data()+index,c);
52 c = tolower((unsigned char)c);
53 while (*pos && tolower((unsigned char)*pos)!=c) pos++;
54 if (!*pos && c) pos=0; // not found
56 return pos ? (int)(pos - data()) : -1;
59 int QCString::find( const char *str, int index, bool cs ) const
62 if (index<0 || index>=l) return -1; // index outside string
63 if (!str) return -1; // no string to search for
64 if (!*str) return index; // empty string matching at index
66 if (cs) // case sensitive
68 pos = strstr(data()+index,str);
70 else // case insensitive
73 int len = qstrlen(str);
76 if (qstrnicmp(pos,str,len)==0) break;
79 if (!*pos) pos = 0; // not found
81 return pos ? (int)(pos - data()) : -1;
84 int QCString::find( const QCString &str, int index, bool cs ) const
86 return find(str.data(),index,cs);
89 int QCString::find( const QRegExp &rx, int index ) const
91 QString d = QString::fromLatin1( data() );
92 return d.find( rx, index );
95 int QCString::findRev( char c, int index, bool cs) const
97 const char *b = data();
100 if (len==0) return -1; // empty string
101 if (index<0) // start from end
106 return pos ? (int)(pos - b) : -1;
110 else if (index>len) // bad index
117 while ( pos>=b && *pos!=c) pos--;
121 c = tolower((unsigned char)c);
122 while ( pos>=b && tolower((unsigned char)*pos)!=c) pos--;
124 return pos>=b ? (int)(pos - b) : -1;
127 int QCString::findRev( const char *str, int index, bool cs) const
129 int slen = qstrlen(str);
131 if (index<0) index = len-slen; // start from end
132 else if (index>len) return -1; // bad index
133 else if (index+slen>len) index=len-slen; // str would be too long
134 if (index<0) return -1; // no match possible
135 const char *pos = data()+index;
136 if (cs) // case sensitive
138 for (int i=index; i>=0; i--) if (qstrncmp(pos--,str,slen)==0) return i;
140 else // case insensitive
142 for (int i=index; i>=0; i--) if (qstrnicmp(pos,str,slen)==0) return i;
147 int QCString::findRev( const QRegExp &rx, int index ) const
149 QString d = QString::fromLatin1( data() );
150 return d.findRev( rx, index );
153 int QCString::contains( char c, bool cs ) const
155 if (length()==0) return 0;
157 const char *pos = data();
160 while (*pos) if (*pos++ == c) count++;
164 c = tolower((unsigned char)c);
167 if (tolower((unsigned char)*pos)==c) count++;
174 int QCString::contains( const char *str, bool cs ) const
176 if (str==0 || length()==0) return 0;
178 const char *pos = data();
179 int len = qstrlen(str);
184 if (qstrncmp(pos,str,len)==0) count++;
188 if (qstrnicmp(pos,str,len)==0) count++;
195 int QCString::contains( const QRegExp &rx ) const
197 QString d = QString::fromLatin1( data() );
198 return d.contains( rx );
201 bool QCString::stripPrefix(const char *prefix)
203 if (prefix==0 || length()==0) return FALSE;
204 int len = qstrlen(prefix);
205 if (qstrncmp(prefix,data(),len)==0)
207 m_rep=mid(len,length()-len).m_rep; // need to make a deep copy
213 QCString QCString::left( uint len ) const
219 else if (len>=length())
221 return QCString(data());
226 memcpy( s.rawData(), data(), len);
231 QCString QCString::right( uint len ) const
240 if ((int)len>l) len=l;
241 const char *pos = data() + (l-len);
242 return QCString(pos);
246 QCString QCString::mid( uint index, uint len) const
248 uint slen = (uint)length();
249 if (len==0xffffffff) len = slen-index;
250 if (isEmpty() || index>=slen || len==0)
256 const char *p = data()+index;
258 qstrncpy( s.rawData(), p, len+1 );
263 QCString QCString::lower() const
265 if (length()==0) return QCString();
267 char *pos = s.rawData();
272 *pos = tolower((unsigned char)*pos);
279 QCString QCString::upper() const
281 if (length()==0) return QCString();
283 char *pos = s.rawData();
288 *pos = toupper((unsigned char)*pos);
295 QCString QCString::stripWhiteSpace() const
297 if ( isEmpty() ) // nothing to do
300 const char *cs = data();
301 int reslen = length();
302 if ( !isspace((uchar)cs[0]) && !isspace((uchar)cs[reslen-1]) )
303 return *this; // returns a copy
306 char *s = result.rawData();
308 int end = reslen - 1;
309 while ( isspace((uchar) s[start]) ) // skip white space from start
311 if ( s[start] == '\0' )
312 { // only white space
315 while ( end && isspace((uchar) s[end]) ) // skip white space from end
318 qmemmove( s, &s[start], end );
319 result.resize( end + 1 );
323 QCString QCString::simplifyWhiteSpace() const
325 if ( isEmpty() ) // nothing to do
328 QCString result( length()+1 );
329 const char *from = data();
330 char *to = result.rawData();
334 while ( *from && isspace((uchar) *from) )
336 while ( *from && !isspace((uchar)*from) )
343 if ( to > first && *(to-1) == 0x20 )
346 result.resize( (int)((long)to - (long)result.data()) + 1 );
350 QCString &QCString::assign( const char *str )
352 return operator=(str);
355 QCString &QCString::insert( uint index, const char *s )
357 int len = s ? qstrlen(s) : 0;
358 if ( len == 0 ) return *this;
360 int nlen = olen + len;
361 if ((int)index>=olen)
363 resize(nlen+index-olen+1);
364 memset(rawData()+olen, ' ', index-olen);
365 memcpy(rawData()+index,s, len+1);
370 qmemmove(rawData()+index+len,data()+index,olen-index+1);
371 memcpy(rawData()+index,s,len);
376 QCString &QCString::insert( uint index, char c)
381 return insert( index, buf );
383 QCString &QCString::append( const char *s )
385 return operator+=(s);
387 QCString &QCString::prepend( const char *s )
391 QCString &QCString::remove( uint index, uint len )
393 uint olen = length();
394 if ( index + len >= olen ) // range problems
396 if ( index < olen ) // index ok
403 QCString tmp(olen-index-len+1);
404 qmemmove( tmp.rawData(), data()+index+len, olen-index-len+1 );
405 resize( olen-len+1 );
406 memcpy( rawData()+index,tmp.data(),tmp.length() );
411 QCString &QCString::replace( uint index, uint len, const char *s)
413 remove( index, len );
418 QCString &QCString::replace( const QRegExp &rx, const char *str )
420 QString d = QString::fromLatin1( data() );
421 QString r = QString::fromLatin1( str );
423 operator=( d.ascii() );
427 short QCString::toShort(bool *ok) const
430 return s.toShort(ok);
433 ushort QCString::toUShort(bool *ok) const
436 return s.toUShort(ok);
439 int QCString::toInt(bool *ok) const
445 uint QCString::toUInt(bool *ok) const
451 long QCString::toLong(bool *ok) const
457 ulong QCString::toULong(bool *ok) const
460 return s.toULong(ok);
463 uint64 QCString::toUInt64(bool *ok) const
466 return s.toUInt64(ok);
469 QCString &QCString::setNum(short n)
471 return setNum((long)n);
474 QCString &QCString::setNum(ushort n)
476 return setNum((ulong)n);
479 QCString &QCString::setNum(int n)
481 return setNum((long)n);
484 QCString &QCString::setNum(uint n)
486 return setNum((ulong)n);
489 QCString &QCString::setNum(long n)
506 *--p = ((int)(n%10)) + '0';
509 if ( neg ) *--p = '-';
514 QCString &QCString::setNum( ulong n)
521 *--p = ((int)(n%10)) + '0';
528 //-------------------------------------------------
530 void *qmemmove( void *dst, const void *src, uint len )
535 d = (char *)dst + len - 1;
536 s = (char *)src + len - 1;
539 } else if ( dst < src ) {
548 char *qstrdup( const char *str )
552 char *dst = new char[qstrlen(str)+1];
554 return strcpy( dst, str );
557 char *qstrncpy( char *dst, const char *src, uint len )
561 strncpy( dst, src, len );
567 int qstricmp( const char *str1, const char *str2 )
569 const uchar *s1 = (const uchar *)str1;
570 const uchar *s2 = (const uchar *)str2;
574 return s1 == s2 ? 0 : (int)((long)s2 - (long)s1);
575 for ( ; !(res = (c=tolower(*s1)) - tolower(*s2)); s1++, s2++ )
576 if ( !c ) // strings are equal
581 int qstrnicmp( const char *str1, const char *str2, uint len )
583 const uchar *s1 = (const uchar *)str1;
584 const uchar *s2 = (const uchar *)str2;
588 return (int)((long)s2 - (long)s1);
589 for ( ; len--; s1++, s2++ ) {
590 if ( (res = (c=tolower(*s1)) - tolower(*s2)) )
592 if ( !c ) // strings are equal
598 #ifndef QT_NO_DATASTREAM
600 QDataStream &operator<<( QDataStream &s, const QByteArray &a )
602 return s.writeBytes( a.data(), a.size() );
605 QDataStream &operator>>( QDataStream &s, QByteArray &a )
608 s >> len; // read size of array
609 if ( len == 0 || s.eof() ) { // end of file reached
613 if ( !a.resize( (uint)len ) ) { // resize array
614 #if defined(CHECK_NULL)
615 qWarning( "QDataStream: Not enough memory to read QByteArray" );
619 if ( len > 0 ) // not null array
620 s.readRawBytes( a.data(), (uint)len );
624 QDataStream &operator<<( QDataStream &s, const QCString &str )
626 return s.writeBytes( str.data(), str.size() );
629 QDataStream &operator>>( QDataStream &s, QCString &str )
632 s >> len; // read size of string
633 if ( len == 0 || s.eof() ) { // end of file reached
637 if ( !str.resize( (uint)len )) {// resize string
638 #if defined(CHECK_NULL)
639 qWarning( "QDataStream: Not enough memory to read QCString" );
643 if ( len > 0 ) // not null array
644 s.readRawBytes( str.rawData(), (uint)len );
648 #endif //QT_NO_DATASTREAM
650 inline QCString operator+( const QCString &s1, const QGString &s2 )
657 inline QCString operator+( const QGString &s1, const QCString &s2 )
659 QCString tmp(s1.data());