[Task] Add an extra check in Task.WaitAny to make sure the index returned is valid
[mono.git] / mcs / class / Mono.C5 / C5 / Formatting.cs
index bc2433bd3e202554a7dc9dbd09fdba0652c56fc6..7164b693b0c7f67db9620963082203db26c93cb9 100644 (file)
-#if NET_2_0
-/*\r
- Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
- Permission is hereby granted, free of charge, to any person obtaining a copy\r
- of this software and associated documentation files (the "Software"), to deal\r
- in the Software without restriction, including without limitation the rights\r
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
- copies of the Software, and to permit persons to whom the Software is\r
- furnished to do so, subject to the following conditions:\r
\r
- The above copyright notice and this permission notice shall be included in\r
- all copies or substantial portions of the Software.\r
\r
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
- SOFTWARE.\r
-*/\r
-\r
-using C5;\r
-using System;\r
-using System.Reflection;\r
-using System.Reflection.Emit;\r
-using System.Diagnostics;\r
-using System.Text;\r
-\r
-namespace C5\r
-{\r
-  /// <summary>\r
-  /// <i>(Describe usage of "L:300" format string.)</i>\r
-  /// </summary>\r
-  public interface IShowable : IFormattable\r
-  {\r
-    //TODO: wonder if we should use TextWriters instead of StringBuilders?\r
-    /// <summary>\r
-    /// Format <code>this</code> using at most approximately <code>rest</code> chars and \r
-    /// append the result, possibly truncated, to stringbuilder.\r
-    /// Subtract the actual number of used chars from <code>rest</code>.\r
-    /// </summary>\r
-    /// <param name="stringbuilder"></param>\r
-    /// <param name="rest"></param>\r
-    /// <param name="formatProvider"></param>\r
-    /// <returns>True if the appended formatted string was complete (not truncated).</returns>\r
-    bool Show(StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider);\r
-  }\r
-  // ------------------------------------------------------------\r
-\r
-  // Static helper methods for Showing collections \r
-\r
-  /// <summary>\r
-  /// \r
-  /// </summary>\r
-  public static class Showing\r
-  {\r
-    /// <summary>\r
-    /// Show  <code>Object obj</code> by appending it to <code>stringbuilder</code>\r
-    /// </summary>\r
-    /// <param name="obj"></param>\r
-    /// <param name="stringbuilder"></param>\r
-    /// <param name="rest"></param>\r
-    /// <param name="formatProvider"></param>\r
-    /// <returns>True if <code>obj</code> was shown completely.</returns>\r
-    public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
-    {\r
-      if (rest <= 0)\r
-        return false;\r
-      else if (obj is IShowable)\r
-        return ((IShowable)obj).Show(stringbuilder, ref rest, formatProvider);\r
-      int oldLength = stringbuilder.Length;\r
-      stringbuilder.AppendFormat(formatProvider, "{0}", obj);\r
-      rest -= (stringbuilder.Length - oldLength);\r
-      return true;\r
-    }\r
-\r
-    /// <summary>\r
-    /// \r
-    /// </summary>\r
-    /// <param name="showable"></param>\r
-    /// <param name="format"></param>\r
-    /// <param name="formatProvider"></param>\r
-    /// <returns></returns>\r
-    public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider)\r
-    {\r
-      int rest = maxLength(format);\r
-      StringBuilder sb = new StringBuilder();\r
-      showable.Show(sb, ref rest, formatProvider);\r
-      return sb.ToString();\r
-    }\r
-\r
-    /// <summary>\r
-    /// \r
-    /// </summary>\r
-    /// <param name="format"></param>\r
-    /// <returns></returns>\r
-    static int maxLength(String format)\r
-    {\r
-      //TODO: validate format string\r
-      if (format == null)\r
-        return 80;\r
-      if (format.Length > 1 && format.StartsWith("L"))\r
-      {\r
-        return int.Parse(format.Substring(1));\r
-      }\r
-      else\r
-        return int.MaxValue;\r
-    }\r
-\r
-    /// <summary>\r
-    /// \r
-    /// </summary>\r
-    /// <typeparam name="T"></typeparam>\r
-    /// <param name="items"></param>\r
-    /// <param name="stringbuilder"></param>\r
-    /// <param name="rest"></param>\r
-    /// <param name="formatProvider"></param>\r
-    /// <returns>True if collection was shown completely</returns>\r
-    public static bool ShowCollectionValue<T>(ICollectionValue<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
-    {\r
-      string startdelim = "{ ", enddelim = " }";\r
-      bool showIndexes = false;\r
-      bool showMultiplicities = false;\r
-      //TODO: do not test here at run time, but select code at compile time\r
-      //      perhaps by delivering the print type to this metod\r
-      if (items is IList<T>)\r
-      {\r
-        startdelim = "[ ";\r
-        enddelim = " ]";\r
-        //TODO: should have been (items as IIndexed<T>).IndexingSpeed\r
-        showIndexes = (items as IList<T>).IndexingSpeed == Speed.Constant;\r
-      }\r
-      else if (items is ICollection<T>)\r
-      {\r
-        ICollection<T> coll = items as ICollection<T>;\r
-        if (coll.AllowsDuplicates)\r
-        {\r
-          startdelim = "{{ ";\r
-          enddelim = " }}";\r
-          if (coll.DuplicatesByCounting)\r
-            showMultiplicities = true;\r
-        }\r
-      }\r
-\r
-      stringbuilder.Append(startdelim);\r
-      rest -= 2 * startdelim.Length;\r
-      bool first = true;\r
-      bool complete = true;\r
-      int index = 0;\r
-\r
-      if (showMultiplicities)\r
-      {\r
-        foreach (KeyValuePair<T, int> p in (items as ICollection<T>).ItemMultiplicities())\r
-        {\r
-          complete = false;\r
-          if (rest <= 0)\r
-            break;\r
-          if (first)\r
-            first = false;\r
-          else\r
-          {\r
-            stringbuilder.Append(", ");\r
-            rest -= 2;\r
-          }\r
-          if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))\r
-          {\r
-            string multiplicityString = string.Format("(*{0})", p.Value);\r
-            stringbuilder.Append(multiplicityString);\r
-            rest -= multiplicityString.Length;\r
-          }\r
-        }\r
-      }\r
-      else\r
-      {\r
-        foreach (T x in items)\r
-        {\r
-          complete = false;\r
-          if (rest <= 0)\r
-            break;\r
-          if (first)\r
-            first = false;\r
-          else\r
-          {\r
-            stringbuilder.Append(", ");\r
-            rest -= 2;\r
-          }\r
-          if (showIndexes)\r
-          {\r
-            string indexString = string.Format("{0}:", index++);\r
-            stringbuilder.Append(indexString);\r
-            rest -= indexString.Length;\r
-          }\r
-          complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);\r
-        }\r
-      }\r
-      if (!complete)\r
-      {\r
-        stringbuilder.Append("...");\r
-        rest -= 3;\r
-      }\r
-      stringbuilder.Append(enddelim);\r
-      return complete;\r
-    }\r
-\r
-    /// <summary>\r
-    /// \r
-    /// </summary>\r
-    /// <typeparam name="K"></typeparam>\r
-    /// <typeparam name="V"></typeparam>\r
-    /// \r
-    /// <param name="dictionary"></param>\r
-    /// <param name="stringbuilder"></param>\r
-    /// <param name="formatProvider"></param>\r
-    /// <param name="rest"></param>\r
-    /// <returns></returns>\r
-    public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
-    {\r
-      bool sorted = dictionary is ISortedDictionary<K, V>;\r
-      stringbuilder.Append(sorted ? "[ " : "{ ");\r
-      rest -= 4;                                  // Account for "( " and " )"\r
-      bool first = true;\r
-      bool complete = true;\r
-\r
-      foreach (KeyValuePair<K, V> p in dictionary)\r
-      {\r
-        complete = false;\r
-        if (rest <= 0)\r
-          break;\r
-        if (first)\r
-          first = false;\r
-        else\r
-        {\r
-          stringbuilder.Append(", ");\r
-          rest -= 2;\r
-        }\r
-        complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);\r
-      }\r
-      if (!complete)\r
-      {\r
-        stringbuilder.Append("...");\r
-        rest -= 3;\r
-      }\r
-      stringbuilder.Append(sorted ? " ]" : " }");\r
-      return complete;\r
-    }\r
-  }\r
-}
-#endif
+/*
+ Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+using C5;
+using System;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Diagnostics;
+using System.Text;
+
+namespace C5
+{
+  /// <summary>
+  /// <i>(Describe usage of "L:300" format string.)</i>
+  /// </summary>
+  public interface IShowable : IFormattable
+  {
+    //TODO: wonder if we should use TextWriters instead of StringBuilders?
+    /// <summary>
+    /// Format <code>this</code> using at most approximately <code>rest</code> chars and 
+    /// append the result, possibly truncated, to stringbuilder.
+    /// Subtract the actual number of used chars from <code>rest</code>.
+    /// </summary>
+    /// <param name="stringbuilder"></param>
+    /// <param name="rest"></param>
+    /// <param name="formatProvider"></param>
+    /// <returns>True if the appended formatted string was complete (not truncated).</returns>
+    bool Show(StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider);
+  }
+  // ------------------------------------------------------------
+
+  // Static helper methods for Showing collections 
+
+  /// <summary>
+  /// 
+  /// </summary>
+  public static class Showing
+  {
+    /// <summary>
+    /// Show  <code>Object obj</code> by appending it to <code>stringbuilder</code>
+    /// </summary>
+    /// <param name="obj"></param>
+    /// <param name="stringbuilder"></param>
+    /// <param name="rest"></param>
+    /// <param name="formatProvider"></param>
+    /// <returns>True if <code>obj</code> was shown completely.</returns>
+    public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
+    {
+      IShowable showable;
+      if (rest <= 0)
+        return false;
+      else if ((showable = obj as IShowable) != null)
+        return showable.Show(stringbuilder, ref rest, formatProvider);
+      int oldLength = stringbuilder.Length;
+      stringbuilder.AppendFormat(formatProvider, "{0}", obj);
+      rest -= (stringbuilder.Length - oldLength);
+      return true;
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <param name="showable"></param>
+    /// <param name="format"></param>
+    /// <param name="formatProvider"></param>
+    /// <returns></returns>
+    public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider)
+    {
+      int rest = maxLength(format);
+      StringBuilder sb = new StringBuilder();
+      showable.Show(sb, ref rest, formatProvider);
+      return sb.ToString();
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <param name="format"></param>
+    /// <returns></returns>
+    static int maxLength(String format)
+    {
+      //TODO: validate format string
+      if (format == null)
+        return 80;
+      if (format.Length > 1 && format.StartsWith("L"))
+      {
+        return int.Parse(format.Substring(1));
+      }
+      else
+        return int.MaxValue;
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="items"></param>
+    /// <param name="stringbuilder"></param>
+    /// <param name="rest"></param>
+    /// <param name="formatProvider"></param>
+    /// <returns>True if collection was shown completely</returns>
+    public static bool ShowCollectionValue<T>(ICollectionValue<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
+    {
+      string startdelim = "{ ", enddelim = " }";
+      bool showIndexes = false;
+      bool showMultiplicities = false;
+      //TODO: do not test here at run time, but select code at compile time
+      //      perhaps by delivering the print type to this metod
+      IList<T> list;
+      ICollection<T> coll = items as ICollection<T>;
+      if ((list = items as IList<T>) != null)
+      {
+        startdelim = "[ ";
+        enddelim = " ]";
+        //TODO: should have been (items as IIndexed<T>).IndexingSpeed
+        showIndexes = list.IndexingSpeed == Speed.Constant;
+      }
+      else if (coll != null)
+      {
+        if (coll.AllowsDuplicates)
+        {
+          startdelim = "{{ ";
+          enddelim = " }}";
+          if (coll.DuplicatesByCounting)
+            showMultiplicities = true;
+        }
+      }
+
+      stringbuilder.Append(startdelim);
+      rest -= 2 * startdelim.Length;
+      bool first = true;
+      bool complete = true;
+      int index = 0;
+
+      if (showMultiplicities)
+      {
+        foreach (KeyValuePair<T, int> p in coll.ItemMultiplicities())
+        {
+          complete = false;
+          if (rest <= 0)
+            break;
+          if (first)
+            first = false;
+          else
+          {
+            stringbuilder.Append(", ");
+            rest -= 2;
+          }
+          if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))
+          {
+            string multiplicityString = string.Format("(*{0})", p.Value);
+            stringbuilder.Append(multiplicityString);
+            rest -= multiplicityString.Length;
+          }
+        }
+      }
+      else
+      {
+        foreach (T x in items)
+        {
+          complete = false;
+          if (rest <= 0)
+            break;
+          if (first)
+            first = false;
+          else
+          {
+            stringbuilder.Append(", ");
+            rest -= 2;
+          }
+          if (showIndexes)
+          {
+            string indexString = string.Format("{0}:", index++);
+            stringbuilder.Append(indexString);
+            rest -= indexString.Length;
+          }
+          complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
+        }
+      }
+      if (!complete)
+      {
+        stringbuilder.Append("...");
+        rest -= 3;
+      }
+      stringbuilder.Append(enddelim);
+      return complete;
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <typeparam name="K"></typeparam>
+    /// <typeparam name="V"></typeparam>
+    /// 
+    /// <param name="dictionary"></param>
+    /// <param name="stringbuilder"></param>
+    /// <param name="formatProvider"></param>
+    /// <param name="rest"></param>
+    /// <returns></returns>
+    public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
+    {
+      bool sorted = dictionary is ISortedDictionary<K, V>;
+      stringbuilder.Append(sorted ? "[ " : "{ ");
+      rest -= 4;                                  // Account for "( " and " )"
+      bool first = true;
+      bool complete = true;
+
+      foreach (KeyValuePair<K, V> p in dictionary)
+      {
+        complete = false;
+        if (rest <= 0)
+          break;
+        if (first)
+          first = false;
+        else
+        {
+          stringbuilder.Append(", ");
+          rest -= 2;
+        }
+        complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);
+      }
+      if (!complete)
+      {
+        stringbuilder.Append("...");
+        rest -= 3;
+      }
+      stringbuilder.Append(sorted ? " ]" : " }");
+      return complete;
+    }
+  }
+}
\ No newline at end of file