xref: /haiku/src/kits/tracker/RegExp.h (revision e53f0019b57484c9fe0b24371d9c8520b52af57d)
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 const int32 kSubExpressionMax = 10;
93 
94 struct regexp {
95 	const char* startp[kSubExpressionMax];
96 	const char* endp[kSubExpressionMax];
97 	char regstart;		// Internal use only. See RegExp.cpp for details.
98 	char reganch;		// Internal use only.
99 	const char* regmust;// Internal use only.
100 	int regmlen;		// Internal use only.
101 	char program[1];	// Unwarranted chumminess with compiler.
102 };
103 
104 
105 class RegExp {
106 public:
107 	RegExp();
108 	RegExp(const char*);
109 	RegExp(const BString&);
110 	~RegExp();
111 
112 	status_t InitCheck() const;
113 
114 	status_t SetTo(const char*);
115 	status_t SetTo(const BString &);
116 
117 	bool Matches(const char* string) const;
118 	bool Matches(const BString &) const;
119 
120 	int32 RunMatcher(regexp*, const char*) const;
121 	regexp* Compile(const char*);
122 	regexp* Expression() const;
123 	const char* ErrorString() const;
124 
125 #ifdef DEBUG
126 	void Dump();
127 #endif
128 
129 private:
130 	void SetError(status_t error) const;
131 
132 	// Working functions for Compile():
133 	char* Reg(int32, int32*);
134 	char* Branch(int32*);
135 	char* Piece(int32*);
136 	char* Atom(int32*);
137 	char* Node(char);
138 	char* Next(char*);
139 	const char* Next(const char*) const;
140 	void Char(char);
141 	void Insert(char, char*);
142 	void Tail(char*, char*);
143 	void OpTail(char*, char*);
144 
145 	// Working functions for RunMatcher():
146 	int32 Try(regexp*, const char*) const;
147 	int32 Match(const char*) const;
148 	int32 Repeat(const char*) const;
149 
150 	// Utility functions:
151 #ifdef DEBUG
152 	char* Prop(const char*) const;
153 	void RegExpError(const char*) const;
154 #endif
155 	inline int32 UCharAt(const char* p) const;
156 	inline char* Operand(char* p) const;
157 	inline const char* Operand(const char* p) const;
158 	inline bool	IsMult(char c) const;
159 
160 // --------- Variables -------------
161 
162 	mutable status_t fError;
163 	regexp* fRegExp;
164 
165 	// Work variables for Compile().
166 	const char* fInputScanPointer;
167 	int32 fParenthesisCount;
168 	char fDummy;
169 	char* fCodeEmitPointer;
170 		// &fDummy = don't.
171 	long fCodeSize;
172 
173 	// Work variables for RunMatcher().
174 	mutable const char* fStringInputPointer;
175 	mutable const char* fRegBol;
176 		// Beginning of input, for ^ check.
177 	mutable const char** fStartPArrayPointer;
178 	mutable const char** fEndPArrayPointer;
179 };
180 
181 
182 } // namespace BPrivate
183 
184 using namespace BPrivate;
185 
186 #endif	// _REG_EXP_H
187