Bringing C5 1.0 into the main branch.
[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       if (rest <= 0)\r
68         return false;\r
69       else if (obj is IShowable)\r
70         return ((IShowable)obj).Show(stringbuilder, ref rest, formatProvider);\r
71       int oldLength = stringbuilder.Length;\r
72       stringbuilder.AppendFormat(formatProvider, "{0}", obj);\r
73       rest -= (stringbuilder.Length - oldLength);\r
74       return true;\r
75     }\r
76 \r
77     /// <summary>\r
78     /// \r
79     /// </summary>\r
80     /// <param name="showable"></param>\r
81     /// <param name="format"></param>\r
82     /// <param name="formatProvider"></param>\r
83     /// <returns></returns>\r
84     public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider)\r
85     {\r
86       int rest = maxLength(format);\r
87       StringBuilder sb = new StringBuilder();\r
88       showable.Show(sb, ref rest, formatProvider);\r
89       return sb.ToString();\r
90     }\r
91 \r
92     /// <summary>\r
93     /// \r
94     /// </summary>\r
95     /// <param name="format"></param>\r
96     /// <returns></returns>\r
97     static int maxLength(String format)\r
98     {\r
99       //TODO: validate format string\r
100       if (format == null)\r
101         return 80;\r
102       if (format.Length > 1 && format.StartsWith("L"))\r
103       {\r
104         return int.Parse(format.Substring(1));\r
105       }\r
106       else\r
107         return int.MaxValue;\r
108     }\r
109 \r
110     /// <summary>\r
111     /// \r
112     /// </summary>\r
113     /// <typeparam name="T"></typeparam>\r
114     /// <param name="items"></param>\r
115     /// <param name="stringbuilder"></param>\r
116     /// <param name="rest"></param>\r
117     /// <param name="formatProvider"></param>\r
118     /// <returns>True if collection was shown completely</returns>\r
119     public static bool ShowCollectionValue<T>(ICollectionValue<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
120     {\r
121       string startdelim = "{ ", enddelim = " }";\r
122       bool showIndexes = false;\r
123       bool showMultiplicities = false;\r
124       //TODO: do not test here at run time, but select code at compile time\r
125       //      perhaps by delivering the print type to this metod\r
126       if (items is IList<T>)\r
127       {\r
128         startdelim = "[ ";\r
129         enddelim = " ]";\r
130         //TODO: should have been (items as IIndexed<T>).IndexingSpeed\r
131         showIndexes = (items as IList<T>).IndexingSpeed == Speed.Constant;\r
132       }\r
133       else if (items is ICollection<T>)\r
134       {\r
135         ICollection<T> coll = items as ICollection<T>;\r
136         if (coll.AllowsDuplicates)\r
137         {\r
138           startdelim = "{{ ";\r
139           enddelim = " }}";\r
140           if (coll.DuplicatesByCounting)\r
141             showMultiplicities = true;\r
142         }\r
143       }\r
144 \r
145       stringbuilder.Append(startdelim);\r
146       rest -= 2 * startdelim.Length;\r
147       bool first = true;\r
148       bool complete = true;\r
149       int index = 0;\r
150 \r
151       if (showMultiplicities)\r
152       {\r
153         foreach (KeyValuePair<T, int> p in (items as ICollection<T>).ItemMultiplicities())\r
154         {\r
155           complete = false;\r
156           if (rest <= 0)\r
157             break;\r
158           if (first)\r
159             first = false;\r
160           else\r
161           {\r
162             stringbuilder.Append(", ");\r
163             rest -= 2;\r
164           }\r
165           if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))\r
166           {\r
167             string multiplicityString = string.Format("(*{0})", p.Value);\r
168             stringbuilder.Append(multiplicityString);\r
169             rest -= multiplicityString.Length;\r
170           }\r
171         }\r
172       }\r
173       else\r
174       {\r
175         foreach (T x in items)\r
176         {\r
177           complete = false;\r
178           if (rest <= 0)\r
179             break;\r
180           if (first)\r
181             first = false;\r
182           else\r
183           {\r
184             stringbuilder.Append(", ");\r
185             rest -= 2;\r
186           }\r
187           if (showIndexes)\r
188           {\r
189             string indexString = string.Format("{0}:", index++);\r
190             stringbuilder.Append(indexString);\r
191             rest -= indexString.Length;\r
192           }\r
193           complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);\r
194         }\r
195       }\r
196       if (!complete)\r
197       {\r
198         stringbuilder.Append("...");\r
199         rest -= 3;\r
200       }\r
201       stringbuilder.Append(enddelim);\r
202       return complete;\r
203     }\r
204 \r
205     /// <summary>\r
206     /// \r
207     /// </summary>\r
208     /// <typeparam name="K"></typeparam>\r
209     /// <typeparam name="V"></typeparam>\r
210     /// \r
211     /// <param name="dictionary"></param>\r
212     /// <param name="stringbuilder"></param>\r
213     /// <param name="formatProvider"></param>\r
214     /// <param name="rest"></param>\r
215     /// <returns></returns>\r
216     public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
217     {\r
218       bool sorted = dictionary is ISortedDictionary<K, V>;\r
219       stringbuilder.Append(sorted ? "[ " : "{ ");\r
220       rest -= 4;                                   // Account for "( " and " )"\r
221       bool first = true;\r
222       bool complete = true;\r
223 \r
224       foreach (KeyValuePair<K, V> p in dictionary)\r
225       {\r
226         complete = false;\r
227         if (rest <= 0)\r
228           break;\r
229         if (first)\r
230           first = false;\r
231         else\r
232         {\r
233           stringbuilder.Append(", ");\r
234           rest -= 2;\r
235         }\r
236         complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);\r
237       }\r
238       if (!complete)\r
239       {\r
240         stringbuilder.Append("...");\r
241         rest -= 3;\r
242       }\r
243       stringbuilder.Append(sorted ? " ]" : " }");\r
244       return complete;\r
245     }\r
246   }\r
247 }