cvsimport
[beagle.git] / beagled / Lucene.Net / Analysis / Standard / FastCharStream.cs
blobbc34cd98e8bffa7b58c499988d2962dea59b6c84
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 namespace Lucene.Net.Analysis.Standard
21 /// <summary>An efficient implementation of JavaCC's CharStream interface. <p>Note that
22 /// this does not do line-number counting, but instead keeps track of the
23 /// character position of the token in the input, as required by Lucene's {@link
24 /// Lucene.Net.analysis.Token} API.
25 /// </summary>
26 public sealed class FastCharStream : CharStream
28 internal char[] buffer = null;
30 internal int bufferLength = 0; // end of valid chars
31 internal int bufferPosition = 0; // next char to read
33 internal int tokenStart = 0; // offset in buffer
34 internal int bufferStart = 0; // position in file of buffer
36 internal System.IO.TextReader input; // source of chars
38 /// <summary>Constructs from a Reader. </summary>
39 public FastCharStream(System.IO.TextReader r)
41 input = r;
44 public int ReadChar()
46 if (bufferPosition >= bufferLength)
47 if (!Refill())
48 return -1;
49 return buffer[bufferPosition++];
52 private bool Refill()
54 int newPosition = bufferLength - tokenStart;
56 if (tokenStart == 0)
58 // token won't fit in buffer
59 if (buffer == null)
61 // first time: alloc buffer
62 buffer = new char[2048];
64 else if (bufferLength == buffer.Length)
66 // grow buffer
67 char[] newBuffer = new char[buffer.Length * 2];
68 Array.Copy(buffer, 0, newBuffer, 0, bufferLength);
69 buffer = newBuffer;
72 else
74 // shift token to front
75 Array.Copy(buffer, tokenStart, buffer, 0, newPosition);
78 bufferLength = newPosition; // update state
79 bufferPosition = newPosition;
80 bufferStart += tokenStart;
81 tokenStart = 0;
83 int charsRead = input.Read(buffer, newPosition, buffer.Length - newPosition);
84 if (charsRead <= 0)
85 return false;
87 bufferLength += charsRead;
88 return true;
91 public int BeginToken()
93 tokenStart = bufferPosition;
94 return ReadChar();
97 public void Backup(int amount)
99 bufferPosition -= amount;
102 public System.String GetImage()
104 return new System.String(buffer, tokenStart, bufferPosition - tokenStart);
107 public char[] GetSuffix(int len)
109 char[] value_Renamed = new char[len];
110 Array.Copy(buffer, bufferPosition - len, value_Renamed, 0, len);
111 return value_Renamed;
114 public void Done()
118 input.Close();
120 catch (System.IO.IOException e)
122 System.Console.Error.WriteLine("Caught: " + e + "; ignoring.");
126 public int GetColumn()
128 return bufferStart + bufferPosition;
130 public int GetLine()
132 return 1;
134 public int GetEndColumn()
136 return bufferStart + bufferPosition;
138 public int GetEndLine()
140 return 1;
142 public int GetBeginColumn()
144 return bufferStart + tokenStart;
146 public int GetBeginLine()
148 return 1;