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;
39 public class StringReader : TextReader {
45 public StringReader( string s ) {
48 throw new ArgumentNullException ("s");
52 sourceLength = s.Length;
55 public override void Close ()
60 protected override void Dispose (bool disposing)
63 base.Dispose (disposing);
66 public override int Peek() {
68 CheckObjectDisposedException ();
70 if( nextChar >= sourceLength ) {
73 return (int)source[ nextChar ];
77 public override int Read() {
79 CheckObjectDisposedException ();
81 if( nextChar >= sourceLength ) {
84 return (int)source[ nextChar++ ];
89 // The method will read up to count characters from the StringReader
90 // into the buffer character array starting at position index. Returns
91 // the actual number of characters read, or zero if the end of the string
92 // has been reached and no characters are read.
94 public override int Read ([In, Out] char[] buffer, int index, int count)
96 CheckObjectDisposedException ();
99 throw new ArgumentNullException ("buffer");
100 if (buffer.Length - index < count)
101 throw new ArgumentException ();
102 if (index < 0 || count < 0)
103 throw new ArgumentOutOfRangeException ();
107 // reordered to avoir possible integer overflow
108 if (nextChar > sourceLength - count)
109 charsToRead = sourceLength - nextChar;
113 source.CopyTo (nextChar, buffer, index, charsToRead);
115 nextChar += charsToRead;
120 public override string ReadLine ()
122 // Reads until next \r or \n or \r\n, otherwise return null
125 // The Beta 2 SDK help says that the ReadLine method
126 // returns "The next line from the input stream [...] A
127 // line is defined as a sequence of characters followed by
128 // a carriage return (\r), a line feed (\n), or a carriage
129 // return immediately followed by a line feed (\r\n).
130 // [...] The returned value is a null reference if the end
131 // of the input stream has been reached."
133 // HOWEVER, the MS implementation returns the rest of
134 // the string if no \r and/or \n is found in the string
136 CheckObjectDisposedException ();
138 if (nextChar >= source.Length)
141 int nextCR = source.IndexOf ('\r', nextChar);
142 int nextLF = source.IndexOf ('\n', nextChar);
144 bool consecutive = false;
151 } else if (nextLF == -1) {
154 readTo = (nextCR > nextLF) ? nextLF : nextCR;
155 consecutive = (nextCR + 1 == nextLF);
158 string nextLine = source.Substring (nextChar, readTo - nextChar);
159 nextChar = readTo + ((consecutive) ? 2 : 1);
163 public override string ReadToEnd() {
165 CheckObjectDisposedException ();
166 string toEnd = source.Substring( nextChar, sourceLength - nextChar );
167 nextChar = sourceLength;
171 private void CheckObjectDisposedException ()
174 throw new ObjectDisposedException ("StringReader",
175 Locale.GetText ("Cannot read from a closed StringReader"));