Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
simplify encodeURI using HttpUtility
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Haegebaert authored and NeilFraser committed Aug 6, 2018
1 parent b750d26 commit 4fdce26
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions csharp/DiffMatchPatch.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Web;


namespace DiffMatchPatch { namespace DiffMatchPatch {
internal static class CompatibilityExtensions { internal static class CompatibilityExtensions {
Expand Down Expand Up @@ -1473,7 +1474,7 @@ public List<Diff> diff_fromDelta(string text1, string delta) {
// decode would change all "+" to " " // decode would change all "+" to " "
param = param.Replace("+", "%2b"); param = param.Replace("+", "%2b");


param = Uri.UnescapeDataString(param); param = HttpUtility.UrlDecode(param);
//} catch (UnsupportedEncodingException e) { //} catch (UnsupportedEncodingException e) {
// // Not likely on modern system. // // Not likely on modern system.
// throw new Error("This system does not support UTF-8.", e); // throw new Error("This system does not support UTF-8.", e);
Expand Down Expand Up @@ -2248,7 +2249,7 @@ Regex patchHeader
} }
line = text[textPointer].Substring(1); line = text[textPointer].Substring(1);
line = line.Replace("+", "%2b"); line = line.Replace("+", "%2b");
line = Uri.UnescapeDataString(line); line = HttpUtility.UrlDecode(line);
if (sign == '-') { if (sign == '-') {
// Deletion. // Deletion.
patch.diffs.Add(new Diff(Operation.DELETE, line)); patch.diffs.Add(new Diff(Operation.DELETE, line));
Expand All @@ -2272,8 +2273,6 @@ Regex patchHeader
return patches; return patches;
} }


private static Regex HEXCODE = new Regex("%[0-9A-F][0-9A-F]");

/** /**
* Encodes a string with URI-style % escaping. * Encodes a string with URI-style % escaping.
* Compatible with JavaScript's encodeURI function. * Compatible with JavaScript's encodeURI function.
Expand All @@ -2282,29 +2281,16 @@ Regex patchHeader
* @return The encoded string. * @return The encoded string.
*/ */
public static string encodeURI(string str) { public static string encodeURI(string str) {
int MAX_LENGTH = 65520 - 1; // C# is overzealous in the replacements. Walk back on a few.
// C# throws a System.UriFormatException if string is too long. return new StringBuilder(HttpUtility.UrlEncode(str))
// Split the string into 64kb chunks. .Replace('+', ' ').Replace("%20", " ").Replace("%21", "!")
StringBuilder sb = new StringBuilder(); .Replace("%2a", "*").Replace("%27", "'").Replace("%28", "(")
while (str.Length > MAX_LENGTH) { .Replace("%29", ")").Replace("%3b", ";").Replace("%2f", "/")
sb.Append(Uri.EscapeDataString(str.Substring(0, MAX_LENGTH))); .Replace("%3f", "?").Replace("%3a", ":").Replace("%40", "@")
str = str.Substring(MAX_LENGTH); .Replace("%26", "&").Replace("%3d", "=").Replace("%2b", "+")
} .Replace("%24", "$").Replace("%2c", ",").Replace("%23", "#")
sb.Append(Uri.EscapeDataString(str)); .Replace("%7e", "~")
str = sb.ToString(); .ToString();
// C# is overzealous in the replacements. Walk back on a few.
str = str.Replace("+", " ").Replace("%20", " ").Replace("%21", "!")
.Replace("%2A", "*").Replace("%27", "'").Replace("%28", "(")
.Replace("%29", ")").Replace("%3B", ";").Replace("%2F", "/")
.Replace("%3F", "?").Replace("%3A", ":").Replace("%40", "@")
.Replace("%26", "&").Replace("%3D", "=").Replace("%2B", "+")
.Replace("%24", "$").Replace("%2C", ",").Replace("%23", "#");
// C# uses uppercase hex codes, JavaScript uses lowercase.
return HEXCODE.Replace(str, new MatchEvaluator(lowerHex));
}

private static string lowerHex(Match m) {
return m.ToString().ToLower();
} }
} }
} }

0 comments on commit 4fdce26

Please sign in to comment.