xref: /haiku/src/kits/tracker/RegExp.h (revision cda5b8808fd0262f0fac472f6cfa809f846a83cf)
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 
35 
36 // This code is based on regexp.c, v.1.3 by Henry Spencer:
37 
38 // @(#)regexp.c	1.3 of 18 April 87
39 //
40 //	Copyright (c) 1986 by University of Toronto.
41 //	Written by Henry Spencer.  Not derived from licensed software.
42 //
43 //	Permission is granted to anyone to use this software for any
44 //	purpose on any computer system, and to redistribute it freely,
45 //	subject to the following restrictions:
46 //
47 //	1. The author is not responsible for the consequences of use of
48 //		this software, no matter how awful, even if they arise
49 //		from defects in it.
50 //
51 //	2. The origin of this software must not be misrepresented, either
52 //		by explicit claim or by omission.
53 //
54 //	3. Altered versions must be plainly marked as such, and must not
55 //		be misrepresented as being the original software.
56 //
57 // Beware that some of this code is subtly aware of the way operator
58 // precedence is structured in regular expressions.  Serious changes in
59 // regular-expression syntax might require a total rethink.
60 //
61 
62 // ALTERED VERSION: Adapted to ANSI C and C++ for the OpenTracker
63 // project (www.opentracker.org), Jul 11, 2000.
64 
65 #ifndef _REG_EXP_H
66 #define _REG_EXP_H
67 
68 #include <String.h>
69 
70 namespace BPrivate {
71 
72 enum {
73 	REGEXP_UNMATCHED_PARENTHESIS = B_ERRORS_END,
74 	REGEXP_TOO_BIG,
75 	REGEXP_TOO_MANY_PARENTHESIS,
76 	REGEXP_JUNK_ON_END,
77 	REGEXP_STAR_PLUS_OPERAND_EMPTY,
78 	REGEXP_NESTED_STAR_QUESTION_PLUS,
79 	REGEXP_INVALID_BRACKET_RANGE,
80 	REGEXP_UNMATCHED_BRACKET,
81 	REGEXP_INTERNAL_ERROR,
82 	REGEXP_QUESTION_PLUS_STAR_FOLLOWS_NOTHING,
83 	REGEXP_TRAILING_BACKSLASH,
84 	REGEXP_CORRUPTED_PROGRAM,
85 	REGEXP_MEMORY_CORRUPTION,
86 	REGEXP_CORRUPTED_POINTERS,
87 	REGEXP_CORRUPTED_OPCODE
88 };
89 
90 const int32 kSubExpressionMax = 10;
91 
92 struct regexp {
93 	const char *startp[kSubExpressionMax];
94 	const char *endp[kSubExpressionMax];
95 	char regstart;		/* Internal use only. See RegExp.cpp for details. */
96 	char reganch;		/* Internal use only. */
97 	const char *regmust;/* Internal use only. */
98 	int regmlen;		/* Internal use only. */
99 	char program[1];	/* Unwarranted chumminess with compiler. */
100 };
101 
102 class RegExp {
103 
104 public:
105 	RegExp();
106 	RegExp(const char *);
107 	RegExp(const BString &);
108 	~RegExp();
109 
110 	status_t InitCheck() const;
111 
112 	status_t SetTo(const char*);
113 	status_t SetTo(const BString &);
114 
115 	bool Matches(const char *string) const;
116 	bool Matches(const BString &) const;
117 
118 	int32 RunMatcher(regexp *, const char *) const;
119 	regexp *Compile(const char *);
120 	regexp *Expression() const;
121 	const char *ErrorString() const;
122 
123 #ifdef DEBUG
124 	void Dump();
125 #endif
126 
127 private:
128 
129 	void SetError(status_t error) const;
130 
131 	// Working functions for Compile():
132 	char *Reg(int32, int32 *);
133 	char *Branch(int32 *);
134 	char *Piece(int32 *);
135 	char *Atom(int32 *);
136 	char *Node(char);
137 	char *Next(char *);
138 	const char *Next(const char *) const;
139 	void Char(char);
140 	void Insert(char, char *);
141 	void Tail(char *, char *);
142 	void OpTail(char *, char *);
143 
144 	// Working functions for RunMatcher():
145 	int32 Try(regexp *, const char *) const;
146 	int32 Match(const char *) const;
147 	int32 Repeat(const char *) const;
148 
149 	// Utility functions:
150 #ifdef DEBUG
151 	char *Prop(const char *) const;
152 	void RegExpError(const char *) const;
153 #endif
154 	inline int32 UCharAt(const char *p) const;
155 	inline char *Operand(char* p) const;
156 	inline const char *Operand(const char* p) const;
157 	inline bool	IsMult(char c) const;
158 
159 // --------- Variables -------------
160 
161 	mutable status_t fError;
162 	regexp *fRegExp;
163 
164 	// Work variables for Compile().
165 
166 	const char *fInputScanPointer;
167 	int32 fParenthesisCount;
168 	char fDummy;
169 	char *fCodeEmitPointer;		// &fDummy = don't.
170 	long fCodeSize;
171 
172 	// Work variables for RunMatcher().
173 
174 	mutable const char *fStringInputPointer;
175 	mutable const char *fRegBol;	// Beginning of input, for ^ check.
176 	mutable const char **fStartPArrayPointer;
177 	mutable const char **fEndPArrayPointer;
178 };
179 
180 } // namespace BPrivate
181 
182 using namespace BPrivate;
183 
184 #endif
185