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.
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.
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
)
43 public char ReadChar()
45 if (bufferPosition
>= bufferLength
)
47 return buffer
[bufferPosition
++];
52 int newPosition
= bufferLength
- tokenStart
;
56 // token won't fit in buffer
59 // first time: alloc buffer
60 buffer
= new char[2048];
62 else if (bufferLength
== buffer
.Length
)
65 char[] newBuffer
= new char[buffer
.Length
* 2];
66 Array
.Copy(buffer
, 0, newBuffer
, 0, bufferLength
);
72 // shift token to front
73 Array
.Copy(buffer
, tokenStart
, buffer
, 0, newPosition
);
76 bufferLength
= newPosition
; // update state
77 bufferPosition
= newPosition
;
78 bufferStart
+= tokenStart
;
85 charsRead
= input
.Read(buffer
, newPosition
, buffer
.Length
- newPosition
);
92 throw new System
.IO
.IOException("read past eof");
94 bufferLength
+= charsRead
;
97 public char BeginToken()
99 tokenStart
= bufferPosition
;
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
;
126 catch (System
.IO
.IOException e
)
128 System
.Console
.Error
.WriteLine("Caught: " + e
+ "; ignoring.");
132 public int GetColumn()
134 return bufferStart
+ bufferPosition
;
140 public int GetEndColumn()
142 return bufferStart
+ bufferPosition
;
144 public int GetEndLine()
148 public int GetBeginColumn()
150 return bufferStart
+ tokenStart
;
152 public int GetBeginLine()