Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Formatting.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Formatting.cs
r2768 r4068 1 1 2 2 using System; 3 using System.Reflection; 4 using System.Reflection.Emit; 5 using System.Diagnostics; 3 using System.Collections.Generic; 6 4 using System.Text; 7 using System.Collections; 8 using System.Collections.Generic; 9 namespace Netron.Diagramming.Core 10 { 5 namespace Netron.Diagramming.Core { 11 6 /// <summary> 12 7 /// <i>(Describe usage of "L:300" format string.)</i> 13 8 /// </summary> 14 public interface IShowable : IFormattable 15 { 9 public interface IShowable : IFormattable { 16 10 //TODO: wonder if we should use TextWriters instead of StringBuilders? 17 11 /// <summary> … … 33 27 /// 34 28 /// </summary> 35 public static class Showing 36 { 29 public static class Showing { 37 30 /// <summary> 38 31 /// Show <code>Object obj</code> by appending it to <code>stringbuilder</code> … … 43 36 /// <param name="formatProvider"></param> 44 37 /// <returns>True if <code>obj</code> was shown completely.</returns> 45 public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) 46 { 38 public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) { 47 39 if (rest <= 0) 48 40 return false; … … 62 54 /// <param name="formatProvider"></param> 63 55 /// <returns></returns> 64 public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider) 65 { 56 public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider) { 66 57 int rest = maxLength(format); 67 58 StringBuilder sb = new StringBuilder(); … … 75 66 /// <param name="format"></param> 76 67 /// <returns></returns> 77 static int maxLength(String format) 78 { 68 static int maxLength(String format) { 79 69 //TODO: validate format string 80 70 if (format == null) 81 71 return 80; 82 if (format.Length > 1 && format.StartsWith("L")) 83 { 72 if (format.Length > 1 && format.StartsWith("L")) { 84 73 return int.Parse(format.Substring(1)); 85 } 86 else 74 } else 87 75 return int.MaxValue; 88 76 } … … 97 85 /// <param name="formatProvider"></param> 98 86 /// <returns>True if collection was shown completely</returns> 99 public static bool ShowCollectionValue<T>(ICollectionBase<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) 100 { 87 public static bool ShowCollectionValue<T>(ICollectionBase<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) { 101 88 string startdelim = "{ ", enddelim = " }"; 102 89 bool showIndexes = false; 103 90 104 91 //TODO: do not test here at run time, but select code at compile time 105 92 // perhaps by delivering the print type to this metod 106 if (items is IList<T>) 107 { 93 if (items is IList<T>) { 108 94 startdelim = "[ "; 109 95 enddelim = " ]"; 110 111 } 112 else if (items is ICollection<T>) 113 { 114 115 startdelim = "{{ "; 116 enddelim = " }}"; 117 96 97 } else if (items is ICollection<T>) { 98 99 startdelim = "{{ "; 100 enddelim = " }}"; 101 118 102 } 119 103 … … 124 108 int index = 0; 125 109 126 110 127 111 { 128 foreach (T x in items) 129 { 112 foreach (T x in items) { 130 113 complete = false; 131 114 if (rest <= 0) … … 133 116 if (first) 134 117 first = false; 135 else 136 { 118 else { 137 119 stringbuilder.Append(", "); 138 120 rest -= 2; 139 121 } 140 if (showIndexes) 141 { 122 if (showIndexes) { 142 123 string indexString = string.Format("{0}:", index++); 143 124 stringbuilder.Append(indexString); … … 147 128 } 148 129 } 149 if (!complete) 150 { 130 if (!complete) { 151 131 stringbuilder.Append("..."); 152 132 rest -= 3; … … 167 147 /// <param name="rest"></param> 168 148 /// <returns></returns> 169 public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) 170 { 149 public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) { 171 150 stringbuilder.Append("{ "); 172 151 rest -= 4; // Account for "( " and " )" … … 174 153 bool complete = true; 175 154 176 foreach (KeyValuePair<K, V> p in dictionary) 177 { 155 foreach (KeyValuePair<K, V> p in dictionary) { 178 156 complete = false; 179 157 if (rest <= 0) … … 181 159 if (first) 182 160 first = false; 183 else 184 { 161 else { 185 162 stringbuilder.Append(", "); 186 163 rest -= 2; … … 188 165 complete = Showing.Show(p, stringbuilder, ref rest, formatProvider); 189 166 } 190 if (!complete) 191 { 167 if (!complete) { 192 168 stringbuilder.Append("..."); 193 169 rest -= 3; 194 170 } 195 stringbuilder.Append( 171 stringbuilder.Append(" }"); 196 172 return complete; 197 173 }
Note: See TracChangeset
for help on using the changeset viewer.