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.
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.
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
)
46 if (bufferPosition
>= bufferLength
)
49 return buffer
[bufferPosition
++];
54 int newPosition
= bufferLength
- tokenStart
;
58 // token won't fit in buffer
61 // first time: alloc buffer
62 buffer
= new char[2048];
64 else if (bufferLength
== buffer
.Length
)
67 char[] newBuffer
= new char[buffer
.Length
* 2];
68 Array
.Copy(buffer
, 0, newBuffer
, 0, bufferLength
);
74 // shift token to front
75 Array
.Copy(buffer
, tokenStart
, buffer
, 0, newPosition
);
78 bufferLength
= newPosition
; // update state
79 bufferPosition
= newPosition
;
80 bufferStart
+= tokenStart
;
83 int charsRead
= input
.Read(buffer
, newPosition
, buffer
.Length
- newPosition
);
87 bufferLength
+= charsRead
;
91 public int BeginToken()
93 tokenStart
= bufferPosition
;
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
;
120 catch (System
.IO
.IOException e
)
122 System
.Console
.Error
.WriteLine("Caught: " + e
+ "; ignoring.");
126 public int GetColumn()
128 return bufferStart
+ bufferPosition
;
134 public int GetEndColumn()
136 return bufferStart
+ bufferPosition
;
138 public int GetEndLine()
142 public int GetBeginColumn()
144 return bufferStart
+ tokenStart
;
146 public int GetBeginLine()