Skip to content

Commit f09c0bd

Browse files
authored
Linq optimizations (#366)
* Replace ICollection.Any(...) with ICollection.Exists(...) * Replace ICollection.All(...) with ICollection.TrueForAll(...)
1 parent a233d38 commit f09c0bd

4 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/SmartFormat/Core/Parsing/Parser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ private Format HandleParsingErrors(ParsingErrors parsingErrors, Format currentRe
856856
// Placeholder without issues are left unmodified
857857
for (var i = 0; i < currentResult.Items.Count; i++)
858858
{
859-
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Any(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
859+
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Exists(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
860860
{
861861
var parent = ph.Format ?? FormatPool.Instance.Get().Initialize(Settings, ph.BaseString);
862862
currentResult.Items[i] = LiteralTextPool.Instance.Get().Initialize(Settings, parent, parent.BaseString, ph.StartIndex, ph.EndIndex);
@@ -867,7 +867,7 @@ private Format HandleParsingErrors(ParsingErrors parsingErrors, Format currentRe
867867
// Replace erroneous Placeholders with an empty LiteralText
868868
for (var i = 0; i < currentResult.Items.Count; i++)
869869
{
870-
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Any(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
870+
if (currentResult.Items[i] is Placeholder ph && parsingErrors.Issues.Exists(errItem => errItem.Index >= currentResult.Items[i].StartIndex && errItem.Index <= currentResult.Items[i].EndIndex))
871871
{
872872
var parent = ph.Format ?? FormatPool.Instance.Get().Initialize(Settings, ph.BaseString);
873873
currentResult.Items[i] = LiteralTextPool.Instance.Get().Initialize(Settings, parent, parent.BaseString, ph.StartIndex, ph.StartIndex);
@@ -884,4 +884,4 @@ private Format HandleParsingErrors(ParsingErrors parsingErrors, Format currentRe
884884
}
885885

886886
#endregion
887-
}
887+
}

src/SmartFormat/Core/Settings/ParserSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void AddCustomOperatorChars(IList<char> characters)
8282
{
8383
foreach (var c in characters)
8484
{
85-
if(DisallowedSelectorChars().Where(_ => OperatorChars().All(ch => ch != c)).Contains(c) ||
85+
if(DisallowedSelectorChars().Where(_ => OperatorChars().TrueForAll(ch => ch != c)).Contains(c) ||
8686
SelectorChars().Contains(c) || CustomSelectorChars().Contains(c))
8787
throw new ArgumentException($"Cannot add '{c}' as a custom operator character. It is disallowed or in use as a selector.");
8888

@@ -190,4 +190,4 @@ public void AddCustomOperatorChars(IList<char> characters)
190190
FormatterNameSeparator, FormatterOptionsBeginChar, FormatterOptionsEndChar, PlaceholderBeginChar,
191191
PlaceholderEndChar
192192
};
193-
}
193+
}

src/SmartFormat/Extensions/ChooseFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
4848
var formats = formattingInfo.Format?.Split(SplitChar);
4949

5050
// Check whether arguments can be handled by this formatter
51-
if (formats is null || formats.Count < 2 || chooseOptions is null)
51+
if (formats is null || formats.Count < 2)
5252
{
5353
// Auto detection calls just return a failure to evaluate
5454
if (string.IsNullOrEmpty(formattingInfo.Placeholder?.FormatterName))

src/SmartFormat/SmartFormatter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public SmartFormatter AddExtensions(params ISource[] sourceExtensions)
9595
_ = InsertExtension(index, source);
9696

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

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

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

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

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

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

178178
if (formatterExtension is IInitializer formatterToInitialize)

0 commit comments

Comments
 (0)