From 8619d8432fc5ea05cd207342ca8db2989f357290 Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Mon, 31 Oct 2022 18:20:22 +0100 Subject: [PATCH] Make use of StringSplitOptions.TrimEntries when possible (#18412) --- .../commands/utility/ConvertFrom-StringData.cs | 15 +++++++-------- .../engine/Modules/ModuleIntrinsics.cs | 11 +++++------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs index 284c344f7c..14cc2eecb6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs @@ -55,35 +55,34 @@ namespace Microsoft.PowerShell.Commands return; } - string[] lines = _stringData.Split('\n'); + string[] lines = _stringData.Split('\n', StringSplitOptions.TrimEntries); foreach (string line in lines) { - string s = line.Trim(); - if (string.IsNullOrEmpty(s) || s[0] == '#') + if (string.IsNullOrEmpty(line) || line[0] == '#') continue; - int index = s.IndexOf(Delimiter); + int index = line.IndexOf(Delimiter); if (index <= 0) { throw PSTraceSource.NewInvalidOperationException( ConvertFromStringData.InvalidDataLine, - s); + line); } - string name = s.Substring(0, index); + string name = line.Substring(0, index); name = name.Trim(); if (result.ContainsKey(name)) { throw PSTraceSource.NewInvalidOperationException( ConvertFromStringData.DataItemAlreadyDefined, - s, + line, name); } - string value = s.Substring(index + 1); + string value = line.Substring(index + 1); value = value.Trim(); value = Regex.Unescape(value); diff --git a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs index 10b5ffa34e..796ce35943 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs @@ -1273,24 +1273,23 @@ namespace System.Management.Automation }; var modulePathList = new List(); - foreach (var path in currentModulePath.Split(';')) + foreach (var path in currentModulePath.Split(';', StringSplitOptions.TrimEntries)) { - var trimmedPath = path.Trim(); - if (!excludeModulePaths.Contains(trimmedPath)) + if (!excludeModulePaths.Contains(path)) { // make sure this module path is Not part of other PS Core installation - var possiblePwshDir = Path.GetDirectoryName(trimmedPath); + var possiblePwshDir = Path.GetDirectoryName(path); if (string.IsNullOrEmpty(possiblePwshDir)) { // i.e. module dir is in the drive root - modulePathList.Add(trimmedPath); + modulePathList.Add(path); } else { if (!File.Exists(Path.Combine(possiblePwshDir, "pwsh.dll"))) { - modulePathList.Add(trimmedPath); + modulePathList.Add(path); } } }