Skip to content

Commit 12cb83a

Browse files
author
Neil Fraser
committed
Add speed test for C#.
1 parent 3715f0c commit 12cb83a

File tree

8 files changed

+1674
-1197
lines changed

8 files changed

+1674
-1197
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
*.class
3+
*.exe
34
*.pyc
45
*.komodoproject
56

csharp/DiffMatchPatch.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using System.Linq;
2222
using System.Text;
2323
using System.Text.RegularExpressions;
24-
using System.Web;
2524

2625
namespace DiffMatchPatch {
2726
internal static class CompatibilityExtensions {
@@ -167,8 +166,7 @@ public override string ToString() {
167166
break;
168167
}
169168

170-
text.Append(HttpUtility.UrlEncode(aDiff.text,
171-
new UTF8Encoding()).Replace('+', ' ')).Append("\n");
169+
text.Append(Uri.EscapeDataString(aDiff.text).Replace('+', ' ')).Append("\n");
172170
}
173171

174172
return diff_match_patch.unescapeForEncodeUriCompatability(
@@ -653,7 +651,7 @@ private string diff_linesToCharsMunge(string text, List<string> lineArray,
653651
* @param lineArray List of unique strings.
654652
*/
655653
protected void diff_charsToLines(ICollection<Diff> diffs,
656-
List<string> lineArray) {
654+
IList<string> lineArray) {
657655
StringBuilder text;
658656
foreach (Diff diff in diffs) {
659657
text = new StringBuilder();
@@ -1429,8 +1427,7 @@ public string diff_toDelta(List<Diff> diffs) {
14291427
foreach (Diff aDiff in diffs) {
14301428
switch (aDiff.operation) {
14311429
case Operation.INSERT:
1432-
text.Append("+").Append(HttpUtility.UrlEncode(aDiff.text,
1433-
new UTF8Encoding()).Replace('+', ' ')).Append("\t");
1430+
text.Append("+").Append(Uri.EscapeDataString(aDiff.text).Replace('+', ' ')).Append("\t");
14341431
break;
14351432
case Operation.DELETE:
14361433
text.Append("-").Append(aDiff.text.Length).Append("\t");
@@ -1475,7 +1472,7 @@ public List<Diff> diff_fromDelta(string text1, string delta) {
14751472
// decode would change all "+" to " "
14761473
param = param.Replace("+", "%2b");
14771474

1478-
param = HttpUtility.UrlDecode(param, new UTF8Encoding(false, true));
1475+
param = Uri.UnescapeDataString(param);
14791476
//} catch (UnsupportedEncodingException e) {
14801477
// // Not likely on modern system.
14811478
// throw new Error("This system does not support UTF-8.", e);
@@ -2250,7 +2247,7 @@ Regex patchHeader
22502247
}
22512248
line = text[textPointer].Substring(1);
22522249
line = line.Replace("+", "%2b");
2253-
line = HttpUtility.UrlDecode(line, new UTF8Encoding(false, true));
2250+
line = Uri.UnescapeDataString(line);
22542251
if (sign == '-') {
22552252
// Deletion.
22562253
patch.diffs.Add(new Diff(Operation.DELETE, line));
@@ -2274,26 +2271,33 @@ Regex patchHeader
22742271
return patches;
22752272
}
22762273

2274+
private static Regex HEXCODE = new Regex("%[0-9A-F][0-9A-F]");
2275+
22772276
/**
2278-
* Unescape selected chars for compatability with JavaScript's encodeURI.
2277+
* Unescape selected chars for compatibility with JavaScript's encodeURI.
22792278
* In speed critical applications this could be dropped since the
22802279
* receiving application will certainly decode these fine.
22812280
* Note that this function is case-sensitive. Thus "%3F" would not be
22822281
* unescaped. But this is ok because it is only called with the output of
2283-
* HttpUtility.UrlEncode which returns lowercase hex.
2282+
* Uri.EscapeDataString which returns lowercase hex.
22842283
*
22852284
* Example: "%3f" -> "?", "%24" -> "$", etc.
22862285
*
22872286
* @param str The string to escape.
22882287
* @return The escaped string.
22892288
*/
22902289
public static string unescapeForEncodeUriCompatability(string str) {
2291-
return str.Replace("%21", "!").Replace("%7e", "~")
2290+
str = str.Replace("%20", " ").Replace("%21", "!").Replace("%2A", "*")
22922291
.Replace("%27", "'").Replace("%28", "(").Replace("%29", ")")
2293-
.Replace("%3b", ";").Replace("%2f", "/").Replace("%3f", "?")
2294-
.Replace("%3a", ":").Replace("%40", "@").Replace("%26", "&")
2295-
.Replace("%3d", "=").Replace("%2b", "+").Replace("%24", "$")
2296-
.Replace("%2c", ",").Replace("%23", "#");
2292+
.Replace("%3B", ";").Replace("%2F", "/").Replace("%3F", "?")
2293+
.Replace("%3A", ":").Replace("%40", "@").Replace("%26", "&")
2294+
.Replace("%3D", "=").Replace("%2B", "+").Replace("%24", "$")
2295+
.Replace("%2C", ",").Replace("%23", "#");
2296+
return HEXCODE.Replace(str, new MatchEvaluator(lowerHex));
2297+
}
2298+
2299+
private static string lowerHex(Match m) {
2300+
return m.ToString().ToLower();
22972301
}
22982302
}
22992303
}

0 commit comments

Comments
 (0)