QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / WildcardTermEnum.cs
blob6d1bd52fbb53774a780b4168433c20ce28772515
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.
17 using System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Term = Lucene.Net.Index.Term;
21 namespace Lucene.Net.Search
24 /// <summary> Subclass of FilteredTermEnum for enumerating all terms that match the
25 /// specified wildcard filter term.
26 /// <p>
27 /// Term enumerations are always ordered by Term.compareTo(). Each term in
28 /// the enumeration is greater than all that precede it.
29 ///
30 /// </summary>
31 /// <version> $Id: WildcardTermEnum.cs,v 1.4 2006/10/02 17:09:07 joeshaw Exp $
32 /// </version>
33 public class WildcardTermEnum : FilteredTermEnum
35 internal Term searchTerm;
36 internal System.String field = "";
37 internal System.String text = "";
38 internal System.String pre = "";
39 internal int preLen = 0;
40 internal bool endEnum = false;
42 /// <summary> Creates a new <code>WildcardTermEnum</code>. Passing in a
43 /// {@link Lucene.Net.index.Term Term} that does not contain a
44 /// <code>WILDCARD_CHAR</code> will cause an exception to be thrown.
45 /// <p>
46 /// After calling the constructor the enumeration is already pointing to the first
47 /// valid term if such a term exists.
48 /// </summary>
49 public WildcardTermEnum(IndexReader reader, Term term):base()
51 searchTerm = term;
52 field = searchTerm.Field();
53 text = searchTerm.Text();
55 int sidx = text.IndexOf((System.Char) WILDCARD_STRING);
56 int cidx = text.IndexOf((System.Char) WILDCARD_CHAR);
57 int idx = sidx;
58 if (idx == - 1)
60 idx = cidx;
62 else if (cidx >= 0)
64 idx = System.Math.Min(idx, cidx);
67 pre = searchTerm.Text().Substring(0, (idx) - (0));
68 preLen = pre.Length;
69 text = text.Substring(preLen);
70 SetEnum(reader.Terms(new Term(searchTerm.Field(), pre)));
73 protected internal override bool TermCompare(Term term)
75 if (field == term.Field())
77 System.String searchText = term.Text();
78 if (searchText.StartsWith(pre))
80 return WildcardEquals(text, 0, searchText, preLen);
83 endEnum = true;
84 return false;
87 public override float Difference()
89 return 1.0f;
92 public override bool EndEnum()
94 return endEnum;
97 /// <summary>*****************************************
98 /// String equality with support for wildcards
99 /// ******************************************
100 /// </summary>
102 public const char WILDCARD_STRING = '*';
103 public const char WILDCARD_CHAR = '?';
105 /// <summary> Determines if a word matches a wildcard pattern.
106 /// <small>Work released by Granta Design Ltd after originally being done on
107 /// company time.</small>
108 /// </summary>
109 public static bool WildcardEquals(System.String pattern, int patternIdx, System.String string_Renamed, int stringIdx)
111 int p = patternIdx;
113 for (int s = stringIdx; ; ++p, ++s)
115 // End of string yet?
116 bool sEnd = (s >= string_Renamed.Length);
117 // End of pattern yet?
118 bool pEnd = (p >= pattern.Length);
120 // If we're looking at the end of the string...
121 if (sEnd)
123 // Assume the only thing left on the pattern is/are wildcards
124 bool justWildcardsLeft = true;
126 // Current wildcard position
127 int wildcardSearchPos = p;
128 // While we haven't found the end of the pattern,
129 // and haven't encountered any non-wildcard characters
130 while (wildcardSearchPos < pattern.Length && justWildcardsLeft)
132 // Check the character at the current position
133 char wildchar = pattern[wildcardSearchPos];
135 // If it's not a wildcard character, then there is more
136 // pattern information after this/these wildcards.
137 if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
139 justWildcardsLeft = false;
141 else
143 // to prevent "cat" matches "ca??"
144 if (wildchar == WILDCARD_CHAR)
146 return false;
149 // Look at the next character
150 wildcardSearchPos++;
154 // This was a prefix wildcard search, and we've matched, so
155 // return true.
156 if (justWildcardsLeft)
158 return true;
162 // If we've gone past the end of the string, or the pattern,
163 // return false.
164 if (sEnd || pEnd)
166 break;
169 // Match a single character, so continue.
170 if (pattern[p] == WILDCARD_CHAR)
172 continue;
176 if (pattern[p] == WILDCARD_STRING)
178 // Look at the character beyond the '*'.
179 ++p;
180 // Examine the string, starting at the last character.
181 for (int i = string_Renamed.Length; i >= s; --i)
183 if (WildcardEquals(pattern, p, string_Renamed, i))
185 return true;
188 break;
190 if (pattern[p] != string_Renamed[s])
192 break;
195 return false;
198 public override void Close()
200 base.Close();
201 searchTerm = null;
202 field = null;
203 text = null;