Advertisement
Guest User

FormatExtensions

a guest
Oct 13th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.UI;
  8.  
  9. namespace YourCompany.Web
  10. {
  11.     public static class FormatExtensions
  12.     {
  13.         private enum State
  14.         {
  15.             OutsideExpression,
  16.             OnOpenBracket,
  17.             InsideExpression,
  18.             OnCloseBracket,
  19.             End
  20.         }
  21.  
  22.         private static string OutExpression(object source, string expression)
  23.         {
  24.             string format = "";
  25.             bool colon = false;
  26.             int colonIndex = expression.IndexOf(':');
  27.             int commaIndex = expression.IndexOf(',');
  28.             if (colonIndex > 0 || commaIndex > 0)
  29.             {
  30.                 if (colonIndex > 0 && commaIndex > 0)
  31.                     colon = colonIndex < commaIndex;
  32.                 else
  33.                     colon = colonIndex > commaIndex;
  34.                 if (colon)
  35.                 {
  36.                     format = expression.Substring(colonIndex + 1);
  37.                     expression = expression.Substring(0, colonIndex);
  38.                 }
  39.                 else
  40.                 {
  41.                     format = expression.Substring(commaIndex + 1);
  42.                     expression = expression.Substring(0, commaIndex);
  43.                 }
  44.             }
  45.             try
  46.             {
  47.                 if (String.IsNullOrEmpty(format))
  48.                     return (DataBinder.Eval(source, expression) ?? "").ToString();
  49.                 string front = colon ? "{0:" : "{0,";
  50.                 return DataBinder.Eval(source, expression, front + format + "}") ?? "";
  51.             }
  52.             catch (HttpException)
  53.             {
  54.                 throw new FormatException();
  55.             }
  56.         }
  57.  
  58.         private static void FormatObjectBuffer(this string format, object source, StringBuilder buffer)
  59.         {
  60.             if (source == null)
  61.                 return;
  62.             using (var reader = new StringReader(format))
  63.             {
  64.                 StringBuilder expression = new StringBuilder();
  65.                 int c = -1;
  66.                 State state = State.OutsideExpression;
  67.                 do
  68.                 {
  69.                     switch (state)
  70.                     {
  71.                         case State.OutsideExpression:
  72.                             c = reader.Read();
  73.                             switch (c)
  74.                             {
  75.                                 case -1:
  76.                                     state = State.End;
  77.                                     break;
  78.                                 case '{':
  79.                                     state = State.OnOpenBracket;
  80.                                     break;
  81.                                 case '}':
  82.                                     state = State.OnCloseBracket;
  83.                                     break;
  84.                                 default:
  85.                                     buffer.Append((char)c);
  86.                                     break;
  87.                             }
  88.                             break;
  89.                         case State.OnOpenBracket:
  90.                             c = reader.Read();
  91.                             switch (c)
  92.                             {
  93.                                 case -1:
  94.                                     throw new FormatException();
  95.                                 case '{':
  96.                                     buffer.Append('{');
  97.                                     state = State.OutsideExpression;
  98.                                     break;
  99.                                 default:
  100.                                     expression.Append((char)c);
  101.                                     state = State.InsideExpression;
  102.                                     break;
  103.                             }
  104.                             break;
  105.                         case State.InsideExpression:
  106.                             c = reader.Read();
  107.                             switch (c)
  108.                             {
  109.                                 case -1:
  110.                                     throw new FormatException();
  111.                                 case '}':
  112.                                     buffer.Append(OutExpression(source, expression.ToString()));
  113.                                     expression.Length = 0;
  114.                                     state = State.OutsideExpression;
  115.                                     break;
  116.                                 default:
  117.                                     expression.Append((char)c);
  118.                                     break;
  119.                             }
  120.                             break;
  121.                         case State.OnCloseBracket:
  122.                             c = reader.Read();
  123.                             switch (c)
  124.                             {
  125.                                 case '}':
  126.                                     buffer.Append('}');
  127.                                     state = State.OutsideExpression;
  128.                                     break;
  129.                                 default:
  130.                                     throw new FormatException();
  131.                             }
  132.                             break;
  133.                         default:
  134.                             throw new InvalidOperationException("Invalid state");
  135.                     }
  136.                 }
  137.                 while (state != State.End);
  138.             }
  139.         }
  140.  
  141.         public static StringBuilder FormatObject(this string format, object source, StringBuilder buffer = null)
  142.         {
  143.             if (format == null)
  144.                 throw new ArgumentNullException("format");
  145.             if (buffer == null)
  146.                 buffer = new StringBuilder(format.Length * 2);  
  147.             if (source is IEnumerable<object>)
  148.             {
  149.                 var list = source as IEnumerable<object>;
  150.                 foreach (var item in list)
  151.                     FormatObjectBuffer(format, item, buffer);
  152.             }
  153.             else
  154.                 FormatObjectBuffer(format, source, buffer);
  155.             return buffer;
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement