xref: /haiku/src/kits/tracker/TrackerString.h (revision c90684742e7361651849be4116d0e5de3a817194)
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 #ifndef _TRACKER_STRING_H
36 #define _TRACKER_STRING_H
37 
38 #include <ctype.h>
39 
40 #include <OS.h>
41 #include <String.h>
42 
43 #include "RegExp.h"
44 
45 namespace BPrivate {
46 
47 enum TrackerStringExpressionType {
48 	kNone = B_ERROR,
49 	kStartsWith = 0,
50 	kEndsWith,
51 	kContains,
52 	kGlobMatch,
53 	kRegexpMatch
54 };
55 
56 class TrackerString : public BString
57 {
58 public:
59 	TrackerString();
60 	TrackerString(const char *);
61 	TrackerString(const TrackerString &);
62 	TrackerString(const char *, int32 maxLength);
63 	~TrackerString();
64 
65 	bool Matches(const char *, bool caseSensitivity = false,
66 		TrackerStringExpressionType expressionType = kGlobMatch) const;
67 
68 	bool MatchesRegExp(const char *, bool caseSensitivity = true) const;
69 	bool MatchesRegExp(const RegExp &) const;
70 	bool MatchesRegExp(const RegExp *) const;
71 
72 	bool MatchesGlob(const char *, bool caseSensitivity = false) const;
73 	bool EndsWith(const char *, bool caseSensitivity = false) const;
74 	bool StartsWith(const char *, bool caseSensitivity = false) const;
75 	bool Contains(const char *, bool caseSensitivity = false) const;
76 
77 	int32 FindFirst(const BString &) const;
78 	int32 FindFirst(const char *) const;
79 	int32 FindFirst(const BString &, int32 fromOffset) const;
80 	int32 FindFirst(const char *, int32 fromOffset) const;
81 	int32 FindFirst(char) const;
82 	int32 FindFirst(char, int32 fromOffset) const;
83 
84 	int32 FindLast(const BString &) const;
85 	int32 FindLast(const char *) const;
86 	int32 FindLast(const BString &, int32 beforeOffset) const;
87 	int32 FindLast(const char *, int32 beforeOffset) const;
88 	int32 FindLast(char) const;
89 	int32 FindLast(char, int32 beforeOffset) const;
90 
91 	int32 IFindFirst(const BString &) const;
92 	int32 IFindFirst(const char *) const;
93 	int32 IFindFirst(const BString &, int32 fromOffset) const;
94 	int32 IFindFirst(const char *, int32 fromOffset) const;
95 
96 	int32 IFindLast(const BString &) const;
97 	int32 IFindLast(const char *) const;
98 	int32 IFindLast(const BString &, int32 beforeOffset) const;
99 	int32 IFindLast(const char *, int32 beforeOffset) const;
100 
101 private:
102 	bool IsGlyph(char) const;
103 	bool IsInsideGlyph(char) const; // Not counting start!
104 	bool IsStartOfGlyph(char) const;
105 	const char *MoveToEndOfGlyph(const char *) const;
106 
107 	// Functions for Glob matching:
108 	bool MatchesBracketExpression(const char *string, const char *pattern,
109 		bool caseSensitivity) const;
110 	bool StringMatchesPattern(const char *string, const char *pattern,
111 		bool caseSensitivity) const;
112 
113 	char ConditionalToLower(char c, bool toLower) const;
114 	bool CharsAreEqual(char char1, char char2, bool toLower) const;
115 	bool UTF8CharsAreEqual(const char *string1, const char *string2) const;
116 };
117 
118 inline bool
119 TrackerString::MatchesRegExp(const RegExp *expression) const
120 {
121 	if (expression == NULL || expression->InitCheck() != B_OK)
122 		return false;
123 
124 	return expression->Matches(*this);
125 }
126 
127 inline bool
128 TrackerString::MatchesRegExp(const RegExp &expression) const
129 {
130 	if (expression.InitCheck() != B_OK)
131 		return false;
132 
133 	return expression.Matches(*this);
134 }
135 
136 inline char
137 TrackerString::ConditionalToLower(char c, bool caseSensitivity) const
138 {
139 	return caseSensitivity ? c : (char)tolower(c);
140 }
141 
142 inline bool
143 TrackerString::CharsAreEqual(char char1, char char2, bool caseSensitivity) const
144 {
145 	return ConditionalToLower(char1, caseSensitivity)
146 		== ConditionalToLower(char2, caseSensitivity);
147 }
148 
149 } // namespace BPrivate
150 
151 using namespace BPrivate;
152 
153 #endif
154