Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoParser PERF #15943

Merged
merged 9 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ZString" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OrchardCore.Localization.Abstractions\OrchardCore.Localization.Abstractions.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Cysharp.Text;

namespace OrchardCore.Localization.PortableObject
{
Expand All @@ -11,12 +13,17 @@ namespace OrchardCore.Localization.PortableObject
/// </summary>
public class PoParser
{
private static readonly Dictionary<char, char> _escapeTranslations = new()
private static readonly FrozenDictionary<char, char> _escapeTranslations;

static PoParser()
{
{ 'n', '\n' },
{ 'r', '\r' },
{ 't', '\t' },
};
_escapeTranslations = new Dictionary<char, char>()
{
{ 'n', '\n' },
{ 'r', '\r' },
{ 't', '\t' }
}.ToFrozenDictionary();
}

/// <summary>
/// Parses a .po file.
Expand Down Expand Up @@ -55,32 +62,30 @@ public IEnumerable<CultureDictionaryRecord> Parse(TextReader reader)

private static string Unescape(string str)
{
StringBuilder sb = null;
if (!str.Contains('\\'))
{
return str;
}

var escaped = false;
using var builder = ZString.CreateStringBuilder();

for (var i = 0; i < str.Length; i++)
{
var c = str[i];
if (escaped)
{
if (sb == null)
{
sb = new StringBuilder(str.Length);
if (i > 1)
{
sb.Append(str[..(i - 1)]);
}
}

char unescaped;
if (_escapeTranslations.TryGetValue(c, out unescaped))
{
sb.Append(unescaped);
builder.Append(unescaped);
}
else
{
// General rule: \x ==> x
sb.Append(c);
builder.Append(c);
}

escaped = false;
}
else
Expand All @@ -91,12 +96,12 @@ private static string Unescape(string str)
}
else
{
sb?.Append(c);
builder.Append(c);
}
}
}

return sb?.ToString() ?? str;
return builder.ToString();
}

private static string TrimQuote(string str)
Expand Down
Loading