In System.Windows.Forms.CarbonInternal:
[mono.git] / mcs / class / Mono.C5 / C5 / Formatting.cs
1 #if NET_2_0\r
2 /*\r
3  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
4  Permission is hereby granted, free of charge, to any person obtaining a copy\r
5  of this software and associated documentation files (the "Software"), to deal\r
6  in the Software without restriction, including without limitation the rights\r
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
8  copies of the Software, and to permit persons to whom the Software is\r
9  furnished to do so, subject to the following conditions:\r
10  \r
11  The above copyright notice and this permission notice shall be included in\r
12  all copies or substantial portions of the Software.\r
13  \r
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
20  SOFTWARE.\r
21 */\r
22 \r
23 using C5;\r
24 using System;\r
25 using System.Reflection;\r
26 using System.Reflection.Emit;\r
27 using System.Diagnostics;\r
28 using System.Text;\r
29 \r
30 namespace C5\r
31 {\r
32   /// <summary>\r
33   /// <i>(Describe usage of "L:300" format string.)</i>\r
34   /// </summary>\r
35   public interface IShowable : IFormattable\r
36   {\r
37     //TODO: wonder if we should use TextWriters instead of StringBuilders?\r
38     /// <summary>\r
39     /// Format <code>this</code> using at most approximately <code>rest</code> chars and \r
40     /// append the result, possibly truncated, to stringbuilder.\r
41     /// Subtract the actual number of used chars from <code>rest</code>.\r
42     /// </summary>\r
43     /// <param name="stringbuilder"></param>\r
44     /// <param name="rest"></param>\r
45     /// <param name="formatProvider"></param>\r
46     /// <returns>True if the appended formatted string was complete (not truncated).</returns>\r
47     bool Show(StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider);\r
48   }\r
49   // ------------------------------------------------------------\r
50 \r
51   // Static helper methods for Showing collections \r
52 \r
53   /// <summary>\r
54   /// \r
55   /// </summary>\r
56   public static class Showing\r
57   {\r
58     /// <summary>\r
59     /// Show  <code>Object obj</code> by appending it to <code>stringbuilder</code>\r
60     /// </summary>\r
61     /// <param name="obj"></param>\r
62     /// <param name="stringbuilder"></param>\r
63     /// <param name="rest"></param>\r
64     /// <param name="formatProvider"></param>\r
65     /// <returns>True if <code>obj</code> was shown completely.</returns>\r
66     public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
67     {\r
68       if (rest <= 0)\r
69         return false;\r
70       else if (obj is IShowable)\r
71         return ((IShowable)obj).Show(stringbuilder, ref rest, formatProvider);\r
72       int oldLength = stringbuilder.Length;\r
73       stringbuilder.AppendFormat(formatProvider, "{0}", obj);\r
74       rest -= (stringbuilder.Length - oldLength);\r
75       return true;\r
76     }\r
77 \r
78     /// <summary>\r
79     /// \r
80     /// </summary>\r
81     /// <param name="showable"></param>\r
82     /// <param name="format"></param>\r
83     /// <param name="formatProvider"></param>\r
84     /// <returns></returns>\r
85     public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider)\r
86     {\r
87       int rest = maxLength(format);\r
88       StringBuilder sb = new StringBuilder();\r
89       showable.Show(sb, ref rest, formatProvider);\r
90       return sb.ToString();\r
91     }\r
92 \r
93     /// <summary>\r
94     /// \r
95     /// </summary>\r
96     /// <param name="format"></param>\r
97     /// <returns></returns>\r
98     static int maxLength(String format)\r
99     {\r
100       //TODO: validate format string\r
101       if (format == null)\r
102         return 80;\r
103       if (format.Length > 1 && format.StartsWith("L"))\r
104       {\r
105         return int.Parse(format.Substring(1));\r
106       }\r
107       else\r
108         return int.MaxValue;\r
109     }\r
110 \r
111     /// <summary>\r
112     /// \r
113     /// </summary>\r
114     /// <typeparam name="T"></typeparam>\r
115     /// <param name="items"></param>\r
116     /// <param name="stringbuilder"></param>\r
117     /// <param name="rest"></param>\r
118     /// <param name="formatProvider"></param>\r
119     /// <returns>True if collection was shown completely</returns>\r
120     public static bool ShowCollectionValue<T>(ICollectionValue<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
121     {\r
122       string startdelim = "{ ", enddelim = " }";\r
123       bool showIndexes = false;\r
124       bool showMultiplicities = false;\r
125       //TODO: do not test here at run time, but select code at compile time\r
126       //      perhaps by delivering the print type to this metod\r
127       if (items is IList<T>)\r
128       {\r
129         startdelim = "[ ";\r
130         enddelim = " ]";\r
131         //TODO: should have been (items as IIndexed<T>).IndexingSpeed\r
132         showIndexes = (items as IList<T>).IndexingSpeed == Speed.Constant;\r
133       }\r
134       else if (items is ICollection<T>)\r
135       {\r
136         ICollection<T> coll = items as ICollection<T>;\r
137         if (coll.AllowsDuplicates)\r
138         {\r
139           startdelim = "{{ ";\r
140           enddelim = " }}";\r
141           if (coll.DuplicatesByCounting)\r
142             showMultiplicities = true;\r
143         }\r
144       }\r
145 \r
146       stringbuilder.Append(startdelim);\r
147       rest -= 2 * startdelim.Length;\r
148       bool first = true;\r
149       bool complete = true;\r
150       int index = 0;\r
151 \r
152       if (showMultiplicities)\r
153       {\r
154         foreach (KeyValuePair<T, int> p in (items as ICollection<T>).ItemMultiplicities())\r
155         {\r
156           complete = false;\r
157           if (rest <= 0)\r
158             break;\r
159           if (first)\r
160             first = false;\r
161           else\r
162           {\r
163             stringbuilder.Append(", ");\r
164             rest -= 2;\r
165           }\r
166           if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))\r
167           {\r
168             string multiplicityString = string.Format("(*{0})", p.Value);\r
169             stringbuilder.Append(multiplicityString);\r
170             rest -= multiplicityString.Length;\r
171           }\r
172         }\r
173       }\r
174       else\r
175       {\r
176         foreach (T x in items)\r
177         {\r
178           complete = false;\r
179           if (rest <= 0)\r
180             break;\r
181           if (first)\r
182             first = false;\r
183           else\r
184           {\r
185             stringbuilder.Append(", ");\r
186             rest -= 2;\r
187           }\r
188           if (showIndexes)\r
189           {\r
190             string indexString = string.Format("{0}:", index++);\r
191             stringbuilder.Append(indexString);\r
192             rest -= indexString.Length;\r
193           }\r
194           complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);\r
195         }\r
196       }\r
197       if (!complete)\r
198       {\r
199         stringbuilder.Append("...");\r
200         rest -= 3;\r
201       }\r
202       stringbuilder.Append(enddelim);\r
203       return complete;\r
204     }\r
205 \r
206     /// <summary>\r
207     /// \r
208     /// </summary>\r
209     /// <typeparam name="K"></typeparam>\r
210     /// <typeparam name="V"></typeparam>\r
211     /// \r
212     /// <param name="dictionary"></param>\r
213     /// <param name="stringbuilder"></param>\r
214     /// <param name="formatProvider"></param>\r
215     /// <param name="rest"></param>\r
216     /// <returns></returns>\r
217     public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
218     {\r
219       bool sorted = dictionary is ISortedDictionary<K, V>;\r
220       stringbuilder.Append(sorted ? "[ " : "{ ");\r
221       rest -= 4;                                   // Account for "( " and " )"\r
222       bool first = true;\r
223       bool complete = true;\r
224 \r
225       foreach (KeyValuePair<K, V> p in dictionary)\r
226       {\r
227         complete = false;\r
228         if (rest <= 0)\r
229           break;\r
230         if (first)\r
231           first = false;\r
232         else\r
233         {\r
234           stringbuilder.Append(", ");\r
235           rest -= 2;\r
236         }\r
237         complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);\r
238       }\r
239       if (!complete)\r
240       {\r
241         stringbuilder.Append("...");\r
242         rest -= 3;\r
243       }\r
244       stringbuilder.Append(sorted ? " ]" : " }");\r
245       return complete;\r
246     }\r
247   }\r
248 }\r
249 #endif\r