Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / QueryParser / FastCharStream.cs
blobcff8c5a397b69f0934157ffffcd66efa488969d9
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 namespace Lucene.Net.QueryParsers
20 /// <summary>An efficient implementation of JavaCC's CharStream interface. <p>Note that
21 /// this does not do line-number counting, but instead keeps track of the
22 /// character position of the token in the input, as required by Lucene's {@link
23 /// Lucene.Net.Analysis.Token} API.
24 /// </summary>
25 public sealed class FastCharStream : CharStream
27 internal char[] buffer = null;
29 internal int bufferLength = 0; // end of valid chars
30 internal int bufferPosition = 0; // next char to read
32 internal int tokenStart = 0; // offset in buffer
33 internal int bufferStart = 0; // position in file of buffer
35 internal System.IO.TextReader input; // source of chars
37 /// <summary>Constructs from a Reader. </summary>
38 public FastCharStream(System.IO.TextReader r)
40 input = r;
43 public char ReadChar()
45 if (bufferPosition >= bufferLength)
46 Refill();
47 return buffer[bufferPosition++];
50 private void Refill()
52 int newPosition = bufferLength - tokenStart;
54 if (tokenStart == 0)
56 // token won't fit in buffer
57 if (buffer == null)
59 // first time: alloc buffer
60 buffer = new char[2048];
62 else if (bufferLength == buffer.Length)
64 // grow buffer
65 char[] newBuffer = new char[buffer.Length * 2];
66 Array.Copy(buffer, 0, newBuffer, 0, bufferLength);
67 buffer = newBuffer;
70 else
72 // shift token to front
73 Array.Copy(buffer, tokenStart, buffer, 0, newPosition);
76 bufferLength = newPosition; // update state
77 bufferPosition = newPosition;
78 bufferStart += tokenStart;
79 tokenStart = 0;
81 int charsRead = 0;
83 try
85 charsRead = input.Read(buffer, newPosition, buffer.Length - newPosition);
87 catch
91 if (charsRead <= 0)
92 throw new System.IO.IOException("read past eof");
93 else
94 bufferLength += charsRead;
97 public char BeginToken()
99 tokenStart = bufferPosition;
100 return ReadChar();
103 public void Backup(int amount)
105 bufferPosition -= amount;
108 public System.String GetImage()
110 return new System.String(buffer, tokenStart, bufferPosition - tokenStart);
113 public char[] GetSuffix(int len)
115 char[] value_Renamed = new char[len];
116 Array.Copy(buffer, bufferPosition - len, value_Renamed, 0, len);
117 return value_Renamed;
120 public void Done()
124 input.Close();
126 catch (System.IO.IOException e)
128 System.Console.Error.WriteLine("Caught: " + e + "; ignoring.");
132 public int GetColumn()
134 return bufferStart + bufferPosition;
136 public int GetLine()
138 return 1;
140 public int GetEndColumn()
142 return bufferStart + bufferPosition;
144 public int GetEndLine()
146 return 1;
148 public int GetBeginColumn()
150 return bufferStart + tokenStart;
152 public int GetBeginLine()
154 return 1;