<html> <head> <link rel="stylesheet" href="page.css" type="text/css"> <title>fox-toolkit.org - Documentation</title> </head> <body bgcolor=#ffffff link=#990033 vlink=#990033 alink=#990033 text=#000000> <font face='verdana,sans-serif' size=2 > <!---- TOPIC TITLE WITH LOGO---> <table border=0 cellpadding= cellspacing=2 width=100% ><tr><td><a href='http://www.fox-toolkit.org/doc.html'><img src='../art/foxlogo_small.jpg' border=0></a></td><td width=100% valign=bottom><font face='verdana,sans-serif' size=2 ><b> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <br><img src='../art/line.gif' width=100% height=1></b></font></td></tr></table> <p> <!--- TOPIC TITLE WITH LOGO ---> <!-- Generated by Doxygen 1.3.3 --> <h1>FXRex.h</h1><div class="fragment"><pre>00001 <span class="comment">/********************************************************************************</span> 00002 <span class="comment">* *</span> 00003 <span class="comment">* R e g u l a r E x p r e s s i o n C l a s s *</span> 00004 <span class="comment">* *</span> 00005 <span class="comment">*********************************************************************************</span> 00006 <span class="comment">* Copyright (C) 1999,2004 by Jeroen van der Zijp. All Rights Reserved. *</span> 00007 <span class="comment">*********************************************************************************</span> 00008 <span class="comment">* This library is free software; you can redistribute it and/or *</span> 00009 <span class="comment">* modify it under the terms of the GNU Lesser General Public *</span> 00010 <span class="comment">* License as published by the Free Software Foundation; either *</span> 00011 <span class="comment">* version 2.1 of the License, or (at your option) any later version. *</span> 00012 <span class="comment">* *</span> 00013 <span class="comment">* This library is distributed in the hope that it will be useful, *</span> 00014 <span class="comment">* but WITHOUT ANY WARRANTY; without even the implied warranty of *</span> 00015 <span class="comment">* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *</span> 00016 <span class="comment">* Lesser General Public License for more details. *</span> 00017 <span class="comment">* *</span> 00018 <span class="comment">* You should have received a copy of the GNU Lesser General Public *</span> 00019 <span class="comment">* License along with this library; if not, write to the Free Software *</span> 00020 <span class="comment">* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *</span> 00021 <span class="comment">*********************************************************************************</span> 00022 <span class="comment">* $Id: FXRex.h,v 1.48 2004/02/08 17:17:34 fox Exp $ *</span> 00023 <span class="comment">********************************************************************************/</span> 00024 <span class="preprocessor">#ifndef FXREX_H</span> 00025 <span class="preprocessor"></span><span class="preprocessor">#define FXREX_H</span> 00026 <span class="preprocessor"></span> 00027 00028 <span class="keyword">namespace </span>FX { 00029 00030 <span class="comment"></span> 00031 <span class="comment">/// Regular expression error codes</span> 00032 <span class="comment"></span><span class="keyword">enum</span> <a class="code" href="group__FXRex__0.html#a0">FXRexError</a> { 00033 REGERR_OK, 00034 <a class="code" href="group__FXRex__0.html#a0a1937">REGERR_EMPTY</a>, <span class="comment">/// Empty pattern</span> <a name="l00035"></a><a class="code" href="group__FXRex__0.html#a0">00035</a> <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1938">REGERR_PAREN</a>, <span class="comment">/// Unmatched parenthesis</span> 00036 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1939">REGERR_BRACK</a>, <span class="comment">/// Unmatched bracket</span> 00037 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1940">REGERR_BRACE</a>, <span class="comment">/// Unmatched brace</span> 00038 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1941">REGERR_RANGE</a>, <span class="comment">/// Bad character range</span> 00039 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1942">REGERR_ESC</a>, <span class="comment">/// Bad escape sequence</span> 00040 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1943">REGERR_COUNT</a>, <span class="comment">/// Bad counted repeat</span> 00041 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1944">REGERR_NOATOM</a>, <span class="comment">/// No atom preceding repetition</span> 00042 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1945">REGERR_REPEAT</a>, <span class="comment">/// Repeat following repeat</span> 00043 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1946">REGERR_BACKREF</a>, <span class="comment">/// Bad backward reference</span> 00044 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1947">REGERR_CLASS</a>, <span class="comment">/// Bad character class</span> 00045 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1948">REGERR_COMPLEX</a>, <span class="comment">/// Expression too complex</span> 00046 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1949">REGERR_MEMORY</a>, <span class="comment">/// Out of memory</span> 00047 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0a1950">REGERR_TOKEN</a> <span class="comment">/// Illegal token</span> 00048 <span class="comment"></span> }; 00049 00050 <span class="comment"></span> 00051 <span class="comment">/// Regular expression parse flags</span> 00052 <span class="comment"></span><span class="keyword">enum</span> { 00053 <a class="code" href="group__FXRex__1.html#a0a1951">REX_NORMAL</a> = 0, <span class="comment">/// Normal mode</span> 00054 <span class="comment"></span> <a class="code" href="group__FXRex__1.html#a0a1952">REX_CAPTURE</a> = 1, <span class="comment">/// Perform capturing parentheses</span> 00055 <span class="comment"></span> <a class="code" href="group__FXRex__1.html#a0a1953">REX_ICASE</a> = 2, <span class="comment">/// Case independent matching</span> 00056 <span class="comment"></span> <a class="code" href="group__FXRex__1.html#a0a1954">REX_NEWLINE</a> = 4, <span class="comment">/// Match-any operators match newline too</span> 00057 <span class="comment"></span> <a class="code" href="group__FXRex__1.html#a0a1955">REX_VERBATIM</a> = 8, <span class="comment">/// Disable interpretation of magic characters</span> 00058 <span class="comment"></span> <a class="code" href="group__FXRex__1.html#a0a1956">REX_SYNTAX</a> = 16 <span class="comment">/// Perform syntax check only</span> 00059 <span class="comment"></span> }; 00060 00061 <span class="comment"></span> 00062 <span class="comment">/// Regular expression match flags</span> 00063 <span class="comment"></span><span class="keyword">enum</span> { 00064 <a class="code" href="group__FXRex__2.html#a0a1957">REX_FORWARD</a> = 0, <span class="comment">/// Match scanning forward from offset</span> 00065 <span class="comment"></span> <a class="code" href="group__FXRex__2.html#a0a1958">REX_BACKWARD</a> = 32, <span class="comment">/// Match scanning backward from offset</span> 00066 <span class="comment"></span> <a class="code" href="group__FXRex__2.html#a0a1959">REX_NOT_BOL</a> = 64, <span class="comment">/// Start of string is NOT begin of line</span> 00067 <span class="comment"></span> <a class="code" href="group__FXRex__2.html#a0a1960">REX_NOT_EOL</a> = 128, <span class="comment">/// End of string is NOT end of line</span> 00068 <span class="comment"></span> <a class="code" href="group__FXRex__2.html#a0a1961">REX_NOT_EMPTY</a> = 256 <span class="comment">/// Do not match empty</span> 00069 <span class="comment"></span> }; 00070 00071 <span class="comment"></span> 00072 <span class="comment">/**</span> 00073 <span class="comment">* FXRex is a regular expression class implementing a NFA matcher.</span> 00074 <span class="comment">* It supports capturing parentheses, non-capturing parentheses,</span> 00075 <span class="comment">* positive or negative lookahead, backreferences, case-insensitive</span> 00076 <span class="comment">* matching, counted repetitions, lazy or greedy matches, and</span> 00077 <span class="comment">* PERL-like matching operators.</span> 00078 <span class="comment">* The subject string may be scanned forwards or backwards, and may</span> 00079 <span class="comment">* contain any of 256 possible character values.</span> 00080 <span class="comment">*</span> 00081 <span class="comment">* When parsing a regular expression pattern, the mode parameter is</span> 00082 <span class="comment">* the bitwise OR of a set of flags and affects the match algorithm.</span> 00083 <span class="comment">* Passing the flag REX_CAPTURE enables capturing parentheses</span> 00084 <span class="comment">* and back references. The flag REX_ICASE enables case-insensitive</span> 00085 <span class="comment">* matching. When the flag REX_NEWLINE is passed, newlines are treated</span> 00086 <span class="comment">* like normal characters; otherwise, newline is NOT matched</span> 00087 <span class="comment">* except when explicitly part of a character class. The flag</span> 00088 <span class="comment">* REX_VERBATIM disables all special character interpretation.</span> 00089 <span class="comment">*</span> 00090 <span class="comment">* When matching a compiled pattern, the mode parameter is the</span> 00091 <span class="comment">* bitwise OR of a set of flags that affects how the match is</span> 00092 <span class="comment">* performed. Passing the flag REX_BACKWARD causes the match</span> 00093 <span class="comment">* to proceed backwards through the subject string. Passing the</span> 00094 <span class="comment">* flags REX_NOT_BOL and/or REX_NOT_EOL causes the begin and</span> 00095 <span class="comment">* end of the subject string NOT to be considered a line start</span> 00096 <span class="comment">* or line end. The flag REX_NOT_EMPTY causes a match to fail if</span> 00097 <span class="comment">* the empty string was matched.</span> 00098 <span class="comment">*/</span> 00099 <span class="keyword">class </span>FXAPI FXRex { 00100 <span class="keyword">private</span>: 00101 FXint *code; 00102 <span class="keyword">private</span>: 00103 <span class="keyword">static</span> <span class="keyword">const</span> FXchar *<span class="keyword">const</span> errors[]; 00104 <span class="keyword">static</span> <span class="keyword">const</span> FXint fallback[]; 00105 <span class="keyword">public</span>: 00106 <span class="comment"></span> 00107 <span class="comment"> /// Construct empty regular expression object</span> 00108 <span class="comment"></span> FXRex():code((FXint*)fallback){} 00109 <span class="comment"></span> 00110 <span class="comment"> /// Copy regular expression object</span> 00111 <span class="comment"></span> FXRex(<span class="keyword">const</span> FXRex& orig); 00112 <span class="comment"></span> 00113 <span class="comment"> /// Compile expression from pattern; if error is not NULL, error code is returned</span> 00114 <span class="comment"></span> FXRex(<span class="keyword">const</span> FXchar* pattern,FXint mode=REX_NORMAL,FXRexError* error=NULL); 00115 <span class="comment"></span> <a name="l00116"></a><a class="code" href="classFX_1_1FXRex.html">00116</a> <span class="comment"> /// Compile expression from pattern; if error is not NULL, error code is returned</span> 00117 <span class="comment"></span> FXRex(<span class="keyword">const</span> <a class="code" href="classFX_1_1FXString.html">FXString</a>& pattern,FXint mode=REX_NORMAL,FXRexError* error=NULL); 00118 <span class="comment"></span> 00119 <span class="comment"> /// Assign another regular expression to this one</span> <a name="l00120"></a><a class="code" href="classFX_1_1FXRex.html#a0">00120</a> <span class="comment"></span> FXRex& operator=(<span class="keyword">const</span> FXRex& orig); 00121 <span class="comment"></span> 00122 <span class="comment"> /**</span> 00123 <span class="comment"> * See if regular expression is empty; the regular expression</span> 00124 <span class="comment"> * will be empty when it is unable to parse a pattern due to</span> 00125 <span class="comment"> * a syntax error.</span> 00126 <span class="comment"> */</span> 00127 FXbool empty()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> (code==fallback); } 00128 <span class="comment"></span> 00129 <span class="comment"> /// Parse pattern, return error code if syntax error is found</span> 00130 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0">FXRexError</a> parse(<span class="keyword">const</span> FXchar* pattern,FXint mode=REX_NORMAL); 00131 <span class="comment"></span> 00132 <span class="comment"> /// Parse pattern, return error code if syntax error is found</span> 00133 <span class="comment"></span> <a class="code" href="group__FXRex__0.html#a0">FXRexError</a> parse(<span class="keyword">const</span> FXString& pattern,FXint mode=REX_NORMAL); 00134 <span class="comment"></span> 00135 <span class="comment"> /**</span> 00136 <span class="comment"> * Match a subject string of length len, returning TRUE if a match is found</span> 00137 <span class="comment"> * and FALSE otherwise. The entire pattern is captured in beg[0] and end[0],</span> 00138 <span class="comment"> * where beg[0] refers to the position of the first matched character and end[0]</span> <a name="l00139"></a><a class="code" href="classFX_1_1FXRex.html#a5">00139</a> <span class="comment"> * refers to the position after the last matched character.</span> 00140 <span class="comment"> * Sub expressions from capturing parenthesis i are returned in beg[i] and end[i].</span> 00141 <span class="comment"> */</span> 00142 FXbool match(<span class="keyword">const</span> FXchar* string,FXint len,FXint* beg=NULL,FXint* end=NULL,FXint mode=REX_FORWARD,FXint npar=1,FXint fm=0,FXint to=2147483647) <span class="keyword">const</span>; 00143 <span class="comment"></span> 00144 <span class="comment"> /// Search for match in a string</span> 00145 <span class="comment"></span> FXbool match(<span class="keyword">const</span> <a class="code" href="classFX_1_1FXString.html">FXString</a>& string,FXint* beg=NULL,FXint* end=NULL,FXint mode=REX_FORWARD,FXint npar=1,FXint fm=0,FXint to=2147483647) <span class="keyword">const</span>; 00146 <span class="comment"></span> 00147 <span class="comment"> /**</span> 00148 <span class="comment"> * After performing a regular expression match with capturing parentheses,</span> 00149 <span class="comment"> * a substitution string is build from the replace string, where where "&"</span> 00150 <span class="comment"> * is replaced by the entire matched pattern, and "\1" through "\9" are</span> 00151 <span class="comment"> * replaced by captured expressions. The original source string and its</span> 00152 <span class="comment"> * length, and the match arrays beg and end must be passed.</span> 00153 <span class="comment"> */</span> 00154 <span class="keyword">static</span> <a class="code" href="classFX_1_1FXString.html">FXString</a> substitute(<span class="keyword">const</span> FXchar* string,FXint len,FXint* beg,FXint* end,<span class="keyword">const</span> <a class="code" href="classFX_1_1FXString.html">FXString</a>& replace,FXint npar=1); 00155 <span class="comment"></span> 00156 <span class="comment"> /// Return substitution string</span> 00157 <span class="comment"></span> <span class="keyword">static</span> <a class="code" href="classFX_1_1FXString.html">FXString</a> substitute(<span class="keyword">const</span> <a class="code" href="classFX_1_1FXString.html">FXString</a>& string,FXint* beg,FXint* end,<span class="keyword">const</span> <a class="code" href="classFX_1_1FXString.html">FXString</a>& replace,FXint npar=1); 00158 <span class="comment"></span> 00159 <span class="comment"> /// Returns error code for given error</span> 00160 <span class="comment"></span> <span class="keyword">static</span> <span class="keyword">const</span> FXchar* getError(FXRexError err){ <span class="keywordflow">return</span> errors[err]; } 00161 <span class="comment"></span> 00162 <span class="comment"> /// Comparison operators</span> 00163 <span class="comment"></span> <span class="keyword">friend</span> FXAPI FXbool operator==(<span class="keyword">const</span> FXRex &r1,<span class="keyword">const</span> FXRex &r2); 00164 <span class="keyword">friend</span> FXAPI FXbool operator!=(<span class="keyword">const</span> FXRex &r1,<span class="keyword">const</span> FXRex &r2); 00165 <span class="comment"></span> 00166 <span class="comment"> /// Saving and loading</span> 00167 <span class="comment"></span> <span class="keyword">friend</span> FXAPI FXStream& operator<<(FXStream& store,<span class="keyword">const</span> FXRex& s); 00168 <span class="keyword">friend</span> FXAPI FXStream& operator>>(FXStream& store,FXRex& s); 00169 <span class="comment"></span> 00170 <span class="comment"> /// Delete</span> 00171 <span class="comment"></span> ~FXRex(); <a name="l00172"></a><a class="code" href="classFX_1_1FXRex.html#e2">00172</a> }; 00173 00174 } 00175 00176 <span class="preprocessor">#endif</span> </pre></div></font> <!--- COPYRIGHT --> <p> <table width=100% cellpadding=0 cellspacing=0><tr><td width=100% valign=top id=HEADLINE align=right> <img src='../art/line.gif' width=100% height=1><font size=-1> Copyright © 1997-2004 Jeroen van der Zijp</font> </td></tr></table> </p> <!--- COPYRIGHT --> </body> </html>