diff --git a/src/OrchardCore/OrchardCore.Localization.Core/OrchardCore.Localization.Core.csproj b/src/OrchardCore/OrchardCore.Localization.Core/OrchardCore.Localization.Core.csproj index 9528b3a6787..252b3fd7c7c 100644 --- a/src/OrchardCore/OrchardCore.Localization.Core/OrchardCore.Localization.Core.csproj +++ b/src/OrchardCore/OrchardCore.Localization.Core/OrchardCore.Localization.Core.csproj @@ -13,6 +13,10 @@ + + + + diff --git a/src/OrchardCore/OrchardCore.Localization.Core/PortableObject/PoParser.cs b/src/OrchardCore/OrchardCore.Localization.Core/PortableObject/PoParser.cs index c04878f56de..a6b672307f2 100644 --- a/src/OrchardCore/OrchardCore.Localization.Core/PortableObject/PoParser.cs +++ b/src/OrchardCore/OrchardCore.Localization.Core/PortableObject/PoParser.cs @@ -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 { @@ -11,12 +13,17 @@ namespace OrchardCore.Localization.PortableObject /// public class PoParser { - private static readonly Dictionary _escapeTranslations = new() + private static readonly FrozenDictionary _escapeTranslations; + + static PoParser() { - { 'n', '\n' }, - { 'r', '\r' }, - { 't', '\t' }, - }; + _escapeTranslations = new Dictionary() + { + { 'n', '\n' }, + { 'r', '\r' }, + { 't', '\t' } + }.ToFrozenDictionary(); + } /// /// Parses a .po file. @@ -55,32 +62,30 @@ public IEnumerable 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 @@ -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)