2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using IndexReader
= Lucene
.Net
.Index
.IndexReader
;
18 using Term
= Lucene
.Net
.Index
.Term
;
19 namespace Lucene
.Net
.Search
22 /// <summary> Subclass of FilteredTermEnum for enumerating all terms that match the
23 /// specified wildcard filter term.
25 /// Term enumerations are always ordered by Term.compareTo(). Each term in
26 /// the enumeration is greater than all that precede it.
29 /// <version> $Id: WildcardTermEnum.cs,v 1.2 2005/01/17 19:54:30 joeshaw Exp $
31 public class WildcardTermEnum
:FilteredTermEnum
33 internal Term searchTerm
;
34 internal System
.String field
= "";
35 internal System
.String text
= "";
36 internal System
.String pre
= "";
37 internal int preLen
= 0;
38 internal bool fieldMatch
= false;
39 internal bool endEnum
= false;
41 /// <summary> Creates a new <code>WildcardTermEnum</code>. Passing in a
42 /// {@link Lucene.Net.Index.Term Term} that does not contain a
43 /// <code>WILDCARD_CHAR</code> will cause an exception to be thrown.
45 public WildcardTermEnum(IndexReader reader
, Term term
):base()
48 field
= searchTerm
.Field();
49 text
= searchTerm
.Text();
51 int sidx
= text
.IndexOf((System
.Char
) WILDCARD_STRING
);
52 int cidx
= text
.IndexOf((System
.Char
) WILDCARD_CHAR
);
60 idx
= System
.Math
.Min(idx
, cidx
);
63 pre
= searchTerm
.Text().Substring(0, (idx
) - (0));
65 text
= text
.Substring(preLen
);
66 SetEnum(reader
.Terms(new Term(searchTerm
.Field(), pre
)));
69 protected internal override bool TermCompare(Term term
)
71 if ((System
.Object
) field
== (System
.Object
) term
.Field())
73 System
.String searchText
= term
.Text();
74 if (searchText
.StartsWith(pre
))
76 return WildcardEquals(text
, 0, searchText
, preLen
);
83 public override float Difference()
88 public override bool EndEnum()
93 /// <summary>*****************************************
94 /// String equality with support for wildcards
95 /// ******************************************
98 public const char WILDCARD_STRING
= '*';
99 public const char WILDCARD_CHAR
= '?';
101 /// <summary> Determines if a word matches a wildcard pattern.
102 /// <small>Work released by Granta Design Ltd after originally being done on
103 /// company time.</small>
105 public static bool WildcardEquals(System
.String pattern
, int patternIdx
, System
.String string_Renamed
, int stringIdx
)
107 for (int p
= patternIdx
; ; ++p
)
109 for (int s
= stringIdx
; ; ++p
, ++s
)
111 // End of string yet?
112 bool sEnd
= (s
>= string_Renamed
.Length
);
113 // End of pattern yet?
114 bool pEnd
= (p
>= pattern
.Length
);
116 // If we're looking at the end of the string...
119 // Assume the only thing left on the pattern is/are wildcards
120 bool justWildcardsLeft
= true;
122 // Current wildcard position
123 int wildcardSearchPos
= p
;
124 // While we haven't found the end of the pattern,
125 // and haven't encountered any non-wildcard characters
126 while (wildcardSearchPos
< pattern
.Length
&& justWildcardsLeft
)
128 // Check the character at the current position
129 char wildchar
= pattern
[wildcardSearchPos
];
130 // If it's not a wildcard character, then there is more
131 // pattern information after this/these wildcards.
133 if (wildchar
!= WILDCARD_CHAR
&& wildchar
!= WILDCARD_STRING
)
135 justWildcardsLeft
= false;
139 // Look at the next character
144 // This was a prefix wildcard search, and we've matched, so
146 if (justWildcardsLeft
)
152 // If we've gone past the end of the string, or the pattern,
159 // Match a single character, so continue.
160 if (pattern
[p
] == WILDCARD_CHAR
)
166 if (pattern
[p
] == WILDCARD_STRING
)
168 // Look at the character beyond the '*'.
170 // Examine the string, starting at the last character.
171 for (int i
= string_Renamed
.Length
; i
>= s
; --i
)
173 if (WildcardEquals(pattern
, p
, string_Renamed
, i
))
180 if (pattern
[p
] != string_Renamed
[s
])
189 public override void Close()