Author: krasnov
Date: 2007-07-10 07:55:20 -0400 (Tue, 10 Jul 2007)
New Revision: 81697

Modified:
   trunk/mcs/class/System.Web/System.Web/ChangeLog
   trunk/mcs/class/System.Web/System.Web/HttpUtility.cs
Log:
* HttpUtility.cs: optimized HtmlEncode and HtmlAttributeEncode methods


Modified: trunk/mcs/class/System.Web/System.Web/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web/ChangeLog     2007-07-10 11:34:39 UTC 
(rev 81696)
+++ trunk/mcs/class/System.Web/System.Web/ChangeLog     2007-07-10 11:55:20 UTC 
(rev 81697)
@@ -1,3 +1,7 @@
+2007-07-10  Vladimir Krasnov  <[EMAIL PROTECTED]>
+
+       * HttpUtility.cs: optimized HtmlEncode and HtmlAttributeEncode methods
+
 2007-07-05  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * HttpApplication.cs (AsyncRequestState.Complete): Add the

Modified: trunk/mcs/class/System.Web/System.Web/HttpUtility.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web/HttpUtility.cs        2007-07-10 
11:34:39 UTC (rev 81696)
+++ trunk/mcs/class/System.Web/System.Web/HttpUtility.cs        2007-07-10 
11:55:20 UTC (rev 81697)
@@ -351,8 +351,9 @@
                                return s;
 
                        StringBuilder output = new StringBuilder ();
-                       foreach (char c in s) 
-                               switch (c) {
+                       int len = s.Length;
+                       for (int i = 0; i < len; i++)
+                               switch (s [i]) {
                                case '&' : 
                                        output.Append ("&amp;");
                                        break;
@@ -363,7 +364,7 @@
                                        output.Append ("&lt;");
                                        break;
                                default:
-                                       output.Append (c);
+                                       output.Append (s [i]);
                                        break;
                                }
        
@@ -910,10 +911,23 @@
                        if (s == null)
                                return null;
 
+                       bool needEncode = false;
+                       for (int i = 0; i < s.Length; i++) {
+                               char c = s [i];
+                               if (c == '&' || c == '"' || c == '<' || c == 
'>' || c > 159) {
+                                       needEncode = true;
+                                       break;
+                               }
+                       }
+
+                       if (!needEncode)
+                               return s;
+
                        StringBuilder output = new StringBuilder ();
                        
-                       foreach (char c in s) 
-                               switch (c) {
+                       int len = s.Length;
+                       for (int i = 0; i < len; i++) 
+                               switch (s [i]) {
                                case '&' :
                                        output.Append ("&amp;");
                                        break;
@@ -930,12 +944,12 @@
                                        // MS starts encoding with &# from 160 
and stops at 255.
                                        // We don't do that. One reason is the 
65308/65310 unicode
                                        // characters that look like '<' and 
'>'.
-                                       if (c > 159) {
+                                       if (s [i] > 159) {
                                                output.Append ("&#");
-                                               output.Append (((int) 
c).ToString (CultureInfo.InvariantCulture));
+                                               output.Append (((int) s 
[i]).ToString (CultureInfo.InvariantCulture));
                                                output.Append (";");
                                        } else {
-                                               output.Append (c);
+                                               output.Append (s [i]);
                                        }
                                        break;
                                }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to