Skip to content

Commit

Permalink
minor perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Mar 10, 2023
1 parent 44a2764 commit 348987f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/DiffEngine.Tests/DiffEngine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Using Include="DiffEngine" />
<PackageReference Include="MarkdownSnippets.MsBuild" Version="24.5.1" Condition="$(TargetFramework) == 'net7.0'" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Argon" Version="0.4.0" />
<PackageReference Include="Argon" Version="0.5.0" />
<PackageReference Include="Xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />
<PackageReference Include="ProjectDefaults" Version="1.0.89" PrivateAssets="all" />
Expand Down
2 changes: 1 addition & 1 deletion src/DiffEngine/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void AgainstBadExtension(string value, string argumentName)
{
AgainstEmpty(value, argumentName);

if (value.StartsWith("."))
if (value[0] == '.')
{
throw new ArgumentException("Must not start with a period ('.').", argumentName);
}
Expand Down
49 changes: 37 additions & 12 deletions src/DiffEngine/Tray/JsonEscaping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,41 @@ static bool IsStartOfScriptTag(string src, int i, char c) =>
i > 0 &&
src[i - 1] == '<';

static bool IsBrokenTailSurrogate(string src, int i, char c) =>
c is
>= '\uDC00' and
<= '\uDFFF' &&
(i == 0 || src[i - 1] < '\uD800' || src[i - 1] > '\uDBFF');
static bool IsBrokenTailSurrogate(string src, int i, char c)
{
if (c is
< '\uDC00' or
> '\uDFFF')
{
return false;
}

static bool IsBrokenLeadSurrogate(string src, int i, char c) =>
c is
>= '\uD800' and
<= '\uDBFF' &&
(i == src.Length - 1 || src[i + 1] < '\uDC00' || src[i + 1] > '\uDFFF');
if (i == 0)
{
return true;
}

var l = src[i - 1];
return l is < '\uD800' or > '\uDBFF';
}

static bool IsBrokenLeadSurrogate(string src, int i, char c)
{
if (c is
< '\uD800' or
> '\uDBFF')
{
return false;
}

if (i == src.Length - 1)
{
return true;
}

var l = src[i + 1];
return l is < '\uDC00' or > '\uDFFF';
}

public static string JsonEscape(this string contents)
{
Expand All @@ -50,7 +74,8 @@ public static string JsonEscape(this string contents)
}

builder.Append(contents, start, i - start);
switch (contents[i])
var content = contents[i];
switch (content)
{
case '\b':
builder.Append("\\b");
Expand Down Expand Up @@ -78,7 +103,7 @@ public static string JsonEscape(this string contents)
break;
default:
builder.Append("\\u");
builder.Append(((int) contents[i]).ToString("x04"));
builder.Append(((int) content).ToString("x04"));
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DiffEngineTray.Tests/DiffEngineTray.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<Using Include="DiffEngine" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Argon" Version="0.4.0" />
<PackageReference Include="Argon" Version="0.5.0" />
<PackageReference Include="Verify.DiffPlex" Version="2.2.0" />
<PackageReference Include="Verify.WinForms" Version="4.0.0" />
<PackageReference Include="Verify.Xunit" Version="19.10.0" />
Expand Down

0 comments on commit 348987f

Please sign in to comment.