-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add argument count mismatch warnings for Client
- Loading branch information
Showing
2 changed files
with
78 additions
and
29 deletions.
There are no files selected for viewing
59 changes: 38 additions & 21 deletions
59
Framework/Intersect.Framework.Core/Localization/LocalizedString.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,58 @@ | ||
namespace Intersect.Localization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Intersect.Localization; | ||
|
||
[Serializable] | ||
public partial class LocalizedString : Localized | ||
public partial class LocalizedString(string value) : Localized | ||
{ | ||
private readonly string _value = value; | ||
|
||
private readonly string mValue; | ||
public int ArgumentCount { get; } = CountArguments(value); | ||
|
||
public LocalizedString(string value) | ||
{ | ||
mValue = value; | ||
} | ||
public static implicit operator LocalizedString(string value) => new(value); | ||
|
||
public static implicit operator LocalizedString(string value) | ||
{ | ||
return new LocalizedString(value); | ||
} | ||
public static implicit operator string(LocalizedString localizedString) => localizedString._value; | ||
|
||
public static implicit operator string(LocalizedString str) | ||
{ | ||
return str.mValue; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return mValue; | ||
} | ||
public override string ToString() => _value; | ||
|
||
public string ToString(params object[] args) | ||
{ | ||
try | ||
{ | ||
return args?.Length == 0 ? mValue : string.Format(mValue, args ?? new object[] { }); | ||
return args?.Length == 0 ? _value : string.Format(_value, args ?? []); | ||
} | ||
catch (FormatException) | ||
{ | ||
return "Format Exception!"; | ||
} | ||
} | ||
|
||
private static readonly Regex PatternArgument = new( | ||
"\\{(?<argumentIndex>\\d+)\\}", | ||
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.NonBacktracking | ||
); | ||
|
||
public static int CountArguments(string formatString) | ||
{ | ||
HashSet<int> argumentIndices = []; | ||
|
||
var matches = PatternArgument.Matches(formatString); | ||
foreach (Match match in matches) | ||
{ | ||
if (!match.Success) | ||
{ | ||
continue; | ||
} | ||
|
||
var rawArgumentIndex = match.Groups["argumentIndex"].Value; | ||
if (!int.TryParse(rawArgumentIndex, out var argumentIndex)) | ||
{ | ||
continue; | ||
} | ||
|
||
_ = argumentIndices.Add(argumentIndex); | ||
} | ||
|
||
return argumentIndices.Count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters