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 {
40 private string source;
41 private char[] sourceChars;
44 private int sourceLength;
45 private bool disposed = false;
47 public StringReader( string s ) {
50 throw new ArgumentNullException ("s");
54 sourceLength = s.Length;
55 sourceChars = s.ToCharArray();
58 public override void Close() {
63 protected override void Dispose (bool disposing)
66 base.Dispose (disposing);
69 public override int Peek() {
71 CheckObjectDisposedException ();
73 if( nextChar >= sourceLength ) {
76 return (int)source[ nextChar ];
80 public override int Read() {
82 CheckObjectDisposedException ();
84 if( nextChar >= sourceLength ) {
87 return (int)source[ nextChar++ ];
92 // The method will read up to count characters from the StringReader
93 // into the buffer character array starting at position index. Returns
94 // the actual number of characters read, or zero if the end of the string
95 // has been reached and no characters are read.
97 public override int Read ([In, Out] char[] buffer, int index, int count )
99 CheckObjectDisposedException ();
101 if( buffer == null ) {
102 throw new ArgumentNullException ("buffer");
103 } else if( buffer.Length - index < count ) {
104 throw new ArgumentException();
105 } else if( index < 0 || count < 0 ) {
106 throw new ArgumentOutOfRangeException();
111 // reordered to avoir possible integer overflow
112 if (nextChar > sourceLength - count) {
113 charsToRead = sourceLength - nextChar;
118 Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );
120 nextChar += charsToRead;
125 public override string ReadLine ()
127 // Reads until next \r or \n or \r\n, otherwise return null
130 // The Beta 2 SDK help says that the ReadLine method
131 // returns "The next line from the input stream [...] A
132 // line is defined as a sequence of characters followed by
133 // a carriage return (\r), a line feed (\n), or a carriage
134 // return immediately followed by a line feed (\r\n).
135 // [...] The returned value is a null reference if the end
136 // of the input stream has been reached."
138 // HOWEVER, the MS implementation returns the rest of
139 // the string if no \r and/or \n is found in the string
141 CheckObjectDisposedException ();
143 if (nextChar >= source.Length)
146 int nextCR = source.IndexOf ('\r', nextChar);
147 int nextLF = source.IndexOf ('\n', nextChar);
149 bool consecutive = false;
156 } else if (nextLF == -1) {
159 readTo = (nextCR > nextLF) ? nextLF : nextCR;
160 consecutive = (nextCR + 1 == nextLF || nextLF + 1 == nextCR);
163 string nextLine = source.Substring (nextChar, readTo - nextChar);
164 nextChar = readTo + ((consecutive) ? 2 : 1);
168 public override string ReadToEnd() {
170 CheckObjectDisposedException ();
171 string toEnd = source.Substring( nextChar, sourceLength - nextChar );
172 nextChar = sourceLength;
176 private void CheckObjectDisposedException ()
179 throw new ObjectDisposedException ("StringReader",
180 Locale.GetText ("Cannot read from a closed StringReader"));