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 private:
80 bool IsGlyph(char) const;
81 bool IsInsideGlyph(char) const;
82 // Not counting start!
83 bool IsStartOfGlyph(char) const;
84 const char* MoveToEndOfGlyph(const char*) const;
85
86 // Functions for Glob matching:
87 bool MatchesBracketExpression(const char* string, const char* pattern,
88 bool caseSensitivity) const;
89 bool StringMatchesPattern(const char* string, const char* pattern,
90 bool caseSensitivity) const;
91
92 char ConditionalToLower(char c, bool toLower) const;
93 bool CharsAreEqual(char char1, char char2, bool toLower) const;
94 bool UTF8CharsAreEqual(const char* string1, const char* string2) const;
95 };
96
97
98 inline bool
MatchesRegExp(const RegExp * expression)99 TrackerString::MatchesRegExp(const RegExp* expression) const
100 {
101 if (expression == NULL || expression->InitCheck() != B_OK)
102 return false;
103
104 return expression->Matches(*this);
105 }
106
107
108 inline bool
MatchesRegExp(const RegExp & expression)109 TrackerString::MatchesRegExp(const RegExp &expression) const
110 {
111 if (expression.InitCheck() != B_OK)
112 return false;
113
114 return expression.Matches(*this);
115 }
116
117
118 inline char
ConditionalToLower(char c,bool caseSensitivity)119 TrackerString::ConditionalToLower(char c, bool caseSensitivity) const
120 {
121 return caseSensitivity ? c : (char)tolower(c);
122 }
123
124
125 inline bool
CharsAreEqual(char char1,char char2,bool caseSensitivity)126 TrackerString::CharsAreEqual(char char1, char char2,
127 bool caseSensitivity) const
128 {
129 return ConditionalToLower(char1, caseSensitivity)
130 == ConditionalToLower(char2, caseSensitivity);
131 }
132
133 } // namespace BPrivate
134
135 using namespace BPrivate;
136
137
138 #endif // _TRACKER_STRING_H
139