Advertisement
nateshoffner

Laziness is a virtue

Sep 25th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.33 KB | None | 0 0
  1. #region
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Text;
  8.  
  9. #endregion
  10.  
  11. namespace Project.Helper
  12. {
  13.     /*
  14.      * Ripped straight from System.Web (.NET 2.0)
  15.      */
  16.  
  17.     internal class HtmlEntities
  18.     {
  19.         private static readonly string[] _entitiesList = new[]
  20.                                                              {
  21.                                                                  "\"-quot", "&-amp", "<-lt", ">-gt", "\x00a0-nbsp", "\x00a1-iexcl", "\x00a2-cent", "\x00a3-pound", "\x00a4-curren", "\x00a5-yen", "\x00a6-brvbar", "\x00a7-sect", "\x00a8-uml", "\x00a9-copy", "\x00aa-ordf", "\x00ab-laquo",
  22.                                                                  "\x00ac-not", "\x00ad-shy", "\x00ae-reg", "\x00af-macr", "\x00b0-deg", "\x00b1-plusmn", "\x00b2-sup2", "\x00b3-sup3", "\x00b4-acute", "\x00b5-micro", "\x00b6-para", "\x00b7-middot", "\x00b8-cedil", "\x00b9-sup1", "\x00ba-ordm",
  23.                                                                  "\x00bb-raquo",
  24.                                                                  "\x00bc-frac14", "\x00bd-frac12", "\x00be-frac34", "\x00bf-iquest", "\x00c0-Agrave", "\x00c1-Aacute", "\x00c2-Acirc", "\x00c3-Atilde", "\x00c4-Auml", "\x00c5-Aring", "\x00c6-AElig", "\x00c7-Ccedil", "\x00c8-Egrave", "\x00c9-Eacute",
  25.                                                                  "\x00ca-Ecirc", "\x00cb-Euml",
  26.                                                                  "\x00cc-Igrave", "\x00cd-Iacute", "\x00ce-Icirc", "\x00cf-Iuml", "\x00d0-ETH", "\x00d1-Ntilde", "\x00d2-Ograve", "\x00d3-Oacute", "\x00d4-Ocirc", "\x00d5-Otilde", "\x00d6-Ouml", "\x00d7-times", "\x00d8-Oslash", "\x00d9-Ugrave",
  27.                                                                  "\x00da-Uacute", "\x00db-Ucirc",
  28.                                                                  "\x00dc-Uuml", "\x00dd-Yacute", "\x00de-THORN", "\x00df-szlig", "\x00e0-agrave", "\x00e1-aacute", "\x00e2-acirc", "\x00e3-atilde", "\x00e4-auml", "\x00e5-aring", "\x00e6-aelig", "\x00e7-ccedil", "\x00e8-egrave", "\x00e9-eacute",
  29.                                                                  "\x00ea-ecirc", "\x00eb-euml",
  30.                                                                  "\x00ec-igrave", "\x00ed-iacute", "\x00ee-icirc", "\x00ef-iuml", "\x00f0-eth", "\x00f1-ntilde", "\x00f2-ograve", "\x00f3-oacute", "\x00f4-ocirc", "\x00f5-otilde", "\x00f6-ouml", "\x00f7-divide", "\x00f8-oslash", "\x00f9-ugrave",
  31.                                                                  "\x00fa-uacute", "\x00fb-ucirc",
  32.                                                                  "\x00fc-uuml", "\x00fd-yacute", "\x00fe-thorn", "\x00ff-yuml", "Œ-OElig", "œ-oelig", "Š-Scaron", "š-scaron", "Ÿ-Yuml", "ƒ-fnof", "ˆ-circ", "˜-tilde", "?-Alpha", "?-Beta", "G-Gamma", "?-Delta",
  33.                                                                  "?-Epsilon", "?-Zeta", "?-Eta", "T-Theta", "?-Iota", "?-Kappa", "?-Lambda", "?-Mu", "?-Nu", "?-Xi", "?-Omicron", "?-Pi", "?-Rho", "S-Sigma", "?-Tau", "?-Upsilon",
  34.                                                                  "F-Phi", "?-Chi", "?-Psi", "O-Omega", "a-alpha", "ß-beta", "?-gamma", "d-delta", "e-epsilon", "?-zeta", "?-eta", "?-theta", "?-iota", "?-kappa", "?-lambda", "µ-mu",
  35.                                                                  "?-nu", "?-xi", "?-omicron", "p-pi", "?-rho", "?-sigmaf", "s-sigma", "t-tau", "?-upsilon", "f-phi", "?-chi", "?-psi", "?-omega", "?-thetasym", "?-upsih", "?-piv",
  36.                                                                  " -ensp", " -emsp", "?-thinsp", "?-zwnj", "?-zwj", "?-lrm", "?-rlm", "–-ndash", "—-mdash", "‘-lsquo", "’-rsquo", "‚-sbquo", "“-ldquo", "”-rdquo", "„-bdquo", "†-dagger",
  37.                                                                  "‡-Dagger", "•-bull", "…-hellip", "‰-permil", "'-prime", "?-Prime", "‹-lsaquo", "›-rsaquo", "?-oline", "/-frasl", "€-euro", "I-image", "P-weierp", "R-real", "™-trade", "?-alefsym",
  38.                                                                  "?-larr", "?-uarr", "?-rarr", "?-darr", "?-harr", "?-crarr", "?-lArr", "?-uArr", "?-rArr", "?-dArr", "?-hArr", "?-forall", "?-part", "?-exist", "Ø-empty", "?-nabla",
  39.                                                                  "?-isin", "?-notin", "?-ni", "?-prod", "?-sum", "--minus", "*-lowast", "v-radic", "?-prop", "8-infin", "?-ang", "?-and", "?-or", "n-cap", "?-cup", "?-int",
  40.                                                                  "?-there4", "~-sim", "?-cong", "˜-asymp", "?-ne", "=-equiv", "=-le", "=-ge", "?-sub", "?-sup", "?-nsub", "?-sube", "?-supe", "?-oplus", "?-otimes", "?-perp",
  41.                                                                  "·-sdot", "?-lceil", "?-rceil", "?-lfloor", "?-rfloor", "<-lang", ">-rang", "?-loz", "?-spades", "?-clubs", "?-hearts", "?-diams"
  42.                                                              };
  43.  
  44.         private static Hashtable _entitiesLookupTable;
  45.         private static readonly object _lookupLockObject = new object();
  46.  
  47.         internal static char Lookup(string entity)
  48.         {
  49.             if (_entitiesLookupTable == null)
  50.             {
  51.                 lock (_lookupLockObject)
  52.                 {
  53.                     if (_entitiesLookupTable == null)
  54.                     {
  55.                         var hashtable = new Hashtable();
  56.                         foreach (var str in _entitiesList)
  57.                         {
  58.                             hashtable[str.Substring(2)] = str[0];
  59.                         }
  60.                         _entitiesLookupTable = hashtable;
  61.                     }
  62.                 }
  63.             }
  64.             var obj2 = _entitiesLookupTable[entity];
  65.             if (obj2 != null)
  66.             {
  67.                 return (char) obj2;
  68.             }
  69.             return '\0';
  70.         }
  71.     }
  72.  
  73.     internal class HttpUtility
  74.     {
  75.         private static readonly char[] s_entityEndingChars;
  76.  
  77.         static HttpUtility()
  78.         {
  79.             s_entityEndingChars = new[] {';', '&'};
  80.         }
  81.  
  82.         public static string HtmlDecode(string s)
  83.         {
  84.             if (s == null)
  85.             {
  86.                 return null;
  87.             }
  88.             if (s.IndexOf('&') < 0)
  89.             {
  90.                 return s;
  91.             }
  92.             var sb = new StringBuilder();
  93.             var output = new StringWriter(sb);
  94.             HtmlDecode(s, output);
  95.             return sb.ToString();
  96.         }
  97.  
  98.         public static void HtmlDecode(string s, TextWriter output)
  99.         {
  100.             if (s != null)
  101.             {
  102.                 if (s.IndexOf('&') < 0)
  103.                 {
  104.                     output.Write(s);
  105.                 }
  106.                 else
  107.                 {
  108.                     var length = s.Length;
  109.                     for (var i = 0; i < length; i++)
  110.                     {
  111.                         var ch = s[i];
  112.                         if (ch == '&')
  113.                         {
  114.                             var num3 = s.IndexOfAny(s_entityEndingChars, i + 1);
  115.                             if ((num3 > 0) && (s[num3] == ';'))
  116.                             {
  117.                                 var entity = s.Substring(i + 1, (num3 - i) - 1);
  118.                                 if ((entity.Length > 1) && (entity[0] == '#'))
  119.                                 {
  120.                                     try
  121.                                     {
  122.                                         if ((entity[1] == 'x') || (entity[1] == 'X'))
  123.                                         {
  124.                                             ch = (char) int.Parse(entity.Substring(2), NumberStyles.AllowHexSpecifier);
  125.                                         }
  126.                                         else
  127.                                         {
  128.                                             ch = (char) int.Parse(entity.Substring(1));
  129.                                         }
  130.                                         i = num3;
  131.                                     }
  132.                                     catch (FormatException)
  133.                                     {
  134.                                         i++;
  135.                                     }
  136.                                     catch (ArgumentException)
  137.                                     {
  138.                                         i++;
  139.                                     }
  140.                                 }
  141.                                 else
  142.                                 {
  143.                                     i = num3;
  144.                                     var ch2 = HtmlEntities.Lookup(entity);
  145.                                     if (ch2 != '\0')
  146.                                     {
  147.                                         ch = ch2;
  148.                                     }
  149.                                     else
  150.                                     {
  151.                                         output.Write('&');
  152.                                         output.Write(entity);
  153.                                         output.Write(';');
  154.                                         continue;
  155.                                     }
  156.                                 }
  157.                             }
  158.                         }
  159.                         output.Write(ch);
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement