Make use of StringSplitOptions.TrimEntries when possible (#18412)

This commit is contained in:
Berkan Diler
2022-10-31 22:20:22 +05:00
committed by GitHub
parent 9016adf1c2
commit 8619d8432f
2 changed files with 12 additions and 14 deletions
@@ -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);
@@ -1273,24 +1273,23 @@ namespace System.Management.Automation
};
var modulePathList = new List<string>();
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);
}
}
}