Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Formatting.cs @ 11178

Last change on this file since 11178 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.6 KB
RevLine 
[2768]1
2using System;
[4068]3using System.Collections.Generic;
[2768]4using System.Text;
[4068]5namespace Netron.Diagramming.Core {
[2768]6  /// <summary>
7  /// <i>(Describe usage of "L:300" format string.)</i>
8  /// </summary>
[4068]9  public interface IShowable : IFormattable {
[2768]10    //TODO: wonder if we should use TextWriters instead of StringBuilders?
11    /// <summary>
12    /// Format <code>this</code> using at most approximately <code>rest</code> chars and
13    /// append the result, possibly truncated, to stringbuilder.
14    /// Subtract the actual number of used chars from <code>rest</code>.
15    /// </summary>
16    /// <param name="stringbuilder"></param>
17    /// <param name="rest"></param>
18    /// <param name="formatProvider"></param>
19    /// <returns>True if the appended formatted string was complete (not truncated).</returns>
20    bool Show(StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider);
21  }
22  // ------------------------------------------------------------
23
24  // Static helper methods for Showing collections
25
26  /// <summary>
27  ///
28  /// </summary>
[4068]29  public static class Showing {
[2768]30    /// <summary>
31    /// Show  <code>Object obj</code> by appending it to <code>stringbuilder</code>
32    /// </summary>
33    /// <param name="obj"></param>
34    /// <param name="stringbuilder"></param>
35    /// <param name="rest"></param>
36    /// <param name="formatProvider"></param>
37    /// <returns>True if <code>obj</code> was shown completely.</returns>
[4068]38    public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) {
[2768]39      if (rest <= 0)
40        return false;
41      else if (obj is IShowable)
42        return ((IShowable)obj).Show(stringbuilder, ref rest, formatProvider);
43      int oldLength = stringbuilder.Length;
44      stringbuilder.AppendFormat(formatProvider, "{0}", obj);
45      rest -= (stringbuilder.Length - oldLength);
46      return true;
47    }
48
49    /// <summary>
50    ///
51    /// </summary>
52    /// <param name="showable"></param>
53    /// <param name="format"></param>
54    /// <param name="formatProvider"></param>
55    /// <returns></returns>
[4068]56    public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider) {
[2768]57      int rest = maxLength(format);
58      StringBuilder sb = new StringBuilder();
59      showable.Show(sb, ref rest, formatProvider);
60      return sb.ToString();
61    }
62
63    /// <summary>
64    ///
65    /// </summary>
66    /// <param name="format"></param>
67    /// <returns></returns>
[4068]68    static int maxLength(String format) {
[2768]69      //TODO: validate format string
70      if (format == null)
71        return 80;
[4068]72      if (format.Length > 1 && format.StartsWith("L")) {
[2768]73        return int.Parse(format.Substring(1));
[4068]74      } else
[2768]75        return int.MaxValue;
76    }
77
78    /// <summary>
79    ///
80    /// </summary>
81    /// <typeparam name="T"></typeparam>
82    /// <param name="items"></param>
83    /// <param name="stringbuilder"></param>
84    /// <param name="rest"></param>
85    /// <param name="formatProvider"></param>
86    /// <returns>True if collection was shown completely</returns>
[4068]87    public static bool ShowCollectionValue<T>(ICollectionBase<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) {
[2768]88      string startdelim = "{ ", enddelim = " }";
89      bool showIndexes = false;
[4068]90
[2768]91      //TODO: do not test here at run time, but select code at compile time
92      //      perhaps by delivering the print type to this metod
[4068]93      if (items is IList<T>) {
[2768]94        startdelim = "[ ";
95        enddelim = " ]";
[4068]96
97      } else if (items is ICollection<T>) {
98
99        startdelim = "{{ ";
100        enddelim = " }}";
101
[2768]102      }
103
104      stringbuilder.Append(startdelim);
105      rest -= 2 * startdelim.Length;
106      bool first = true;
107      bool complete = true;
108      int index = 0;
109
[4068]110
[2768]111      {
[4068]112        foreach (T x in items) {
[2768]113          complete = false;
114          if (rest <= 0)
115            break;
116          if (first)
117            first = false;
[4068]118          else {
[2768]119            stringbuilder.Append(", ");
120            rest -= 2;
121          }
[4068]122          if (showIndexes) {
[2768]123            string indexString = string.Format("{0}:", index++);
124            stringbuilder.Append(indexString);
125            rest -= indexString.Length;
126          }
127          complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
128        }
129      }
[4068]130      if (!complete) {
[2768]131        stringbuilder.Append("...");
132        rest -= 3;
133      }
134      stringbuilder.Append(enddelim);
135      return complete;
136    }
137
138    /// <summary>
139    ///
140    /// </summary>
141    /// <typeparam name="K"></typeparam>
142    /// <typeparam name="V"></typeparam>
143    ///
144    /// <param name="dictionary"></param>
145    /// <param name="stringbuilder"></param>
146    /// <param name="formatProvider"></param>
147    /// <param name="rest"></param>
148    /// <returns></returns>
[4068]149    public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) {
[2768]150      stringbuilder.Append("{ ");
151      rest -= 4;           // Account for "( " and " )"
152      bool first = true;
153      bool complete = true;
154
[4068]155      foreach (KeyValuePair<K, V> p in dictionary) {
[2768]156        complete = false;
157        if (rest <= 0)
158          break;
159        if (first)
160          first = false;
[4068]161        else {
[2768]162          stringbuilder.Append(", ");
163          rest -= 2;
164        }
165        complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);
166      }
[4068]167      if (!complete) {
[2768]168        stringbuilder.Append("...");
169        rest -= 3;
170      }
[4068]171      stringbuilder.Append(" }");
[2768]172      return complete;
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.