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