Use AsSpan/AsMemory slice constructor (#14265)

* Use AsSpan slice constructor

* Use AsMemory slice constructor
This commit is contained in:
xtqqczze
2020-11-27 13:05:41 +05:00
committed by GitHub
parent 1de0b64bc9
commit dc9ecd9a85
10 changed files with 14 additions and 14 deletions
@@ -1806,7 +1806,7 @@ $result
}
else
{
localUserName = string.Concat(computerName.AsSpan().Slice(0, dotIndex), "\\", psLocalCredential.UserName);
localUserName = string.Concat(computerName.AsSpan(0, dotIndex), "\\", psLocalCredential.UserName);
}
}
@@ -736,7 +736,7 @@ namespace Microsoft.PowerShell.Commands
UriBuilder uriBuilder = new(uri);
if (uriBuilder.Query != null && uriBuilder.Query.Length > 1)
{
uriBuilder.Query = string.Concat(uriBuilder.Query.AsSpan().Slice(1), "&", FormatDictionary(bodyAsDictionary));
uriBuilder.Query = string.Concat(uriBuilder.Query.AsSpan(1), "&", FormatDictionary(bodyAsDictionary));
}
else
{
@@ -1077,7 +1077,7 @@ namespace Microsoft.PowerShell
static object ConvertToBoolIfPossible(string arg)
{
// Before parsing we skip '$' if present.
return arg.Length > 0 && bool.TryParse(arg.AsSpan().Slice(arg[0] == '$' ? 1 : 0), out bool boolValue)
return arg.Length > 0 && bool.TryParse(arg.AsSpan(arg[0] == '$' ? 1 : 0), out bool boolValue)
? (object)boolValue
: (object)arg;
}
@@ -315,7 +315,7 @@ namespace Microsoft.PowerShell
int dateStartIndex = updateFileName.LastIndexOf('_') + 1;
if (!DateTime.TryParse(
updateFileName.AsSpan().Slice(dateStartIndex),
updateFileName.AsSpan(dateStartIndex),
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal,
out lastUpdateDate))
@@ -3647,13 +3647,13 @@ namespace Microsoft.WSMan.Management
else
{
if (ChildName.StartsWith(WSManStringLiterals.containerListener, StringComparison.OrdinalIgnoreCase))
result = string.Concat(WSManStringLiterals.containerListener, "_", ChildName.AsSpan().Slice(ChildName.IndexOf('_') + 1));
result = string.Concat(WSManStringLiterals.containerListener, "_", ChildName.AsSpan(ChildName.IndexOf('_') + 1));
if (ChildName.StartsWith(WSManStringLiterals.containerSingleResource, StringComparison.OrdinalIgnoreCase))
result = string.Concat(WSManStringLiterals.containerSingleResource, "_", ChildName.AsSpan().Slice(ChildName.IndexOf('_') + 1));
result = string.Concat(WSManStringLiterals.containerSingleResource, "_", ChildName.AsSpan(ChildName.IndexOf('_') + 1));
if (ChildName.StartsWith(WSManStringLiterals.containerSecurity, StringComparison.OrdinalIgnoreCase))
result = string.Concat(WSManStringLiterals.containerSecurity, "_", ChildName.AsSpan().Slice(ChildName.IndexOf('_') + 1));
result = string.Concat(WSManStringLiterals.containerSecurity, "_", ChildName.AsSpan(ChildName.IndexOf('_') + 1));
if (ChildName.StartsWith(WSManStringLiterals.containerClientCertificate, StringComparison.OrdinalIgnoreCase))
result = string.Concat(WSManStringLiterals.containerClientCertificate, "_", ChildName.AsSpan().Slice(ChildName.IndexOf('_') + 1));
result = string.Concat(WSManStringLiterals.containerClientCertificate, "_", ChildName.AsSpan(ChildName.IndexOf('_') + 1));
}
}
@@ -511,7 +511,7 @@ namespace System.Management.Automation
&& !string.IsNullOrWhiteSpace(context.WordToComplete) && context.WordToComplete.StartsWith('-'))
{
var lastAst = context.RelatedAsts.Last();
var wordToMatch = string.Concat(context.WordToComplete.AsSpan().Slice(1), "*");
var wordToMatch = string.Concat(context.WordToComplete.AsSpan(1), "*");
var pattern = WildcardPattern.Get(wordToMatch, WildcardOptions.IgnoreCase);
var parameterNames = keywordAst.CommandElements.Where(ast => ast is CommandParameterAst).Select(ast => (ast as CommandParameterAst).ParameterName);
foreach (var parameterName in s_parameterNamesOfImportDSCResource)
@@ -4707,7 +4707,7 @@ namespace System.Management.Automation
provider = wordToComplete.Substring(0, colon + 1);
if (s_variableScopes.Contains(provider, StringComparer.OrdinalIgnoreCase))
{
pattern = string.Concat("variable:", wordToComplete.AsSpan().Slice(colon + 1), "*");
pattern = string.Concat("variable:", wordToComplete.AsSpan(colon + 1), "*");
}
else
{
@@ -184,7 +184,7 @@ namespace System.Management.Automation
if (index == Pattern.Length - 1 && Pattern[index] == '*')
{
// No special characters present in the pattern before last position and last character is asterisk.
var patternWithoutAsterisk = Pattern.AsMemory().Slice(0, index);
var patternWithoutAsterisk = Pattern.AsMemory(0, index);
_isMatch = str => str.AsSpan().StartsWith(patternWithoutAsterisk.Span, GetStringComparison());
return;
}
@@ -234,7 +234,7 @@ namespace System.Management.Automation
// Convert this index into something printable (we hope)...
string msgString = PSObject.ToString(null, index, ",", null, null, true, true);
if (msgString.Length > 20)
msgString = string.Concat(msgString.AsSpan().Slice(0, 20), " ...");
msgString = string.Concat(msgString.AsSpan(0, 20), " ...");
return msgString;
}
@@ -407,7 +407,7 @@ namespace System.Management.Automation
else
{
string whitespaces = parameterName.Substring(endPosition);
parameterText = string.Concat("-", parameterName.AsSpan().Slice(0, endPosition), ":", whitespaces);
parameterText = string.Concat("-", parameterName.AsSpan(0, endPosition), ":", whitespaces);
}
return parameterText;
@@ -2376,7 +2376,7 @@ namespace Microsoft.PowerShell.Commands
if (strTargetPath.StartsWith(".\\", StringComparison.OrdinalIgnoreCase) ||
strTargetPath.StartsWith("./", StringComparison.OrdinalIgnoreCase))
{
normalizedTargetPath = Path.Join(SessionState.Internal.CurrentLocation.ProviderPath, strTargetPath.AsSpan().Slice(2));
normalizedTargetPath = Path.Join(SessionState.Internal.CurrentLocation.ProviderPath, strTargetPath.AsSpan(2));
}
GetFileSystemInfo(normalizedTargetPath, out isDirectory);