Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / corlib / System / Console.iOS.cs
1 //
2 // Helper for Console to allow indirect access to `stdout` using NSLog
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@xamarin.com>
6 //
7 // Copyright 2012-2013 Xamarin Inc. All rights reserved.
8 //
9
10 #if MONOTOUCH
11
12 using System;
13 using System.IO;
14 using System.Runtime.InteropServices;
15 using System.Text;
16
17 namespace System {
18
19         public static partial class Console {
20
21                 class NSLogWriter : TextWriter {
22                         
23                         [DllImport ("__Internal", CharSet=CharSet.Unicode)]
24                         extern static void monotouch_log (string s);
25                         
26                         StringBuilder sb;
27                         
28                         public NSLogWriter ()
29                         {
30                                 sb = new StringBuilder ();
31                         }
32                         
33                         public override System.Text.Encoding Encoding {
34                                 get { return System.Text.Encoding.UTF8; }
35                         }
36                         
37                         public override void Flush ()
38                         {
39                                 try {
40                                         monotouch_log (sb.ToString ());
41                                         sb.Length = 0;
42                                 }
43                                 catch (Exception) {
44                                 }
45                         }
46                         
47                         // minimum to override - see http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx
48                         public override void Write (char value)
49                         {
50                                 try {
51                                         sb.Append (value);
52                                 }
53                                 catch (Exception) {
54                                 }
55                         }
56                         
57                         // optimization (to avoid concatening chars)
58                         public override void Write (string value)
59                         {
60                                 try {
61                                         sb.Append (value);
62                                         if (value != null && value.Length >= CoreNewLine.Length && EndsWithNewLine (value))
63                                                 Flush ();
64                                 }
65                                 catch (Exception) {
66                                 }
67                         }
68                         
69                         bool EndsWithNewLine (string value)
70                         {
71                                 for (int i = 0, v = value.Length - CoreNewLine.Length; i < CoreNewLine.Length; ++i, ++v) {
72                                         if (value [v] != CoreNewLine [i])
73                                                 return false;
74                                 }
75                                 
76                                 return true;
77                         }
78                         
79                         public override void WriteLine ()
80                         {
81                                 try {
82                                         Flush ();
83                                 }
84                                 catch (Exception) {
85                                 }
86                         }
87                 }
88         }
89 }
90
91 #endif