Merge pull request #681 from tritao/dll-api
[mono.git] / mcs / class / corlib / System.IO / StringReader.cs
index c1b334d7296fc9a9c1f7820d73419ddd6a25c336..e182d71b7b51ec0dba8a1abc13bb38b4b0b262b3 100644 (file)
@@ -1,13 +1,13 @@
 //
 // System.IO.StringReader
 //
-// Author: Marcin Szczepanski (marcins@zipworld.com.au)
-//
-// Copyright (C) 2004 Novell (http://www.novell.com)
-// 
-
+// Authors:
+//   Marcin Szczepanski (marcins@zipworld.com.au)
+//   Marek Safar (marek.safar@gmail.com)
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright 2011 Xamarin Inc.
+//
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 using System;
 using System.Globalization;
 using System.Runtime.InteropServices;
+#if NET_4_5
+using System.Threading.Tasks;
+#endif
 
 namespace System.IO {
        [Serializable]
+       [ComVisible (true)]
        public class StringReader : TextReader {
 
                string source;
                int nextChar;
                int sourceLength;
+               static char[] cr_lf;
 
-               public StringReader( string s ) {
-
+               public StringReader (string s)
+               {
                        if (s == null) 
                                throw new ArgumentNullException ("s");
 
@@ -62,29 +67,26 @@ namespace System.IO {
                        base.Dispose (disposing);
                }
 
-               public override int Peek() {
-
-                       CheckObjectDisposedException ();
+               public override int Peek ()
+               {
+                       if (source == null)
+                               ObjectDisposedException ();
 
-                       if( nextChar >= sourceLength ) {
+                       if (nextChar >= sourceLength) 
                                return -1;
-                       } else {
-                               return (int)source[ nextChar ];
-                       }
+                       return (int)source [nextChar];
                }
 
-               public override int Read() {
-
-                       CheckObjectDisposedException ();
+               public override int Read ()
+               {
+                       if (source == null)
+                               ObjectDisposedException ();
 
-                       if( nextChar >= sourceLength ) {
+                       if (nextChar >= sourceLength)
                                return -1;
-                       } else {
-                               return (int)source[ nextChar++ ];
-                       }
+                       return (int)source [nextChar++];
                }
 
-
                // The method will read up to count characters from the StringReader
                // into the buffer character array starting at position index. Returns
                // the actual number of characters read, or zero if the end of the string
@@ -92,7 +94,8 @@ namespace System.IO {
 
                public override int Read ([In, Out] char[] buffer, int index, int count)
                {
-                       CheckObjectDisposedException ();
+                       if (source == null)
+                               ObjectDisposedException ();
 
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
@@ -120,58 +123,68 @@ namespace System.IO {
                {
                        // Reads until next \r or \n or \r\n, otherwise return null
 
-                       // LAMESPEC:
-                       // The Beta 2 SDK help says that the ReadLine method
-                       // returns "The next line from the input stream [...] A
-                       // line is defined as a sequence of characters followed by
-                       // a carriage return (\r), a line feed (\n), or a carriage
-                       // return immediately followed by a line feed (\r\n).
-                       // [...] The returned value is a null reference if the end
-                       // of the input stream has been reached."
-                       //
-                       // HOWEVER, the MS implementation returns the rest of
-                       // the string if no \r and/or \n is found in the string
-
-                       CheckObjectDisposedException ();
+                       if (source == null)
+                               ObjectDisposedException ();
 
                        if (nextChar >= source.Length)
                                return null;
 
-                       int nextCR = source.IndexOf ('\r', nextChar);
-                       int nextLF = source.IndexOf ('\n', nextChar);
-                       int readTo;
-                       bool consecutive = false;
-
-                       if (nextCR == -1) {
-                               if (nextLF == -1)
-                                       return ReadToEnd ();
-
-                               readTo = nextLF;
-                       } else if (nextLF == -1) {
-                               readTo = nextCR;
-                       } else {
-                               readTo = (nextCR > nextLF) ? nextLF : nextCR;
-                               consecutive = (nextCR + 1 == nextLF || nextLF + 1 == nextCR);
-                       }
-
-                       string nextLine = source.Substring (nextChar, readTo - nextChar);
-                       nextChar = readTo + ((consecutive) ? 2 : 1);
+                       if (cr_lf == null)
+                               cr_lf = new char [] { '\n', '\r' };
+                       
+                       int readto = source.IndexOfAny (cr_lf, nextChar);
+                       
+                       if (readto == -1)
+                               return ReadToEnd ();
+
+                       bool consecutive = source[readto] == '\r'
+                               && readto + 1 < source.Length
+                               && source[readto + 1] == '\n';
+
+                       string nextLine = source.Substring (nextChar, readto - nextChar);
+                       nextChar = readto + ((consecutive) ? 2 : 1);
                        return nextLine;
                }
 
-                public override string ReadToEnd() {
+               public override string ReadToEnd ()
+               {
+                       if (source == null)
+                               ObjectDisposedException ();
+                       string toEnd = source.Substring (nextChar, sourceLength - nextChar);
+                       nextChar = sourceLength;
+                       return toEnd;
+               }
 
-                       CheckObjectDisposedException ();
-                        string toEnd = source.Substring( nextChar, sourceLength - nextChar );
-                        nextChar = sourceLength;
-                        return toEnd;
-                }
+#if NET_4_5
+               //
+               // All async methods return finished task with a result as it's faster
+               // than setting up async call
+               //
+               public override Task<int> ReadAsync (char[] buffer, int index, int count)
+               {
+                       return Task.FromResult (Read (buffer, index, count));
+               }
 
-               private void CheckObjectDisposedException ()
+               public override Task<int> ReadBlockAsync (char[] buffer, int index, int count)
                {
-                       if (source == null)
-                               throw new ObjectDisposedException ("StringReader", 
-                                       Locale.GetText ("Cannot read from a closed StringReader"));
+                       return Task.FromResult (ReadBlock (buffer, index, count));
+               }
+
+               public override Task<string> ReadLineAsync ()
+               {
+                       return Task.FromResult (ReadLine ());
+               }
+
+               public override Task<string> ReadToEndAsync ()
+               {
+                       return Task.FromResult (ReadToEnd ());
+               }
+#endif
+
+               static void ObjectDisposedException ()
+               {
+                       throw new ObjectDisposedException ("StringReader", 
+                                                          Locale.GetText ("Cannot read from a closed StringReader"));
                }
        }
 }