From 483dd9e5cac6d5ce5bfa856c15beaf861a815e3a Mon Sep 17 00:00:00 2001 From: Ladi Prosek Date: Sun, 7 Feb 2021 18:49:41 +0100 Subject: [PATCH] Use SpanBasedConcatenator in ExpandPropertiesLeaveTypedAndEscaped --- src/Build/Evaluation/Expander.cs | 44 +++++--------------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/src/Build/Evaluation/Expander.cs b/src/Build/Evaluation/Expander.cs index f03b8a2858d..e074b66ff46 100644 --- a/src/Build/Evaluation/Expander.cs +++ b/src/Build/Evaluation/Expander.cs @@ -1093,7 +1093,7 @@ private static class PropertyExpander // so that we can either maintain the object's type in the event // that we have a single component, or convert to a string // if concatenation is required. - List results = new List(); + using Expander.SpanBasedConcatenator results = new Expander.SpanBasedConcatenator(); // The sourceIndex is the zero-based index into the expression, // where we've essentially read up to and copied into the target string. @@ -1107,7 +1107,7 @@ private static class PropertyExpander // (but not including) the "$(", and advance the sourceIndex pointer. if (propertyStartIndex - sourceIndex > 0) { - results.Add(expression.Substring(sourceIndex, propertyStartIndex - sourceIndex)); + results.Add(expression.AsMemory(sourceIndex, propertyStartIndex - sourceIndex)); } // Following the "$(" we need to locate the matching ')' @@ -1122,7 +1122,7 @@ private static class PropertyExpander // isn't really a well-formed property tag. Just literally // copy the remainder of the expression (starting with the "$(" // that we found) into the result, and quit. - results.Add(expression.Substring(propertyStartIndex, expression.Length - propertyStartIndex)); + results.Add(expression.AsMemory(propertyStartIndex, expression.Length - propertyStartIndex)); sourceIndex = expression.Length; } else @@ -1206,43 +1206,13 @@ private static class PropertyExpander propertyStartIndex = s_invariantCompareInfo.IndexOf(expression, "$(", sourceIndex, CompareOptions.Ordinal); } - // If we have only a single result, then just return it - if (results.Count == 1 && expression.Length == sourceIndex) + // If we couldn't find any more property tags in the expression just copy the remainder into the result. + if (expression.Length - sourceIndex > 0) { - var resultString = results[0] as string; - return resultString != null ? FileUtilities.MaybeAdjustFilePath(resultString) : results[0]; + results.Add(expression.AsMemory(sourceIndex, expression.Length - sourceIndex)); } - else - { - // The expression is constant, return it as is - if (sourceIndex == 0) - { - return expression; - } - - // We have more than one result collected, therefore we need to concatenate - // into the final result string. This does mean that we will lose type information. - // However since the user wanted contatenation, then they clearly wanted that to happen. - - // Initialize our output string to empty string. - // This method is called very often - of the order of 3,000 times per project. - using SpanBasedStringBuilder result = Strings.GetSpanBasedStringBuilder(); - // Create a combined result string from the result components that we've gathered - foreach (object component in results) - { - result.Append(FileUtilities.MaybeAdjustFilePath(component.ToString())); - } - - // And if we couldn't find anymore property tags in the expression, - // so just literally copy the remainder into the result. - if (expression.Length - sourceIndex > 0) - { - result.Append(expression, sourceIndex, expression.Length - sourceIndex); - } - - return result.ToString(); - } + return results.GetResult(); } ///