Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Index / ReusableStringReader.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 namespace Mono.Lucene.Net.Index
21 {
22         
23         /// <summary>Used by DocumentsWriter to implemented a StringReader
24         /// that can be reset to a new string; we use this when
25         /// tokenizing the string value from a Field. 
26         /// </summary>
27     sealed class ReusableStringReader : System.IO.TextReader
28     {
29         internal int upto;
30         internal int left;
31         internal System.String s;
32         internal void Init(System.String s)
33         {
34             this.s = s;
35             left = s.Length;
36             this.upto = 0;
37         }
38         public int Read(char[] c)
39         {
40             return Read(c, 0, c.Length);
41         }
42         public override int Read(System.Char[] c, int off, int len)
43         {
44             if (left > len)
45             {
46                 SupportClass.TextSupport.GetCharsFromString(s, upto, upto + len, c, off);
47                 upto += len;
48                 left -= len;
49                 return len;
50             }
51             else if (0 == left)
52             {
53                 s = null;
54                 return 0;
55             }
56             else
57             {
58                 SupportClass.TextSupport.GetCharsFromString(s, upto, upto + left, c, off);
59                 int r = left;
60                 left = 0;
61                 upto = s.Length;
62                 return r;
63             }
64         }
65         public override void Close()
66         {
67         }
68
69
70         public override int Read()
71         {
72             if (left > 0)
73             {
74                 char ch = s[upto];
75                 upto += 1;
76                 left -= 1;
77                 return (int)ch;
78             }
79             return -1;
80         }
81
82         public override int ReadBlock(char[] buffer, int index, int count)
83         {
84             return Read(buffer, index, count);
85         }
86
87         public override string ReadLine()
88         {
89             int i;
90             for (i = upto; i < s.Length; i++)
91             {
92                 char c = s[i];
93                 if (c == '\r' || c == '\n')
94                 {
95                     string result = s.Substring(upto, i - upto);
96                     upto = i + 1;
97                     left = s.Length - upto;
98                     if (c == '\r' && upto < s.Length && s[upto] == '\n')
99                     {
100                         upto++;
101                         left--;
102                     }
103                     return result;
104                 }
105             }
106             if (i > upto)
107             {
108                 return ReadToEnd();
109             }
110             return null;
111         }
112
113         public override int Peek()
114         {
115             if (left > 0)
116             {
117                 return (int)s[upto];
118             }
119             return -1;
120         }
121
122         public override string ReadToEnd()
123         {
124             string result = s.Substring(upto, left);
125             left = 0;
126             upto = s.Length - 1;
127             return result;
128         }
129     }
130 }