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.
19 namespace Lucene
.Net
.QueryParsers
22 /// <summary>An efficient implementation of JavaCC's CharStream interface. <p>Note that
23 /// this does not do line-number counting, but instead keeps track of the
24 /// character position of the token in the input, as required by Lucene's {@link
25 /// Lucene.Net.analysis.Token} API.
27 public sealed class FastCharStream
: CharStream
29 internal char[] buffer
= null;
31 internal int bufferLength
= 0; // end of valid chars
32 internal int bufferPosition
= 0; // next char to read
34 internal int tokenStart
= 0; // offset in buffer
35 internal int bufferStart
= 0; // position in file of buffer
37 internal System
.IO
.TextReader input
; // source of chars
39 /// <summary>Constructs from a Reader. </summary>
40 public FastCharStream(System
.IO
.TextReader r
)
45 public char ReadChar()
47 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
;
87 charsRead
= input
.Read(buffer
, newPosition
, buffer
.Length
- newPosition
);
94 throw new System
.IO
.IOException("read past eof");
96 bufferLength
+= charsRead
;
99 public char BeginToken()
101 tokenStart
= bufferPosition
;
105 public void Backup(int amount
)
107 bufferPosition
-= amount
;
110 public System
.String
GetImage()
112 return new System
.String(buffer
, tokenStart
, bufferPosition
- tokenStart
);
115 public char[] GetSuffix(int len
)
117 char[] value_Renamed
= new char[len
];
118 Array
.Copy(buffer
, bufferPosition
- len
, value_Renamed
, 0, len
);
119 return value_Renamed
;
128 catch (System
.IO
.IOException e
)
130 System
.Console
.Error
.WriteLine("Caught: " + e
+ "; ignoring.");
134 public int GetColumn()
136 return bufferStart
+ bufferPosition
;
142 public int GetEndColumn()
144 return bufferStart
+ bufferPosition
;
146 public int GetEndLine()
150 public int GetBeginColumn()
152 return bufferStart
+ tokenStart
;
154 public int GetBeginLine()