**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.IO / StringReader.cs
blobc1b334d7296fc9a9c1f7820d73419ddd6a25c336
1 //
2 // System.IO.StringReader
3 //
4 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
5 //
6 // Copyright (C) 2004 Novell (http://www.novell.com)
7 //
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Globalization;
34 using System.Runtime.InteropServices;
36 namespace System.IO {
37 [Serializable]
38 public class StringReader : TextReader {
40 string source;
41 int nextChar;
42 int sourceLength;
44 public StringReader( string s ) {
46 if (s == null)
47 throw new ArgumentNullException ("s");
49 this.source = s;
50 nextChar = 0;
51 sourceLength = s.Length;
54 public override void Close ()
56 Dispose (true);
59 protected override void Dispose (bool disposing)
61 source = null;
62 base.Dispose (disposing);
65 public override int Peek() {
67 CheckObjectDisposedException ();
69 if( nextChar >= sourceLength ) {
70 return -1;
71 } else {
72 return (int)source[ nextChar ];
76 public override int Read() {
78 CheckObjectDisposedException ();
80 if( nextChar >= sourceLength ) {
81 return -1;
82 } else {
83 return (int)source[ nextChar++ ];
88 // The method will read up to count characters from the StringReader
89 // into the buffer character array starting at position index. Returns
90 // the actual number of characters read, or zero if the end of the string
91 // has been reached and no characters are read.
93 public override int Read ([In, Out] char[] buffer, int index, int count)
95 CheckObjectDisposedException ();
97 if (buffer == null)
98 throw new ArgumentNullException ("buffer");
99 if (buffer.Length - index < count)
100 throw new ArgumentException ();
101 if (index < 0 || count < 0)
102 throw new ArgumentOutOfRangeException ();
104 int charsToRead;
106 // reordered to avoir possible integer overflow
107 if (nextChar > sourceLength - count)
108 charsToRead = sourceLength - nextChar;
109 else
110 charsToRead = count;
112 source.CopyTo (nextChar, buffer, index, charsToRead);
114 nextChar += charsToRead;
116 return charsToRead;
119 public override string ReadLine ()
121 // Reads until next \r or \n or \r\n, otherwise return null
123 // LAMESPEC:
124 // The Beta 2 SDK help says that the ReadLine method
125 // returns "The next line from the input stream [...] A
126 // line is defined as a sequence of characters followed by
127 // a carriage return (\r), a line feed (\n), or a carriage
128 // return immediately followed by a line feed (\r\n).
129 // [...] The returned value is a null reference if the end
130 // of the input stream has been reached."
132 // HOWEVER, the MS implementation returns the rest of
133 // the string if no \r and/or \n is found in the string
135 CheckObjectDisposedException ();
137 if (nextChar >= source.Length)
138 return null;
140 int nextCR = source.IndexOf ('\r', nextChar);
141 int nextLF = source.IndexOf ('\n', nextChar);
142 int readTo;
143 bool consecutive = false;
145 if (nextCR == -1) {
146 if (nextLF == -1)
147 return ReadToEnd ();
149 readTo = nextLF;
150 } else if (nextLF == -1) {
151 readTo = nextCR;
152 } else {
153 readTo = (nextCR > nextLF) ? nextLF : nextCR;
154 consecutive = (nextCR + 1 == nextLF || nextLF + 1 == nextCR);
157 string nextLine = source.Substring (nextChar, readTo - nextChar);
158 nextChar = readTo + ((consecutive) ? 2 : 1);
159 return nextLine;
162 public override string ReadToEnd() {
164 CheckObjectDisposedException ();
165 string toEnd = source.Substring( nextChar, sourceLength - nextChar );
166 nextChar = sourceLength;
167 return toEnd;
170 private void CheckObjectDisposedException ()
172 if (source == null)
173 throw new ObjectDisposedException ("StringReader",
174 Locale.GetText ("Cannot read from a closed StringReader"));