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

📄 qregexp.cpp

📁 doxygen(一个自动从源代码生成文档的工具)的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** $Id: qt/src/tools/qregexp.cpp   2.2.3   edited 2000-08-25 $**** Implementation of QRegExp class**** Created : 950126**** 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 "qregexp.h"#include <ctype.h>#include <stdlib.h>// NOT REVISED/*!  \class QRegExp qregexp.h  \ingroup tools  \ingroup misc  \brief The QRegExp class provides pattern matching using regular  expressions or wildcards.  QRegExp knows these regexp primitives:  <ul plain>  <li><dfn>c</dfn> matches the character 'c'  <li><dfn>.</dfn> matches any character  <li><dfn>^</dfn> matches start of input  <li><dfn>$</dfn>  matches end of input  <li><dfn>[]</dfn> matches a defined set of characters - see below.  <li><dfn>a*</dfn> matches a sequence of zero or more a's  <li><dfn>a+</dfn> matches a sequence of one or more a's  <li><dfn>a?</dfn> matches an optional a  <li><dfn>\c</dfn> escape code for matching special characters such  as \, [, *, +, . etc.  <li><dfn>\t</dfn> matches the TAB character (9)  <li><dfn>\n</dfn> matches newline (10)  <li><dfn>\r</dfn> matches return (13)  <li><dfn>\s</dfn> matches a white space (defined as any character  for which QChar::isSpace() returns TRUE. This includes at least  ASCII characters 9 (TAB), 10 (LF), 11 (VT), 12(FF), 13 (CR) and 32  (Space)).  <li><dfn>\d</dfn> matches a digit (defined as any character for  which QChar::isDigit() returns TRUE. This includes at least ASCII  characters '0'-'9').  <li><dfn>\x1f6b</dfn> matches the character with unicode point U1f6b  (hexadecimal 1f6b). \x0012 will match the ASCII/Latin1 character  0x12 (18 decimal, 12 hexadecimal).  <li><dfn>\022</dfn> matches the ASCII/Latin1 character 022 (18  decimal, 22 octal).  </ul>  In wildcard mode, it only knows four primitives:  <ul plain>  <li><dfn>c</dfn> matches the character 'c'  <li><dfn>?</dfn> matches any character  <li><dfn>*</dfn> matches any sequence of characters  <li><dfn>[]</dfn> matches a defined set of characters - see below.  </ul>  QRegExp supports Unicode both in the pattern strings and in the  strings to be matched.  When writing regular expressions in C++ code, remember that C++  processes \ characters.  So in order to match e.g. a "." character,  you must write "\\." in C++ source, not "\.".  A character set matches a defined set of characters. For example,  [BSD] matches any of 'B', 'D' and 'S'. Within a character set, the  special characters '.', '*', '?', '^', '$', '+' and '[' lose their  special meanings. The following special characters apply:  <ul plain>  <li><dfn>^</dfn> When placed first in the list, changes the  character set to match any character \e not in the list. To include  the character '^' itself in the set, escape it or place it anywhere  but first.  <li><dfn>-</dfn> Defines a range of characters. To include the  character '-' itself in the set, escape it or place it last.  <li><dfn>]</dfn> Ends the character set definition. To include the  character ']' itself in the set, escape it or place it first (but  after the negation operator '^', if present)  </ul>  Thus, [a-zA-Z0-9.] matches upper and lower case ASCII letters,  digits and dot; and [^\s] matches everything except white space.  \bug Case insensitive matching is not supported for non-ASCII/Latin1  (non-8bit) characters. Any character with a non-zero QChar.row() is  matched case sensitively even if the QRegExp is in case insensitive  mode.  \note In Qt 3.0, the language of regular expressions will contain  five more special characters, namely '(', ')', '{', '|' and '}'. To  ease porting, it's a good idea to escape these characters with a  backslash in all the regular expressions you'll write from now on.*///// The regexp pattern is internally represented as an array of uints,// each element containing an 16-bit character or a 32-bit code// (listed below).  User-defined character classes (e.g. [a-zA-Z])// are encoded as this:// uint no:	1		2		3		...// value:	CCL | n		from | to	from | to//// where n is the (16-bit) number of following range definitions and// from and to define the ranges inclusive. from <= to is always true,// otherwise it is a built-in charclass (Pxx, eg \s - PWS). Single// characters in the class are coded as from==to.  Negated classes// (e.g. [^a-z]) use CCN instead of CCL.const uint END	= 0x00000000;const uint PWS	= 0x10010000;		// predef charclass: whitespace (\s)const uint PDG	= 0x10020000;		// predef charclass: digit (\d)const uint CCL	= 0x20010000;		// character class	[]const uint CCN	= 0x20020000;		// neg character class	[^]const uint CHR	= 0x40000000;		// characterconst uint BOL	= 0x80010000;		// beginning of line	^const uint EOL	= 0x80020000;		// end of line		$const uint BOW	= 0x80030000;		// beginning of word	\<const uint EOW	= 0x80040000;		// end of word		\>const uint ANY	= 0x80050000;		// any character	.const uint CLO	= 0x80070000;		// Kleene closure	*const uint OPT	= 0x80080000;		// Optional closure	?const uint MCC  = 0x20000000;		// character class bitmaskconst uint MCD  = 0xffff0000;		// code maskconst uint MVL  = 0x0000ffff;		// value mask//// QRegExp::error codes (internal)//const int PatOk		= 0;			// pattern okconst int PatNull	= 1;			// no pattern definedconst int PatSyntax	= 2;			// pattern syntax errorconst int PatOverflow	= 4;			// pattern too long/*****************************************************************************  QRegExp member functions *****************************************************************************//*!  Constructs an empty regular expression.*/QRegExp::QRegExp(){    rxdata = 0;    cs = TRUE;    wc = FALSE;    error = PatOk;}/*!  Constructs a regular expression.  \arg \e pattern is the regular expression pattern string.  \arg \e caseSensitive specifies whether or not to use case sensitive  matching.  \arg \e wildcard specifies whether the pattern string should be used for  wildcard matching (also called globbing expression), normally used for  matching file names.  \sa setWildcard()*/QRegExp::QRegExp( const QString &pattern, bool caseSensitive, bool wildcard ){    rxstring = pattern;    rxdata = 0;    cs = caseSensitive;    wc = wildcard;    compile();}/*!  Constructs a regular expression which is a copy of \e r.  \sa operator=(const QRegExp&)*/QRegExp::QRegExp( const QRegExp &r ){    rxstring = r.pattern();    rxdata = 0;    cs = r.caseSensitive();    wc = r.wildcard();    compile();}/*!  Destructs the regular expression and cleans up its internal data.*/QRegExp::~QRegExp(){    if ( rxdata )                      // Avoid purify complaints	delete [] rxdata;}/*!  Copies the regexp \e r and returns a reference to this regexp.  The case sensitivity and wildcard options are copied, as well.*/QRegExp &QRegExp::operator=( const QRegExp &r ){    rxstring = r.rxstring;    cs = r.cs;    wc = r.wc;    compile();    return *this;}/*!  \obsolete  Consider using setPattern() instead of this method.  Sets the pattern string to \e pattern and returns a reference to this regexp.  The case sensitivity or wildcard options do not change.*/QRegExp &QRegExp::operator=( const QString &pattern ){    rxstring = pattern;    compile();    return *this;}/*!  Returns TRUE if this regexp is equal to \e r.  Two regexp objects are equal if they have equal pattern strings,  case sensitivity options and wildcard options.*/bool QRegExp::operator==( const QRegExp &r ) const{    return rxstring == r.rxstring && cs == r.cs && wc == r.wc;}/*!  \fn bool QRegExp::operator!=( const QRegExp &r ) const  Returns TRUE if this regexp is \e not equal to \e r.  \sa operator==()*//*!  \fn bool QRegExp::isEmpty() const  Returns TRUE if the regexp is empty.*//*!  \fn bool QRegExp::isValid() const  Returns TRUE if the regexp is valid, or FALSE if it is invalid.  The pattern "[a-z" is an example of an invalid pattern, since it lacks a  closing bracket.*//*!  \fn bool QRegExp::wildcard() const  Returns TRUE if wildcard mode is on, otherwise FALSE. \sa setWildcard().*//*!  Sets the wildcard option for the regular expression.	The default  is FALSE.  Setting \e wildcard to TRUE makes it convenient to match filenames  instead of plain text.  For example, "qr*.cpp" matches the string "qregexp.cpp" in wildcard mode,  but not "qicpp" (which would be matched in normal mode).  \sa wildcard()*/void QRegExp::setWildcard( bool wildcard ){    if ( wildcard != wc ) {	wc = wildcard;	compile();    }}/*!  \fn bool QRegExp::caseSensitive() const  Returns TRUE if case sensitivity is enabled, otherwise FALSE.	 The  default is TRUE.  \sa setCaseSensitive()*//*!  Enables or disables case sensitive matching.  In case sensitive mode, "a.e" matches "axe" but not "Axe".  See also: caseSensitive()*/void QRegExp::setCaseSensitive( bool enable ){    if ( cs != enable ) {	cs = enable;	compile();    }}/*!  \fn QString QRegExp::pattern() const  Returns the pattern string of the regexp.*//*!  \fn void QRegExp::setPattern(const QString & pattern)  Sets the pattern string to \a pattern and returns a reference to this regexp.  The case sensitivity or wildcard options do not change.*/static inline bool iswordchar( int x ){    return isalnum(x) || x == '_';	//# Only 8-bit support}/*!  \internal  Match character class*/static bool matchcharclass( uint *rxd, QChar c ){    uint *d = rxd;    uint clcode = *d & MCD;    bool neg = clcode == CCN;    if ( clcode != CCL && clcode != CCN)	qWarning("QRegExp: Internal error, please report to qt-bugs@trolltech.com");    uint numFields = *d & MVL;    uint cval = (((uint)(c.row())) << 8) | ((uint)c.cell());    bool found = FALSE;    for ( int i = 0; i < (int)numFields; i++ ) {	d++;	if ( *d == PWS && c.isSpace() ) {	    found = TRUE;	    break;	}	if ( *d == PDG && c.isDigit() ) {	    found = TRUE;	    break;	}	else {	    uint from = ( *d & MCD ) >> 16;	    uint to = *d & MVL;	    if ( (cval >= from) && (cval <= to) ) {		found = TRUE;		break;	    }	}    }    return neg ? !found : found;}/*  Internal: Recursively match string.*/static int matchstring( uint *rxd, const QChar *str, uint strlength,			const QChar *bol, bool cs ){    const QChar *p = str;    const QChar *start = p;    uint pl = strlength;    uint *d = rxd;    //### in all cases here: handle pl == 0! (don't read past strlen)    while ( *d ) {	if ( *d & CHR ) {			// match char	    if ( !pl )		return -1;	    QChar c( *d );	    if ( !cs && !c.row() ) {		// case insensitive, #Only 8bit		if ( p->row() || tolower(p->cell()) != c.cell() )		    return -1;		p++;		pl--;	    } else {				// case insensitive		if ( *p != c )		    return -1;		p++;		pl--;	    }	    d++;	}	else if ( *d & MCC ) {			// match char class	    if ( !pl )		return -1;	    if ( !matchcharclass( d, *p ) )		return -1;	    p++;	    pl--;	    d += (*d & MVL) + 1;	}	else switch ( *d++ ) {	    case PWS:				// match whitespace		if ( !pl || !p->isSpace() )		    return -1;		p++;		pl--;		break;	    case PDG:				// match digits		if ( !pl || !p->isDigit() )		    return -1;		p++;		pl--;		break;	    case ANY:				// match anything		if ( !pl )		    return -1;		p++;		pl--;		break;	    case BOL:				// match beginning of line		if ( p != bol )		    return -1;		break;	    case EOL:				// match end of line		if ( pl )		    return -1;		break;	    case BOW:				// match beginning of word		if ( !iswordchar(*p) || (p > bol && iswordchar(*(p-1)) ) )		    return -1;		break;	    case EOW:				// match end of word		if ( iswordchar(*p) || p == bol || !iswordchar(*(p-1)) )		    return -1;		break;	    case CLO:				// Kleene closure		{		const QChar *first_p = p;		if ( *d & CHR ) {		// match char		    QChar c( *d );		    if ( !cs && !c.row() ) {	// case insensitive, #only 8bit			while ( pl && !p->row() && tolower(p->cell())==c.cell() ) {			    p++;			    pl--;			}		    }		    else {			// case sensitive			while ( pl && *p == c ) {			    p++;			    pl--;			}		    }		    d++;		}		else if ( *d & MCC ) {			// match char class		    while( pl && matchcharclass( d, *p ) ) {			p++;			pl--;		    }		    d += (*d & MVL) + 1;		}		else if ( *d == PWS ) {		    while ( pl && p->isSpace() ) {			p++;			pl--;		    }		    d++;		}		else if ( *d == PDG ) {		    while ( pl && p->isDigit() ) {			p++;			pl--;		    }		    d++;		}		else if ( *d == ANY ) {		    p += pl;		    pl = 0;		    d++;		}		else {		    return -1;			// error		}		d++;				// skip CLO's END		while ( p >= first_p ) {	// go backwards		    int end = matchstring( d, p, pl, bol, cs );		    if ( end >= 0 )			return ( p - start ) + end;		    if ( !p )			return -1;		    --p;		    ++pl;		}		}		return -1;	    case OPT:				// optional closure		{		const QChar *first_p = p;		if ( *d & CHR ) {		// match char		    QChar c( *d );		    if ( !cs && !c.row() ) {	// case insensitive, #only 8bit			if ( pl && !p->row() && tolower(p->cell()) == c.cell() ) {			    p++;			    pl--;			}		    }

⌨️ 快捷键说明

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