⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qcstring.cpp

📁 doxygen(一个自动从源代码生成文档的工具)的源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/****************************************************************************** $Id: qt/src/tools/qcstring.cpp   2.2.3   edited 2000-10-13 $**** Implementation of extended char array operations, and QByteArray and** QCString classes**** Created : 920722**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of the tools module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for**   information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qstring.h"#include "qregexp.h"#include "qdatastream.h"#include <stdio.h>#include <stdarg.h>#include <stdlib.h>#include <ctype.h>/*****************************************************************************  Safe and portable C string functions; extensions to standard string.h *****************************************************************************//*!  \fn void *memmove( void *dst, const void *src, uint len )  \relates QCString  This function is normally part of the C library. Qt implements  memmove() for platforms that do not have it.  memmove() copies \e len bytes from \e src into \e dst.  The data is  copied correctly even if \e src and \e dst overlap.*/void *qmemmove( void *dst, const void *src, uint len ){    register char *d;    register char *s;    if ( dst > src ) {	d = (char *)dst + len - 1;	s = (char *)src + len - 1;	while ( len-- )	    *d-- = *s--;    } else if ( dst < src ) {	d = (char *)dst;	s = (char *)src;	while ( len-- )	    *d++ = *s++;    }    return dst;}/*!  \relates QCString  Returns a duplicate string.  Allocates space for a copy of \e str (using \c new), copies it, and returns  a pointer to the copy.  If \e src is null, it immediately returns 0.*/char *qstrdup( const char *str ){    if ( !str )	return 0;    char *dst = new char[strlen(str)+1];    CHECK_PTR( dst );    return strcpy( dst, str );}/*!  \relates QCString  A safe strncpy() function.  Copies all characters up to \e len bytes from \e str into \e dst and returns  a pointer to \e dst.	Guarantees that \e dst is \0-terminated.  If \e src is null, it immediately returns 0.  \sa qstrcpy()*/char *qstrncpy( char *dst, const char *src, uint len ){    if ( !src )	return 0;    strncpy( dst, src, len );    if ( len > 0 )	dst[len-1] = '\0';    return dst;}/*!  \fn int qstrcmp( const char *str1, const char *str2 )  \relates QCString  A safe strcmp() function.  Compares \e str1 and \e str2.	 Returns a negative value if \e str1  is less than \e str2, 0 if \e str1 is equal to \e str2 or a positive  value if \e str1 is greater than \e str2.  Special case I: Returns 0 if \e str1 and \e str2 are both null.  Special case II: Returns a random nonzero value if \e str1 is null  or \e str2 is null (but not both).  \sa qstrncmp(), qstricmp(), qstrnicmp()*//*!  \fn int qstrncmp( const char *str1, const char *str2, uint len )  \relates QCString  A safe strncmp() function.  Compares \e str1 and \e str2 up to \e len bytes.  Returns a negative value if \e str1 is less than \e str2, 0 if \e str1  is equal to \e str2 or a positive value if \e str1 is greater than \e  str2.  Special case I: Returns 0 if \e str1 and \e str2 are both null.  Special case II: Returns a random nonzero value if \e str1 is null  or \e str2 is null (but not both).  \sa qstrcmp(), qstricmp(), qstrnicmp()*//*!  \fn int qstricmp( const char *str1, const char *str2 )  \relates QCString  A safe stricmp() function.  Compares \e str1 and \e str2 ignoring the case.  Returns a negative value if \e str1 is less than \e str2, 0 if \e str1  is equal to \e str2 or a positive value if \e str1 is greater than \e  str2.  Special case I: Returns 0 if \e str1 and \e str2 are both null.  Special case II: Returns a random nonzero value if \e str1 is null  or \e str2 is null (but not both).  \sa qstrcmp(), qstrncmp(), qstrnicmp()*/int qstricmp( const char *str1, const char *str2 ){    register const uchar *s1 = (const uchar *)str1;    register const uchar *s2 = (const uchar *)str2;    int res;    uchar c;    if ( !s1 || !s2 )	return s1 == s2 ? 0 : (int)((long)s2 - (long)s1);    for ( ; !(res = (c=tolower(*s1)) - tolower(*s2)); s1++, s2++ )	if ( !c )				// strings are equal	    break;    return res;}/*!  \fn int strnicmp( const char *str1, const char *str2, uint len )  \relates QCString  A safe strnicmp() function.  Compares \e str1 and \e str2 up to \e len bytes ignoring the case.  Returns a negative value if \e str1 is less than \e str2, 0 if \e str1  is equal to \e str2 or a positive value if \e str1 is greater than \e  str2.  Special case I: Returns 0 if \e str1 and \e str2 are both null.  Special case II: Returns a random nonzero value if \e str1 is null  or \e str2 is null (but not both).  \sa qstrcmp(), qstrncmp() qstricmp()*/int qstrnicmp( const char *str1, const char *str2, uint len ){    register const uchar *s1 = (const uchar *)str1;    register const uchar *s2 = (const uchar *)str2;    int res;    uchar c;    if ( !s1 || !s2 )	return (int)((long)s2 - (long)s1);    for ( ; len--; s1++, s2++ ) {	if ( (res = (c=tolower(*s1)) - tolower(*s2)) )	    return res;	if ( !c )				// strings are equal	    break;    }    return 0;}static Q_UINT16 crc_tbl[16];static bool   crc_tbl_init = FALSE;static void createCRC16Table()			// build CRC16 lookup table{    register uint i;    register uint j;    uint v0, v1, v2, v3;    for ( i=0; i<16; i++ ) {	v0 = i & 1;	v1 = (i >> 1) & 1;	v2 = (i >> 2) & 1;	v3 = (i >> 3) & 1;	j = 0;#undef	SET_BIT#define SET_BIT(x,b,v)	x |= v << b	SET_BIT(j, 0,v0);	SET_BIT(j, 7,v0);	SET_BIT(j,12,v0);	SET_BIT(j, 1,v1);	SET_BIT(j, 8,v1);	SET_BIT(j,13,v1);	SET_BIT(j, 2,v2);	SET_BIT(j, 9,v2);	SET_BIT(j,14,v2);	SET_BIT(j, 3,v3);	SET_BIT(j,10,v3);	SET_BIT(j,15,v3);	crc_tbl[i] = j;    }}/*!  \relates QByteArray  Returns the CRC-16 checksum of \e len bytes starting at \e data.  The checksum is independent of the byte order (endianness).*/Q_UINT16 qChecksum( const char *data, uint len ){    if ( !crc_tbl_init ) {			// create lookup table	createCRC16Table();	crc_tbl_init = TRUE;    }    register Q_UINT16 crc = 0xffff;    uchar c;    uchar *p = (uchar *)data;    while ( len-- ) {	c = *p++;	crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];	c >>= 4;	crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];    }    return (~crc & 0xffff);}/*****************************************************************************  QByteArray member functions *****************************************************************************/// NOT REVISED/*! \class QByteArray qcstring.h  \brief The QByteArray class provides an array of bytes.  \inherit QArray  \ingroup tools  \ingroup shared  QByteArray is defined as QArray\<char\>.*//*****************************************************************************  QByteArray stream functions *****************************************************************************//*!  \relates QByteArray  Writes a byte array to a stream 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 QByteArray  Reads a byte array from a stream 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(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  \brief The QCString class provides an abstraction of the classic C  zero-terminated char array (<var>char*</var>).  \ingroup tools  \ingroup shared  QCString inherits QByteArray, which is defined as QArray\<char\>.  Since QCString is a QArray, it uses \e explicit  \link shclass.html sharing\endlink with a reference count.  You might use QCString for text that is never exposed to the user,  but for text the user sees, you should use QString (which provides  implicit sharing, Unicode and other internationalization support).  Note that QCString is one of the weaker classes in Qt; its design is  flawed (it tries to behave like a more convenient const char *) and  as a result, algorithms that use QCString heavily all too often  perform badly.  For example, append() is O(length()) since it scans  for a null terminator, which makes many algorithms that use QCString  scale even worse.  Note that for the QCString methods that take a <var>const char *</var>  parameter the results are undefined if the QCString is not  zero-terminated.  It is legal for the <var>const char *</var> parameter  to be 0.  A QCString that has not been assigned to anything is \e null, i.e. both  the length and 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 <var>const char  * 0</var> to QCString gives a null QCString.  \sa \link shclass.html Shared classes\endlink*//*  Implementation 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. Therefore, if you are giong to perform many QRegExp searches  on one and the same, large QCString, you will get better performance  by converting the QCString to a QString yourself, and do the  searches on that. The results will be of course be identical.*//*!  \fn QCString::QCString()  Constructs a null string.  \sa isNull()*//*!  \fn QCString::QCString( const QCString &s )  Constructs a shallow copy \e s.  \sa assign()*//*!  Constructs a string with room for \e size characters, including the  '\0'-terminator.  Makes a null string if \e size == 0.  If \e 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 \e 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 \e str, that is no more  than \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 the 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;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -