~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to beagled/Lucene.Net/Analysis/Standard/FastCharStream.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
15
 
 */
16
 
 
17
 
using System;
18
 
namespace Lucene.Net.Analysis.Standard
19
 
{
20
 
        
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
27
 
        {
28
 
                internal char[] buffer = null;
29
 
                
30
 
                internal int bufferLength = 0; // end of valid chars
31
 
                internal int bufferPosition = 0; // next char to read
32
 
                
33
 
                internal int tokenStart = 0; // offset in buffer
34
 
                internal int bufferStart = 0; // position in file of buffer
35
 
                
36
 
                internal System.IO.TextReader input; // source of chars
37
 
                
38
 
                /// <summary>Constructs from a Reader. </summary>
39
 
                public FastCharStream(System.IO.TextReader r)
40
 
                {
41
 
                        input = r;
42
 
                }
43
 
                
44
 
                public int ReadChar()
45
 
                {
46
 
                        if (bufferPosition >= bufferLength)
47
 
                                if (!Refill())
48
 
                                        return -1;
49
 
                        return buffer[bufferPosition++];
50
 
                }
51
 
                
52
 
                private bool  Refill()
53
 
                {
54
 
                        int newPosition = bufferLength - tokenStart;
55
 
                        
56
 
                        if (tokenStart == 0)
57
 
                        {
58
 
                                // token won't fit in buffer
59
 
                                if (buffer == null)
60
 
                                {
61
 
                                        // first time: alloc buffer
62
 
                                        buffer = new char[2048];
63
 
                                }
64
 
                                else if (bufferLength == buffer.Length)
65
 
                                {
66
 
                                        // grow buffer
67
 
                                        char[] newBuffer = new char[buffer.Length * 2];
68
 
                                        Array.Copy(buffer, 0, newBuffer, 0, bufferLength);
69
 
                                        buffer = newBuffer;
70
 
                                }
71
 
                        }
72
 
                        else
73
 
                        {
74
 
                                // shift token to front
75
 
                                Array.Copy(buffer, tokenStart, buffer, 0, newPosition);
76
 
                        }
77
 
                        
78
 
                        bufferLength = newPosition; // update state
79
 
                        bufferPosition = newPosition;
80
 
                        bufferStart += tokenStart;
81
 
                        tokenStart = 0;
82
 
                        
83
 
                        int charsRead = input.Read(buffer, newPosition, buffer.Length - newPosition);
84
 
                        if (charsRead <= 0)
85
 
                                return false;
86
 
 
87
 
                        bufferLength += charsRead;
88
 
                        return true;
89
 
                }
90
 
                
91
 
                public int BeginToken()
92
 
                {
93
 
                        tokenStart = bufferPosition;
94
 
                        return ReadChar();
95
 
                }
96
 
                
97
 
                public void  Backup(int amount)
98
 
                {
99
 
                        bufferPosition -= amount;
100
 
                }
101
 
                
102
 
                public System.String GetImage()
103
 
                {
104
 
                        return new System.String(buffer, tokenStart, bufferPosition - tokenStart);
105
 
                }
106
 
                
107
 
                public char[] GetSuffix(int len)
108
 
                {
109
 
                        char[] value_Renamed = new char[len];
110
 
                        Array.Copy(buffer, bufferPosition - len, value_Renamed, 0, len);
111
 
                        return value_Renamed;
112
 
                }
113
 
                
114
 
                public void  Done()
115
 
                {
116
 
                        try
117
 
                        {
118
 
                                input.Close();
119
 
                        }
120
 
                        catch (System.IO.IOException e)
121
 
                        {
122
 
                                System.Console.Error.WriteLine("Caught: " + e + "; ignoring.");
123
 
                        }
124
 
                }
125
 
                
126
 
                public int GetColumn()
127
 
                {
128
 
                        return bufferStart + bufferPosition;
129
 
                }
130
 
                public int GetLine()
131
 
                {
132
 
                        return 1;
133
 
                }
134
 
                public int GetEndColumn()
135
 
                {
136
 
                        return bufferStart + bufferPosition;
137
 
                }
138
 
                public int GetEndLine()
139
 
                {
140
 
                        return 1;
141
 
                }
142
 
                public int GetBeginColumn()
143
 
                {
144
 
                        return bufferStart + tokenStart;
145
 
                }
146
 
                public int GetBeginLine()
147
 
                {
148
 
                        return 1;
149
 
                }
150
 
        }
151
 
}