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
10 The above copyright notice and this permission notice shall be included in
\r
11 all copies or substantial portions of the Software.
\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
24 using System.Reflection;
\r
25 using System.Reflection.Emit;
\r
26 using System.Diagnostics;
\r
32 /// <i>(Describe usage of "L:300" format string.)</i>
\r
34 public interface IShowable : IFormattable
\r
36 //TODO: wonder if we should use TextWriters instead of StringBuilders?
\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
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
48 // ------------------------------------------------------------
\r
50 // Static helper methods for Showing collections
\r
55 public static class Showing
\r
58 /// Show <code>Object obj</code> by appending it to <code>stringbuilder</code>
\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
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
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
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
95 /// <param name="format"></param>
\r
96 /// <returns></returns>
\r
97 static int maxLength(String format)
\r
99 //TODO: validate format string
\r
100 if (format == null)
\r
102 if (format.Length > 1 && format.StartsWith("L"))
\r
104 return int.Parse(format.Substring(1));
\r
107 return int.MaxValue;
\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
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
130 //TODO: should have been (items as IIndexed<T>).IndexingSpeed
\r
131 showIndexes = (items as IList<T>).IndexingSpeed == Speed.Constant;
\r
133 else if (items is ICollection<T>)
\r
135 ICollection<T> coll = items as ICollection<T>;
\r
136 if (coll.AllowsDuplicates)
\r
138 startdelim = "{{ ";
\r
140 if (coll.DuplicatesByCounting)
\r
141 showMultiplicities = true;
\r
145 stringbuilder.Append(startdelim);
\r
146 rest -= 2 * startdelim.Length;
\r
148 bool complete = true;
\r
151 if (showMultiplicities)
\r
153 foreach (KeyValuePair<T, int> p in (items as ICollection<T>).ItemMultiplicities())
\r
162 stringbuilder.Append(", ");
\r
165 if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))
\r
167 string multiplicityString = string.Format("(*{0})", p.Value);
\r
168 stringbuilder.Append(multiplicityString);
\r
169 rest -= multiplicityString.Length;
\r
175 foreach (T x in items)
\r
184 stringbuilder.Append(", ");
\r
189 string indexString = string.Format("{0}:", index++);
\r
190 stringbuilder.Append(indexString);
\r
191 rest -= indexString.Length;
\r
193 complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
\r
198 stringbuilder.Append("...");
\r
201 stringbuilder.Append(enddelim);
\r
208 /// <typeparam name="K"></typeparam>
\r
209 /// <typeparam name="V"></typeparam>
\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
218 bool sorted = dictionary is ISortedDictionary<K, V>;
\r
219 stringbuilder.Append(sorted ? "[ " : "{ ");
\r
220 rest -= 4; // Account for "( " and " )"
\r
222 bool complete = true;
\r
224 foreach (KeyValuePair<K, V> p in dictionary)
\r
233 stringbuilder.Append(", ");
\r
236 complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);
\r
240 stringbuilder.Append("...");
\r
243 stringbuilder.Append(sorted ? " ]" : " }");
\r