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

Commit 4fdce26

Browse files
Jeroen HaegebaertNeilFraser
Jeroen Haegebaert
authored andcommitted
simplify encodeURI using HttpUtility
1 parent b750d26 commit 4fdce26

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

csharp/DiffMatchPatch.cs

+13-27
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Linq;
2222
using System.Text;
2323
using System.Text.RegularExpressions;
24+
using System.Web;
2425

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

1476-
param = Uri.UnescapeDataString(param);
1477+
param = HttpUtility.UrlDecode(param);
14771478
//} catch (UnsupportedEncodingException e) {
14781479
// // Not likely on modern system.
14791480
// throw new Error("This system does not support UTF-8.", e);
@@ -2248,7 +2249,7 @@ Regex patchHeader
22482249
}
22492250
line = text[textPointer].Substring(1);
22502251
line = line.Replace("+", "%2b");
2251-
line = Uri.UnescapeDataString(line);
2252+
line = HttpUtility.UrlDecode(line);
22522253
if (sign == '-') {
22532254
// Deletion.
22542255
patch.diffs.Add(new Diff(Operation.DELETE, line));
@@ -2272,8 +2273,6 @@ Regex patchHeader
22722273
return patches;
22732274
}
22742275

2275-
private static Regex HEXCODE = new Regex("%[0-9A-F][0-9A-F]");
2276-
22772276
/**
22782277
* Encodes a string with URI-style % escaping.
22792278
* Compatible with JavaScript's encodeURI function.
@@ -2282,29 +2281,16 @@ Regex patchHeader
22822281
* @return The encoded string.
22832282
*/
22842283
public static string encodeURI(string str) {
2285-
int MAX_LENGTH = 65520 - 1;
2286-
// C# throws a System.UriFormatException if string is too long.
2287-
// Split the string into 64kb chunks.
2288-
StringBuilder sb = new StringBuilder();
2289-
while (str.Length > MAX_LENGTH) {
2290-
sb.Append(Uri.EscapeDataString(str.Substring(0, MAX_LENGTH)));
2291-
str = str.Substring(MAX_LENGTH);
2292-
}
2293-
sb.Append(Uri.EscapeDataString(str));
2294-
str = sb.ToString();
2295-
// C# is overzealous in the replacements. Walk back on a few.
2296-
str = str.Replace("+", " ").Replace("%20", " ").Replace("%21", "!")
2297-
.Replace("%2A", "*").Replace("%27", "'").Replace("%28", "(")
2298-
.Replace("%29", ")").Replace("%3B", ";").Replace("%2F", "/")
2299-
.Replace("%3F", "?").Replace("%3A", ":").Replace("%40", "@")
2300-
.Replace("%26", "&").Replace("%3D", "=").Replace("%2B", "+")
2301-
.Replace("%24", "$").Replace("%2C", ",").Replace("%23", "#");
2302-
// C# uses uppercase hex codes, JavaScript uses lowercase.
2303-
return HEXCODE.Replace(str, new MatchEvaluator(lowerHex));
2304-
}
2305-
2306-
private static string lowerHex(Match m) {
2307-
return m.ToString().ToLower();
2284+
// C# is overzealous in the replacements. Walk back on a few.
2285+
return new StringBuilder(HttpUtility.UrlEncode(str))
2286+
.Replace('+', ' ').Replace("%20", " ").Replace("%21", "!")
2287+
.Replace("%2a", "*").Replace("%27", "'").Replace("%28", "(")
2288+
.Replace("%29", ")").Replace("%3b", ";").Replace("%2f", "/")
2289+
.Replace("%3f", "?").Replace("%3a", ":").Replace("%40", "@")
2290+
.Replace("%26", "&").Replace("%3d", "=").Replace("%2b", "+")
2291+
.Replace("%24", "$").Replace("%2c", ",").Replace("%23", "#")
2292+
.Replace("%7e", "~")
2293+
.ToString();
23082294
}
23092295
}
23102296
}

0 commit comments

Comments
 (0)