From b118aa95c52a057638d5cfa8fff04442b36cd0b9 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 6 Jan 2016 10:05:13 -0800 Subject: [PATCH] Remove use of `String.Split` in Environment taghelper Fixes #3617 --- .../EnvironmentTagHelper.cs | 50 +++++++------------ .../project.json | 1 + 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/EnvironmentTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/EnvironmentTagHelper.cs index 80b3250107..5b0ac65399 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/EnvironmentTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/EnvironmentTagHelper.cs @@ -2,9 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Razor.TagHelpers; +using Microsoft.Extensions.Primitives; namespace Microsoft.AspNet.Mvc.TagHelpers { @@ -14,7 +14,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers /// public class EnvironmentTagHelper : TagHelper { - private static readonly char[] NameSeparator = new[] { ',' }; + private static readonly char NameSeparator = ','; /// /// Creates a new . @@ -26,13 +26,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers } /// - public override int Order - { - get - { - return -1000; - } - } + public override int Order => -1000; /// /// A comma separated list of environment names in which the content should be rendered. @@ -74,36 +68,28 @@ namespace Microsoft.AspNet.Mvc.TagHelpers return; } - var values = Names.Split(NameSeparator, StringSplitOptions.RemoveEmptyEntries); - var environments = new List(); - - for (var i = 0; i < values.Length; i++) + var tokenizer = new StringTokenizer(Names, NameSeparator); + var hasEnvironments = false; + foreach (var item in tokenizer) { - var trimmedValue = values[i].Trim(); - - if (trimmedValue.Length > 0) + var environment = item.Trim(); + if (environment.HasValue && environment.Length > 0) { - environments.Add(trimmedValue); + hasEnvironments = true; + if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase)) + { + // Matching environment name found, do nothing + return; + } } } - if (environments.Count == 0) + if (hasEnvironments) { - // Names contains only commas or empty entries, do nothing - return; + // This instance had at least one non-empty environment specified but none of these + // environments matched the current environment. Suppress the output in this case. + output.SuppressOutput(); } - - for (var i = 0; i < environments.Count; i++) - { - if (string.Equals(environments[i], currentEnvironmentName, StringComparison.OrdinalIgnoreCase)) - { - // Matching environment name found, do nothing - return; - } - } - - // No matching environment name found, suppress all output - output.SuppressOutput(); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/project.json b/src/Microsoft.AspNet.Mvc.TagHelpers/project.json index a10984778f..5fd05809dc 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/project.json +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/project.json @@ -17,6 +17,7 @@ "version": "1.0.0-*", "type": "build" }, + "Microsoft.Extensions.Primitives": "1.0.0-*", "Microsoft.Extensions.PropertyHelper.Sources": { "version": "1.0.0-*", "type": "build"