2 // System.IO.StringReader
4 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
6 // Copyright (C) 2004 Novell (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System.Globalization;
34 using System.Runtime.InteropServices;
38 public class StringReader : TextReader {
44 public StringReader( string s ) {
47 throw new ArgumentNullException ("s");
51 sourceLength = s.Length;
54 public override void Close ()
59 protected override void Dispose (bool disposing)
62 base.Dispose (disposing);
65 public override int Peek() {
67 CheckObjectDisposedException ();
69 if( nextChar >= sourceLength ) {
72 return (int)source[ nextChar ];
76 public override int Read() {
78 CheckObjectDisposedException ();
80 if( nextChar >= sourceLength ) {
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 ();
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 ();
106 // reordered to avoir possible integer overflow
107 if (nextChar > sourceLength - count)
108 charsToRead = sourceLength - nextChar;
112 source.CopyTo (nextChar, buffer, index, charsToRead);
114 nextChar += charsToRead;
119 public override string ReadLine ()
121 // Reads until next \r or \n or \r\n, otherwise return null
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)
140 int nextCR = source.IndexOf ('\r', nextChar);
141 int nextLF = source.IndexOf ('\n', nextChar);
143 bool consecutive = false;
150 } else if (nextLF == -1) {
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);
162 public override string ReadToEnd() {
164 CheckObjectDisposedException ();
165 string toEnd = source.Substring( nextChar, sourceLength - nextChar );
166 nextChar = sourceLength;
170 private void CheckObjectDisposedException ()
173 throw new ObjectDisposedException ("StringReader",
174 Locale.GetText ("Cannot read from a closed StringReader"));