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 @ 4068

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

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

File size: 5.6 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Text;
5namespace Netron.Diagramming.Core {
6  /// <summary>
7  /// <i>(Describe usage of "L:300" format string.)</i>
8  /// </summary>
9  public interface IShowable : IFormattable {
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>
29  public static class Showing {
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>
38    public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) {
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>
56    public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider) {
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>
68    static int maxLength(String format) {
69      //TODO: validate format string
70      if (format == null)
71        return 80;
72      if (format.Length > 1 && format.StartsWith("L")) {
73        return int.Parse(format.Substring(1));
74      } else
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>
87    public static bool ShowCollectionValue<T>(ICollectionBase<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) {
88      string startdelim = "{ ", enddelim = " }";
89      bool showIndexes = false;
90
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
93      if (items is IList<T>) {
94        startdelim = "[ ";
95        enddelim = " ]";
96
97      } else if (items is ICollection<T>) {
98
99        startdelim = "{{ ";
100        enddelim = " }}";
101
102      }
103
104      stringbuilder.Append(startdelim);
105      rest -= 2 * startdelim.Length;
106      bool first = true;
107      bool complete = true;
108      int index = 0;
109
110
111      {
112        foreach (T x in items) {
113          complete = false;
114          if (rest <= 0)
115            break;
116          if (first)
117            first = false;
118          else {
119            stringbuilder.Append(", ");
120            rest -= 2;
121          }
122          if (showIndexes) {
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      }
130      if (!complete) {
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>
149    public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider) {
150      stringbuilder.Append("{ ");
151      rest -= 4;           // Account for "( " and " )"
152      bool first = true;
153      bool complete = true;
154
155      foreach (KeyValuePair<K, V> p in dictionary) {
156        complete = false;
157        if (rest <= 0)
158          break;
159        if (first)
160          first = false;
161        else {
162          stringbuilder.Append(", ");
163          rest -= 2;
164        }
165        complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);
166      }
167      if (!complete) {
168        stringbuilder.Append("...");
169        rest -= 3;
170      }
171      stringbuilder.Append(" }");
172      return complete;
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.