1285031Sdes/*
2285031SdesOpen Tracker License
3285031Sdes
4285031SdesTerms and Conditions
5285031Sdes
6285031SdesCopyright (c) 1991-2000, Be Incorporated. All rights reserved.
7285031Sdes
8285031SdesPermission is hereby granted, free of charge, to any person obtaining a copy of
9285031Sdesthis software and associated documentation files (the "Software"), to deal in
10285031Sdesthe Software without restriction, including without limitation the rights to
11285031Sdesuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12285031Sdesof the Software, and to permit persons to whom the Software is furnished to do
13285031Sdesso, subject to the following conditions:
14285031Sdes
15285031SdesThe above copyright notice and this permission notice applies to all licensees
16285031Sdesand shall be included in all copies or substantial portions of the Software.
17285031Sdes
18285031SdesTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19285031SdesIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20285031SdesFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21285031SdesBE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22285031SdesAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23285031SdesWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24285031Sdes
25285031SdesExcept as contained in this notice, the name of Be Incorporated shall not be
26285031Sdesused in advertising or otherwise to promote the sale, use or other dealings in
27285031Sdesthis Software without prior written authorization from Be Incorporated.
28285031Sdes
29285031SdesTracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30285031Sdesof Be Incorporated in the United States and other countries. Other brand product
31285031Sdesnames are registered trademarks or trademarks of their respective holders.
32285031SdesAll rights reserved.
33285031Sdes*/
34285031Sdes#ifndef _REG_EXP_H
35285031Sdes#define _REG_EXP_H
36285031Sdes
37285031Sdes
38285031Sdes// This code is based on regexp.c, v.1.3 by Henry Spencer:
39285031Sdes
40285031Sdes// @(#)regexp.c	1.3 of 18 April 87
41285031Sdes//
42285031Sdes//	Copyright (c) 1986 by University of Toronto.
43285031Sdes//	Written by Henry Spencer.  Not derived from licensed software.
44285031Sdes//
45285031Sdes//	Permission is granted to anyone to use this software for any
46285031Sdes//	purpose on any computer system, and to redistribute it freely,
47285031Sdes//	subject to the following restrictions:
48285031Sdes//
49285031Sdes//	1. The author is not responsible for the consequences of use of
50285031Sdes//		this software, no matter how awful, even if they arise
51285031Sdes//		from defects in it.
52285031Sdes//
53285031Sdes//	2. The origin of this software must not be misrepresented, either
54285031Sdes//		by explicit claim or by omission.
55285031Sdes//
56285031Sdes//	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
71namespace BPrivate {
72
73
74enum {
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
92const int32 kSubExpressionMax = 10;
93
94struct 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
105class RegExp {
106public:
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
129private:
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
184using namespace BPrivate;
185
186#endif	// _REG_EXP_H
187