1 /* 2 Open Tracker License 3 4 Terms and Conditions 5 6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved. 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy of 9 this software and associated documentation files (the "Software"), to deal in 10 the Software without restriction, including without limitation the rights to 11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 12 of the Software, and to permit persons to whom the Software is furnished to do 13 so, subject to the following conditions: 14 15 The above copyright notice and this permission notice applies to all licensees 16 and shall be included in all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 25 Except as contained in this notice, the name of Be Incorporated shall not be 26 used in advertising or otherwise to promote the sale, use or other dealings in 27 this Software without prior written authorization from Be Incorporated. 28 29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks 30 of Be Incorporated in the United States and other countries. Other brand product 31 names are registered trademarks or trademarks of their respective holders. 32 All rights reserved. 33 */ 34 #ifndef _REG_EXP_H 35 #define _REG_EXP_H 36 37 38 // This code is based on regexp.c, v.1.3 by Henry Spencer: 39 40 // @(#)regexp.c 1.3 of 18 April 87 41 // 42 // Copyright (c) 1986 by University of Toronto. 43 // Written by Henry Spencer. Not derived from licensed software. 44 // 45 // Permission is granted to anyone to use this software for any 46 // purpose on any computer system, and to redistribute it freely, 47 // subject to the following restrictions: 48 // 49 // 1. The author is not responsible for the consequences of use of 50 // this software, no matter how awful, even if they arise 51 // from defects in it. 52 // 53 // 2. The origin of this software must not be misrepresented, either 54 // by explicit claim or by omission. 55 // 56 // 3. Altered versions must be plainly marked as such, and must not 57 // be misrepresented as being the original software. 58 // 59 // Beware that some of this code is subtly aware of the way operator 60 // precedence is structured in regular expressions. Serious changes in 61 // regular-expression syntax might require a total rethink. 62 // 63 64 // ALTERED VERSION: Adapted to ANSI C and C++ for the OpenTracker 65 // project (www.opentracker.org), Jul 11, 2000. 66 67 68 #include <String.h> 69 70 71 namespace BPrivate { 72 73 74 enum { 75 REGEXP_UNMATCHED_PARENTHESIS = B_ERRORS_END, 76 REGEXP_TOO_BIG, 77 REGEXP_TOO_MANY_PARENTHESIS, 78 REGEXP_JUNK_ON_END, 79 REGEXP_STAR_PLUS_OPERAND_EMPTY, 80 REGEXP_NESTED_STAR_QUESTION_PLUS, 81 REGEXP_INVALID_BRACKET_RANGE, 82 REGEXP_UNMATCHED_BRACKET, 83 REGEXP_INTERNAL_ERROR, 84 REGEXP_QUESTION_PLUS_STAR_FOLLOWS_NOTHING, 85 REGEXP_TRAILING_BACKSLASH, 86 REGEXP_CORRUPTED_PROGRAM, 87 REGEXP_MEMORY_CORRUPTION, 88 REGEXP_CORRUPTED_POINTERS, 89 REGEXP_CORRUPTED_OPCODE 90 }; 91 92 93 const int32 kSubExpressionMax = 10; 94 95 96 struct regexp { 97 const char* startp[kSubExpressionMax]; 98 const char* endp[kSubExpressionMax]; 99 char regstart; // Internal use only. See RegExp.cpp for details. 100 char reganch; // Internal use only. 101 const char* regmust;// Internal use only. 102 int regmlen; // Internal use only. 103 char program[1]; // Unwarranted chumminess with compiler. 104 }; 105 106 107 class RegExp { 108 public: 109 RegExp(); 110 RegExp(const char*); 111 RegExp(const BString&); 112 ~RegExp(); 113 114 status_t InitCheck() const; 115 116 status_t SetTo(const char*); 117 status_t SetTo(const BString&); 118 119 bool Matches(const char* string) const; 120 bool Matches(const BString&) const; 121 122 int32 RunMatcher(regexp*, const char*) const; 123 regexp* Compile(const char*); 124 regexp* Expression() const; 125 const char* ErrorString() const; 126 127 #ifdef DEBUG 128 void Dump(); 129 #endif 130 131 private: 132 void SetError(status_t error) const; 133 134 // Working functions for Compile(): 135 char* Reg(int32, int32*); 136 char* Branch(int32*); 137 char* Piece(int32*); 138 char* Atom(int32*); 139 char* Node(char); 140 char* Next(char*); 141 const char* Next(const char*) const; 142 void Char(char); 143 void Insert(char, char*); 144 void Tail(char*, char*); 145 void OpTail(char*, char*); 146 147 // Working functions for RunMatcher(): 148 int32 Try(regexp*, const char*) const; 149 int32 Match(const char*) const; 150 int32 Repeat(const char*) const; 151 152 // Utility functions: 153 #ifdef DEBUG 154 char* Prop(const char*) const; 155 void RegExpError(const char*) const; 156 #endif 157 inline int32 UCharAt(const char* p) const; 158 inline char* Operand(char* p) const; 159 inline const char* Operand(const char* p) const; 160 inline bool IsMult(char c) const; 161 162 // --------- Variables ------------- 163 164 mutable status_t fError; 165 regexp* fRegExp; 166 167 // Work variables for Compile(). 168 const char* fInputScanPointer; 169 int32 fParenthesisCount; 170 char fDummy; 171 char* fCodeEmitPointer; 172 // &fDummy = don't. 173 long fCodeSize; 174 175 // Work variables for RunMatcher(). 176 mutable const char* fStringInputPointer; 177 mutable const char* fRegBol; 178 // Beginning of input, for ^ check. 179 mutable const char** fStartPArrayPointer; 180 mutable const char** fEndPArrayPointer; 181 }; 182 183 184 } // namespace BPrivate 185 186 using namespace BPrivate; 187 188 189 #endif // _REG_EXP_H 190