Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Search / WildcardTermEnum.cs
blob905a00d0a94203660bfadc03a134862450a4d285
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
16 using System;
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.
24 /// <p>
25 /// Term enumerations are always ordered by Term.compareTo(). Each term in
26 /// the enumeration is greater than all that precede it.
27 ///
28 /// </summary>
29 /// <version> $Id: WildcardTermEnum.cs,v 1.3 2005/10/06 19:29:57 dsd Exp $
30 /// </version>
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 endEnum = false;
40 /// <summary> Creates a new <code>WildcardTermEnum</code>. Passing in a
41 /// {@link Lucene.Net.Index.Term Term} that does not contain a
42 /// <code>WILDCARD_CHAR</code> will cause an exception to be thrown.
43 /// <p>
44 /// After calling the constructor the enumeration is already pointing to the first
45 /// valid term if such a term exists.
46 /// </summary>
47 public WildcardTermEnum(IndexReader reader, Term term):base()
49 searchTerm = term;
50 field = searchTerm.Field();
51 text = searchTerm.Text();
53 int sidx = text.IndexOf((System.Char) WILDCARD_STRING);
54 int cidx = text.IndexOf((System.Char) WILDCARD_CHAR);
55 int idx = sidx;
56 if (idx == - 1)
58 idx = cidx;
60 else if (cidx >= 0)
62 idx = System.Math.Min(idx, cidx);
65 pre = searchTerm.Text().Substring(0, (idx) - (0));
66 preLen = pre.Length;
67 text = text.Substring(preLen);
68 SetEnum(reader.Terms(new Term(searchTerm.Field(), pre)));
71 protected internal override bool TermCompare(Term term)
73 if ((System.Object) field == (System.Object) term.Field())
75 System.String searchText = term.Text();
76 if (searchText.StartsWith(pre))
78 return WildcardEquals(text, 0, searchText, preLen);
81 endEnum = true;
82 return false;
85 public override float Difference()
87 return 1.0f;
90 public override bool EndEnum()
92 return endEnum;
95 /// <summary>*****************************************
96 /// String equality with support for wildcards
97 /// ******************************************
98 /// </summary>
100 public const char WILDCARD_STRING = '*';
101 public const char WILDCARD_CHAR = '?';
103 /// <summary> Determines if a word matches a wildcard pattern.
104 /// <small>Work released by Granta Design Ltd after originally being done on
105 /// company time.</small>
106 /// </summary>
107 public static bool WildcardEquals(System.String pattern, int patternIdx, System.String string_Renamed, int stringIdx)
109 int p = patternIdx;
111 for (int s = stringIdx; ; ++p, ++s)
113 // End of string yet?
114 bool sEnd = (s >= string_Renamed.Length);
115 // End of pattern yet?
116 bool pEnd = (p >= pattern.Length);
118 // If we're looking at the end of the string...
119 if (sEnd)
121 // Assume the only thing left on the pattern is/are wildcards
122 bool justWildcardsLeft = true;
124 // Current wildcard position
125 int wildcardSearchPos = p;
126 // While we haven't found the end of the pattern,
127 // and haven't encountered any non-wildcard characters
128 while (wildcardSearchPos < pattern.Length && justWildcardsLeft)
130 // Check the character at the current position
131 char wildchar = pattern[wildcardSearchPos];
132 // If it's not a wildcard character, then there is more
133 // pattern information after this/these wildcards.
135 if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
137 justWildcardsLeft = false;
139 else
141 // Look at the next character
142 wildcardSearchPos++;
146 // This was a prefix wildcard search, and we've matched, so
147 // return true.
148 if (justWildcardsLeft)
150 return true;
154 // If we've gone past the end of the string, or the pattern,
155 // return false.
156 if (sEnd || pEnd)
158 break;
161 // Match a single character, so continue.
162 if (pattern[p] == WILDCARD_CHAR)
164 continue;
168 if (pattern[p] == WILDCARD_STRING)
170 // Look at the character beyond the '*'.
171 ++p;
172 // Examine the string, starting at the last character.
173 for (int i = string_Renamed.Length; i >= s; --i)
175 if (WildcardEquals(pattern, p, string_Renamed, i))
177 return true;
180 break;
182 if (pattern[p] != string_Renamed[s])
184 break;
187 return false;
190 public override void Close()
192 base.Close();
193 searchTerm = null;
194 field = null;
195 text = null;