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

Linq optimizations #366

Merged
merged 1 commit into from
Jan 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
6 changes: 3 additions & 3 deletions src/SmartFormat/Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ private Format HandleParsingErrors(ParsingErrors parsingErrors, Format currentRe
// Placeholder without issues are left unmodified
for (var i = 0; i < currentResult.Items.Count; i++)
{
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Any(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Exists(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
{
var parent = ph.Format ?? FormatPool.Instance.Get().Initialize(Settings, ph.BaseString);
currentResult.Items[i] = LiteralTextPool.Instance.Get().Initialize(Settings, parent, parent.BaseString, ph.StartIndex, ph.EndIndex);
Expand All @@ -867,7 +867,7 @@ private Format HandleParsingErrors(ParsingErrors parsingErrors, Format currentRe
// Replace erroneous Placeholders with an empty LiteralText
for (var i = 0; i < currentResult.Items.Count; i++)
{
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Any(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Exists(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
{
var parent = ph.Format ?? FormatPool.Instance.Get().Initialize(Settings, ph.BaseString);
currentResult.Items[i] = LiteralTextPool.Instance.Get().Initialize(Settings, parent, parent.BaseString, ph.StartIndex, ph.StartIndex);
Expand All @@ -884,4 +884,4 @@ private Format HandleParsingErrors(ParsingErrors parsingErrors, Format currentRe
}

#endregion
}
}
4 changes: 2 additions & 2 deletions src/SmartFormat/Core/Settings/ParserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void AddCustomOperatorChars(IList<char> characters)
{
foreach (var c in characters)
{
if(DisallowedSelectorChars().Where(_ => OperatorChars().All(ch => ch != c)).Contains(c) ||
if(DisallowedSelectorChars().Where(_ => OperatorChars().TrueForAll(ch => ch != c)).Contains(c) ||
SelectorChars().Contains(c) || CustomSelectorChars().Contains(c))
throw new ArgumentException($"Cannot add '{c}' as a custom operator character. It is disallowed or in use as a selector.");

Expand Down Expand Up @@ -190,4 +190,4 @@ public void AddCustomOperatorChars(IList<char> characters)
FormatterNameSeparator, FormatterOptionsBeginChar, FormatterOptionsEndChar, PlaceholderBeginChar,
PlaceholderEndChar
};
}
}
2 changes: 1 addition & 1 deletion src/SmartFormat/Extensions/ChooseFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
var formats = formattingInfo.Format?.Split(SplitChar);

// Check whether arguments can be handled by this formatter
if (formats is null || formats.Count < 2 || chooseOptions is null)
if (formats is null || formats.Count < 2)
{
// Auto detection calls just return a failure to evaluate
if (string.IsNullOrEmpty(formattingInfo.Placeholder?.FormatterName))
Expand Down
10 changes: 5 additions & 5 deletions src/SmartFormat/SmartFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public SmartFormatter AddExtensions(params ISource[] sourceExtensions)
_ = InsertExtension(index, source);

// Also add the class as a formatter, if possible
if (source is IFormatter formatter && FormatterExtensions.All(fx => fx.GetType() != formatter.GetType())) AddExtensions(formatter);
if (source is IFormatter formatter && FormatterExtensions.TrueForAll(fx => fx.GetType() != formatter.GetType())) AddExtensions(formatter);
}

return this;
Expand Down Expand Up @@ -123,7 +123,7 @@ public SmartFormatter AddExtensions(params IFormatter[] formatterExtensions)
_ = InsertExtension(index, formatter);

// Also add the class as a source, if possible
if (formatter is ISource source && SourceExtensions.All(sx => sx.GetType() != source.GetType())) AddExtensions(source);
if (formatter is ISource source && SourceExtensions.TrueForAll(sx => sx.GetType() != source.GetType())) AddExtensions(source);
}

return this;
Expand All @@ -144,7 +144,7 @@ public SmartFormatter AddExtensions(params IFormatter[] formatterExtensions)
/// </exception>
public SmartFormatter InsertExtension(int position, ISource sourceExtension)
{
if (_sourceExtensions.Any(sx => sx.GetType() == sourceExtension.GetType())) return this;
if (_sourceExtensions.Exists(sx => sx.GetType() == sourceExtension.GetType())) return this;

if (sourceExtension is IInitializer sourceToInitialize)
sourceToInitialize.Initialize(this);
Expand All @@ -169,10 +169,10 @@ public SmartFormatter InsertExtension(int position, ISource sourceExtension)
/// </exception>
public SmartFormatter InsertExtension(int position, IFormatter formatterExtension)
{
if (_formatterExtensions.Any(sx => sx.GetType() == formatterExtension.GetType())) return this;
if (_formatterExtensions.Exists(sx => sx.GetType() == formatterExtension.GetType())) return this;

// Extension name is in use by a different type
if(_formatterExtensions.Any(fx => fx.Name.Equals(formatterExtension.Name)))
if(_formatterExtensions.Exists(fx => fx.Name.Equals(formatterExtension.Name)))
throw new ArgumentException($"Formatter '{formatterExtension.GetType().Name}' uses existing name.", nameof(formatterExtension));

if (formatterExtension is IInitializer formatterToInitialize)
Expand Down