From 91fa2dc7b60c23e3efe017bd9fae97e7949c4594 Mon Sep 17 00:00:00 2001 From: sta Date: Thu, 30 Aug 2018 21:28:49 +0900 Subject: [PATCH] [Modify] Polish it --- websocket-sharp/Net/HttpUtility.cs | 62 +++++++++++++----------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/websocket-sharp/Net/HttpUtility.cs b/websocket-sharp/Net/HttpUtility.cs index 9c0c999e..48affbc5 100644 --- a/websocket-sharp/Net/HttpUtility.cs +++ b/websocket-sharp/Net/HttpUtility.cs @@ -930,59 +930,49 @@ namespace WebSocketSharp.Net output.Write (HtmlDecode (s)); } - /// - /// HTML-encodes a and returns the encoded . - /// - /// - /// A that represents the encoded string. - /// - /// - /// A to encode. - /// public static string HtmlEncode (string s) { if (s == null || s.Length == 0) return s; - var needEncode = false; - foreach (var c in s) { - if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) { - needEncode = true; - break; - } - } - - if (!needEncode) - return s; + var buff = new StringBuilder (); - var output = new StringBuilder (); foreach (var c in s) { if (c == '&') { - output.Append ("&"); + buff.Append ("&"); + continue; } - else if (c == '"') { - output.Append ("""); + + if (c == '"') { + buff.Append ("""); + continue; } - else if (c == '<') { - output.Append ("<"); + + if (c == '<') { + buff.Append ("<"); + continue; } - else if (c == '>') { - output.Append (">"); + + if (c == '>') { + buff.Append (">"); + continue; } - else if (c > 159) { + + if (c > 159) { // MS starts encoding with &# from 160 and stops at 255. - // We don't do that. One reason is the 65308/65310 unicode + // We do not do that. One reason is the 65308/65310 unicode // characters that look like '<' and '>'. - output.Append ("&#"); - output.Append (((int) c).ToString (CultureInfo.InvariantCulture)); - output.Append (";"); - } - else { - output.Append (c); + buff.Append ("&#"); + buff.Append (((int) c).ToString (CultureInfo.InvariantCulture)); + buff.Append (";"); + + continue; } + + buff.Append (c); } - return output.ToString (); + return buff.ToString (); } ///