1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All rights reserved.
33*/
34#ifndef _TRACKER_STRING_H
35#define _TRACKER_STRING_H
36
37
38#include <ctype.h>
39
40#include <OS.h>
41#include <String.h>
42
43#include "RegExp.h"
44
45
46namespace BPrivate {
47
48enum TrackerStringExpressionType {
49	kNone = B_ERROR,
50	kStartsWith = 0,
51	kEndsWith,
52	kContains,
53	kGlobMatch,
54	kRegexpMatch
55};
56
57
58class TrackerString : public BString
59{
60public:
61	TrackerString();
62	TrackerString(const char*);
63	TrackerString(const TrackerString&);
64	TrackerString(const char*, int32 maxLength);
65	~TrackerString();
66
67	bool Matches(const char*, bool caseSensitivity = false,
68		TrackerStringExpressionType expressionType = kGlobMatch) const;
69
70	bool MatchesRegExp(const char*, bool caseSensitivity = true) const;
71	bool MatchesRegExp(const RegExp&) const;
72	bool MatchesRegExp(const RegExp*) const;
73
74	bool MatchesGlob(const char*, bool caseSensitivity = false) const;
75	bool EndsWith(const char*, bool caseSensitivity = false) const;
76	bool StartsWith(const char*, bool caseSensitivity = false) const;
77	bool Contains(const char*, bool caseSensitivity = false) const;
78
79	int32 FindFirst(const BString&) const;
80	int32 FindFirst(const char*) const;
81	int32 FindFirst(const BString&, int32 fromOffset) const;
82	int32 FindFirst(const char*, int32 fromOffset) const;
83	int32 FindFirst(char) const;
84	int32 FindFirst(char, int32 fromOffset) const;
85
86	int32 FindLast(const BString&) const;
87	int32 FindLast(const char*) const;
88	int32 FindLast(const BString&, int32 beforeOffset) const;
89	int32 FindLast(const char*, int32 beforeOffset) const;
90	int32 FindLast(char) const;
91	int32 FindLast(char, int32 beforeOffset) const;
92
93	int32 IFindFirst(const BString&) const;
94	int32 IFindFirst(const char*) const;
95	int32 IFindFirst(const BString&, int32 fromOffset) const;
96	int32 IFindFirst(const char*, int32 fromOffset) const;
97
98	int32 IFindLast(const BString&) const;
99	int32 IFindLast(const char*) const;
100	int32 IFindLast(const BString&, int32 beforeOffset) const;
101	int32 IFindLast(const char*, int32 beforeOffset) const;
102
103private:
104	bool IsGlyph(char) const;
105	bool IsInsideGlyph(char) const;
106		// Not counting start!
107	bool IsStartOfGlyph(char) const;
108	const char* MoveToEndOfGlyph(const char*) const;
109
110	// Functions for Glob matching:
111	bool MatchesBracketExpression(const char* string, const char* pattern,
112		bool caseSensitivity) const;
113	bool StringMatchesPattern(const char* string, const char* pattern,
114		bool caseSensitivity) const;
115
116	char ConditionalToLower(char c, bool toLower) const;
117	bool CharsAreEqual(char char1, char char2, bool toLower) const;
118	bool UTF8CharsAreEqual(const char* string1, const char* string2) const;
119};
120
121
122inline bool
123TrackerString::MatchesRegExp(const RegExp* expression) const
124{
125	if (expression == NULL || expression->InitCheck() != B_OK)
126		return false;
127
128	return expression->Matches(*this);
129}
130
131
132inline bool
133TrackerString::MatchesRegExp(const RegExp &expression) const
134{
135	if (expression.InitCheck() != B_OK)
136		return false;
137
138	return expression.Matches(*this);
139}
140
141
142inline char
143TrackerString::ConditionalToLower(char c, bool caseSensitivity) const
144{
145	return caseSensitivity ? c : (char)tolower(c);
146}
147
148
149inline bool
150TrackerString::CharsAreEqual(char char1, char char2,
151	bool caseSensitivity) const
152{
153	return ConditionalToLower(char1, caseSensitivity)
154		== ConditionalToLower(char2, caseSensitivity);
155}
156
157} // namespace BPrivate
158
159using namespace BPrivate;
160
161#endif	// _TRACKER_STRING_H
162