xref: /haiku/src/kits/tracker/TrackerString.h (revision db41683495bfde817554415d14ae6f9cc91e77eb)
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 _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 
46 namespace BPrivate {
47 
48 enum TrackerStringExpressionType {
49 	kNone = B_ERROR,
50 	kStartsWith = 0,
51 	kEndsWith,
52 	kContains,
53 	kGlobMatch,
54 	kRegexpMatch
55 };
56 
57 
58 class TrackerString : public BString
59 {
60 public:
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 
103 private:
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 
122 inline bool
123 TrackerString::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 
132 inline bool
133 TrackerString::MatchesRegExp(const RegExp &expression) const
134 {
135 	if (expression.InitCheck() != B_OK)
136 		return false;
137 
138 	return expression.Matches(*this);
139 }
140 
141 
142 inline char
143 TrackerString::ConditionalToLower(char c, bool caseSensitivity) const
144 {
145 	return caseSensitivity ? c : (char)tolower(c);
146 }
147 
148 
149 inline bool
150 TrackerString::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 
159 using namespace BPrivate;
160 
161 #endif	// _TRACKER_STRING_H
162