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
11 The above copyright notice and this permission notice shall be included in
\r
12 all copies or substantial portions of the Software.
\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
25 using System.Reflection;
\r
26 using System.Reflection.Emit;
\r
27 using System.Diagnostics;
\r
33 /// <i>(Describe usage of "L:300" format string.)</i>
\r
35 public interface IShowable : IFormattable
\r
37 //TODO: wonder if we should use TextWriters instead of StringBuilders?
\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
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
49 // ------------------------------------------------------------
\r
51 // Static helper methods for Showing collections
\r
56 public static class Showing
\r
59 /// Show <code>Object obj</code> by appending it to <code>stringbuilder</code>
\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
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
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
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
96 /// <param name="format"></param>
\r
97 /// <returns></returns>
\r
98 static int maxLength(String format)
\r
100 //TODO: validate format string
\r
101 if (format == null)
\r
103 if (format.Length > 1 && format.StartsWith("L"))
\r
105 return int.Parse(format.Substring(1));
\r
108 return int.MaxValue;
\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
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
131 //TODO: should have been (items as IIndexed<T>).IndexingSpeed
\r
132 showIndexes = (items as IList<T>).IndexingSpeed == Speed.Constant;
\r
134 else if (items is ICollection<T>)
\r
136 ICollection<T> coll = items as ICollection<T>;
\r
137 if (coll.AllowsDuplicates)
\r
139 startdelim = "{{ ";
\r
141 if (coll.DuplicatesByCounting)
\r
142 showMultiplicities = true;
\r
146 stringbuilder.Append(startdelim);
\r
147 rest -= 2 * startdelim.Length;
\r
149 bool complete = true;
\r
152 if (showMultiplicities)
\r
154 foreach (KeyValuePair<T, int> p in (items as ICollection<T>).ItemMultiplicities())
\r
163 stringbuilder.Append(", ");
\r
166 if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))
\r
168 string multiplicityString = string.Format("(*{0})", p.Value);
\r
169 stringbuilder.Append(multiplicityString);
\r
170 rest -= multiplicityString.Length;
\r
176 foreach (T x in items)
\r
185 stringbuilder.Append(", ");
\r
190 string indexString = string.Format("{0}:", index++);
\r
191 stringbuilder.Append(indexString);
\r
192 rest -= indexString.Length;
\r
194 complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
\r
199 stringbuilder.Append("...");
\r
202 stringbuilder.Append(enddelim);
\r
209 /// <typeparam name="K"></typeparam>
\r
210 /// <typeparam name="V"></typeparam>
\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
219 bool sorted = dictionary is ISortedDictionary<K, V>;
\r
220 stringbuilder.Append(sorted ? "[ " : "{ ");
\r
221 rest -= 4; // Account for "( " and " )"
\r
223 bool complete = true;
\r
225 foreach (KeyValuePair<K, V> p in dictionary)
\r
234 stringbuilder.Append(", ");
\r
237 complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);
\r
241 stringbuilder.Append("...");
\r
244 stringbuilder.Append(sorted ? " ]" : " }");
\r