Use span-based overloads (#11884)

This commit is contained in:
Ilya
2020-03-18 18:38:55 +05:00
committed by GitHub
parent 320656c8de
commit b5d4739b2a
40 changed files with 85 additions and 82 deletions
@@ -429,7 +429,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal
commandName = this.ModuleName + "\\" + commandName;
}
if (commandName.IndexOf(' ') != -1)
if (commandName.Contains(' '))
{
builder.AppendFormat("& \"{0}\"", commandName);
}
@@ -435,21 +435,21 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal
/// <returns>Return match result.</returns>
private static bool MatchesEvenIfInPlural(string commandName, string filter)
{
if (commandName.IndexOf(filter, StringComparison.OrdinalIgnoreCase) != -1)
if (commandName.Contains(filter, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (filter.Length > 5 && filter.EndsWith("es", StringComparison.OrdinalIgnoreCase))
{
filter = filter.Substring(0, filter.Length - 2);
return commandName.IndexOf(filter, StringComparison.OrdinalIgnoreCase) != -1;
ReadOnlySpan<char> filterSpan = filter.AsSpan(0, filter.Length - 2);
return commandName.AsSpan().Contains(filterSpan, StringComparison.OrdinalIgnoreCase);
}
if (filter.Length > 4 && filter.EndsWith("s", StringComparison.OrdinalIgnoreCase))
{
filter = filter.Substring(0, filter.Length - 1);
return commandName.IndexOf(filter, StringComparison.OrdinalIgnoreCase) != -1;
ReadOnlySpan<char> filterSpan = filter.AsSpan(0, filter.Length - 1);
return commandName.AsSpan().Contains(filterSpan, StringComparison.OrdinalIgnoreCase);
}
return false;
@@ -459,8 +459,8 @@ namespace Microsoft.PowerShell.Cim
return exceptionSafeReturn(delegate
{
int indexOfLastColon = cimIntrinsicValue.LastIndexOf(':');
int port = int.Parse(cimIntrinsicValue.Substring(indexOfLastColon + 1), NumberStyles.Integer, CultureInfo.InvariantCulture);
IPAddress address = IPAddress.Parse(cimIntrinsicValue.Substring(0, indexOfLastColon));
int port = int.Parse(cimIntrinsicValue.AsSpan(indexOfLastColon + 1), NumberStyles.Integer, CultureInfo.InvariantCulture);
IPAddress address = IPAddress.Parse(cimIntrinsicValue.AsSpan(0, indexOfLastColon));
return new IPEndPoint(address, port);
});
}
@@ -72,7 +72,7 @@ namespace Microsoft.PowerShell.Commands
if (trimmedValue.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
if ((trimmedValue.Length > 2) &&
int.TryParse(trimmedValue.Substring(2), NumberStyles.AllowHexSpecifier,
int.TryParse(trimmedValue.AsSpan(2), NumberStyles.AllowHexSpecifier,
CultureInfo.CurrentCulture, out cultureNumber))
{
_cultureInfo = new CultureInfo(cultureNumber);
@@ -377,7 +377,7 @@ namespace Microsoft.PowerShell.Commands
try
{
_commandViewModelObj = _showCommandProxy.GetCommandViewModel(new ShowCommandCommandInfo(commandInfo), _noCommonParameter.ToBool(), _importedModules, this.Name.IndexOf('\\') != -1);
_commandViewModelObj = _showCommandProxy.GetCommandViewModel(new ShowCommandCommandInfo(commandInfo), _noCommonParameter.ToBool(), _importedModules, this.Name.Contains('\\'));
_showCommandProxy.ShowCommandWindow(_commandViewModelObj, _passThrough);
}
catch (TargetInvocationException ti)
@@ -1865,8 +1865,11 @@ namespace Microsoft.PowerShell
/// <returns>The string with any \0 characters removed...</returns>
private string RemoveNulls(string input)
{
if (input.IndexOf('\0') == -1)
if (input.Contains('\0'))
{
return input;
}
StringBuilder sb = new StringBuilder();
foreach (char c in input)
{
@@ -4105,7 +4105,7 @@ namespace Microsoft.WSMan.Management
PSObject obj = (PSObject)objcache[CurrentNode];
CurrentNode = RemainingPath.Substring(pos + 1);
if (CurrentNode.IndexOf(WSManStringLiterals.DefaultPathSeparator) != -1)
if (CurrentNode.Contains(WSManStringLiterals.DefaultPathSeparator))
{
// No more directories allowed after listeners objects
return false;
@@ -436,7 +436,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
}
sb.Append(GenerateRowField(values[k], _si.columnInfo[k].width, alignment[k], dc, addPadding));
if (values[k].IndexOf(ESC) != -1)
if (values[k].Contains(ESC))
{
// Reset the console output if the content of this column contains ESC
sb.Append(ResetConsoleVt100Code);
@@ -177,7 +177,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
if (key.Length < normalizedKey.Length)
{
// shorter, could be an abbreviation
if (string.Equals(key, normalizedKey.Substring(0, key.Length), StringComparison.OrdinalIgnoreCase))
if (key.AsSpan().Equals(normalizedKey.AsSpan(0, key.Length), StringComparison.OrdinalIgnoreCase))
{
// found abbreviation
return true;
@@ -3173,7 +3173,7 @@ namespace System.Management.Automation
for (int i = 0; i < parameterName.Length; i++)
{
// try Upper case
if (char.IsUpper(parameterName[i]) && (usedHotKeysStr.IndexOf(parameterName[i]) == -1))
if (char.IsUpper(parameterName[i]) && usedHotKeysStr.Contains(parameterName[i]))
{
label.Insert(i, hotKeyPrefix);
usedHotKeys.Append(parameterName[i]);
@@ -3187,7 +3187,7 @@ namespace System.Management.Automation
// try Lower case
for (int i = 0; i < parameterName.Length; i++)
{
if (char.IsLower(parameterName[i]) && (usedHotKeysStr.IndexOf(parameterName[i]) == -1))
if (char.IsLower(parameterName[i]) && usedHotKeysStr.Contains(parameterName[i]))
{
label.Insert(i, hotKeyPrefix);
usedHotKeys.Append(parameterName[i]);
@@ -3202,7 +3202,7 @@ namespace System.Management.Automation
// try non-letters
for (int i = 0; i < parameterName.Length; i++)
{
if (!char.IsLetter(parameterName[i]) && (usedHotKeysStr.IndexOf(parameterName[i]) == -1))
if (!char.IsLetter(parameterName[i]) && usedHotKeysStr.Contains(parameterName[i]))
{
label.Insert(i, hotKeyPrefix);
usedHotKeys.Append(parameterName[i]);
@@ -77,7 +77,7 @@ namespace System.Management.Automation.ComInterop
// See if we can find an existing type
foreach (Type t in s_generatedTypes)
{
int arity = int.Parse(t.Name.Substring("VariantArray".Length), CultureInfo.InvariantCulture);
int arity = int.Parse(t.Name.AsSpan("VariantArray".Length), NumberStyles.Integer, CultureInfo.InvariantCulture);
if (size == arity)
{
return t;
@@ -1514,19 +1514,18 @@ namespace System.Management.Automation
var analysis = new CompletionAnalysis(_ast, _tokens, _cursorPosition, _options);
var subContext = analysis.CreateCompletionContext(completionContext.TypeInferenceContext);
int subReplaceIndex, subReplaceLength;
var subResult = analysis.GetResultHelper(subContext, out subReplaceIndex, out subReplaceLength, true);
var subResult = analysis.GetResultHelper(subContext, out int subReplaceIndex, out _, true);
if (subResult != null && subResult.Count > 0)
{
result = new List<CompletionResult>();
replacementIndex = stringStartIndex + 1 + (cursorIndexInString - subInput.Length);
replacementLength = subInput.Length;
string prefix = subInput.Substring(0, subReplaceIndex);
ReadOnlySpan<char> prefix = subInput.AsSpan(0, subReplaceIndex);
foreach (CompletionResult entry in subResult)
{
string completionText = prefix + entry.CompletionText;
string completionText = string.Concat(prefix, entry.CompletionText.AsSpan());
if (entry.ResultType == CompletionResultType.Property)
{
completionText = TokenKind.DollarParen.Text() + completionText + TokenKind.RParen.Text();
@@ -1565,7 +1564,7 @@ namespace System.Management.Automation
result = new List<CompletionResult>(CompletionCompleters.CompleteFilename(completionContext));
// Try command name completion only if the text contains '-'
if (wordToComplete.IndexOf('-') != -1)
if (wordToComplete.Contains('-'))
{
var commandNameResult = CompletionCompleters.CompleteCommand(completionContext);
if (commandNameResult != null && commandNameResult.Count > 0)
@@ -1164,7 +1164,7 @@ namespace System.Management.Automation
var tryCmdletCompletion = false;
var clearLiteralPathsKey = TurnOnLiteralPathOption(context);
if (context.WordToComplete.IndexOf('-') != -1)
if (context.WordToComplete.Contains('-'))
{
tryCmdletCompletion = true;
}
@@ -1224,7 +1224,7 @@ namespace System.Management.Automation
}
// Handle member completion with wildcard: echo $a.*<tab>
if (pathAst.Value.IndexOf('*') != -1 && secondToLastMemberAst != null &&
if (pathAst.Value.Contains('*') && secondToLastMemberAst != null &&
secondToLastMemberAst.Extent.EndLineNumber == pathAst.Extent.StartLineNumber &&
secondToLastMemberAst.Extent.EndColumnNumber == pathAst.Extent.StartColumnNumber)
{
@@ -1328,7 +1328,7 @@ namespace System.Management.Automation
context.Options.Remove("LiteralPaths");
}
if (context.WordToComplete != string.Empty && context.WordToComplete.IndexOf('-') != -1)
if (context.WordToComplete != string.Empty && context.WordToComplete.Contains('-'))
{
var commandResults = CompleteCommand(context);
if (commandResults != null)
@@ -3844,7 +3844,7 @@ namespace System.Management.Automation
wordToComplete = wordToComplete.Substring(1, wordToComplete.Length - (endQuoted ? 2 : 1));
}
if (wordToComplete.IndexOf('[') != -1)
if (wordToComplete.Contains('['))
{
var cursor = (InternalScriptPosition)context.CursorPosition;
cursor = cursor.CloneWithNewOffset(cursor.Offset - context.TokenAtCursor.Extent.StartOffset - (isQuoted ? 1 : 0));
@@ -5632,7 +5632,7 @@ namespace System.Management.Automation
// If the completion included a namespace and ToStringCodeMethods.Type found
// an accelerator, then just use the type's FullName instead because the user
// probably didn't want the accelerator.
if (keyMatched.IndexOf('.') != -1 && completion.IndexOf('.') == -1)
if (keyMatched.Contains('.') && !completion.Contains('.'))
{
completion = Type.FullName;
}
@@ -6624,7 +6624,7 @@ namespace System.Management.Automation
}
string tooltip = memberInfo.ToString();
if (tooltip.IndexOf("),", StringComparison.Ordinal) != -1)
if (tooltip.Contains("),", StringComparison.Ordinal))
{
var overloads = tooltip.Split("),", StringSplitOptions.RemoveEmptyEntries);
var newTooltip = new StringBuilder();
@@ -208,7 +208,7 @@ namespace System.Management.Automation
/// </summary>
internal static bool IsEngineFeatureName(string featureName)
{
return featureName.Length > 2 && featureName.IndexOf('.') == -1 && featureName.StartsWith("PS", StringComparison.Ordinal);
return featureName.Length > 2 && !featureName.Contains('.') && featureName.StartsWith("PS", StringComparison.Ordinal);
}
/// <summary>
@@ -440,7 +440,7 @@ namespace Microsoft.PowerShell.Commands
{
foreach (var n in names)
{
if (n.IndexOf(StringLiterals.DefaultPathSeparator) != -1 || n.IndexOf(StringLiterals.AlternatePathSeparator) != -1)
if (n.Contains(StringLiterals.DefaultPathSeparator) || n.Contains(StringLiterals.AlternatePathSeparator))
{
string errorMessage = StringUtil.Format(stringFormat, n);
var argumentException = new ArgumentException(errorMessage);
@@ -925,7 +925,7 @@ namespace Microsoft.PowerShell.Commands
{
foreach (var n in names)
{
if (n.IndexOf(StringLiterals.DefaultPathSeparator) != -1 || n.IndexOf(StringLiterals.AlternatePathSeparator) != -1)
if (n.Contains(StringLiterals.DefaultPathSeparator) || n.Contains(StringLiterals.AlternatePathSeparator))
{
modulePaths.Add(n);
}
@@ -81,7 +81,7 @@ namespace System.Management.Automation
if (parameter.ParameterNameSpecified)
{
Diagnostics.Assert(parameter.ParameterText.IndexOf(' ') == -1, "Parameters cannot have whitespace");
Diagnostics.Assert(!parameter.ParameterText.Contains(' '), "Parameters cannot have whitespace");
PossiblyGlobArg(parameter.ParameterText, usedQuotes: false);
if (parameter.SpaceAfterParameter)
@@ -345,7 +345,7 @@ namespace System.Management.Automation
{
param = CommandParameterInternal.CreateParameterWithArgument(
/*parameterAst*/null, paramText.Substring(1, colonIndex - 1), paramText,
/*argumentAst*/null, paramText.Substring(colonIndex + 1).Trim(),
/*argumentAst*/null, paramText.AsSpan(colonIndex + 1).Trim().ToString(),
false);
}
else if (argIndex == arguments.Length - 1 || paramText[paramText.Length - 1] != ':')
@@ -292,7 +292,7 @@ namespace System.Management.Automation
}
// If the type is really an array, but the typename didn't include [], then add it.
if (type.IsArray && (parameterTypeString.IndexOf("[]", StringComparison.Ordinal) == -1))
if (type.IsArray && !parameterTypeString.Contains("[]", StringComparison.Ordinal))
{
var t = type;
while (t.IsArray)
@@ -220,7 +220,7 @@ namespace System.Management.Automation
// If the breakpoint looks like it might have specified a module name and the command
// we're checking is in a module, try matching the module\command against the pattern
// in the breakpoint.
if (!string.IsNullOrEmpty(commandInfo.ModuleName) && Command.IndexOf('\\') != -1)
if (!string.IsNullOrEmpty(commandInfo.ModuleName) && Command.Contains('\\'))
{
if (CommandPattern.IsMatch(commandInfo.ModuleName + "\\" + commandInfo.Name))
return true;
@@ -1247,7 +1247,7 @@ namespace System.Management.Automation.Host
if (andPos + 1 < choices[i].Label.Length)
{
splitLabel.Append(choices[i].Label.Substring(andPos + 1));
hotkeysAndPlainLabels[0, i] = CultureInfo.CurrentCulture.TextInfo.ToUpper(choices[i].Label.Substring(andPos + 1, 1).Trim());
hotkeysAndPlainLabels[0, i] = CultureInfo.CurrentCulture.TextInfo.ToUpper(choices[i].Label.AsSpan(andPos + 1, 1).Trim().ToString());
}
hotkeysAndPlainLabels[1, i] = splitLabel.ToString().Trim();
@@ -68,7 +68,7 @@ namespace System.Management.Automation.Language
var parser = new Parser();
if (!string.IsNullOrEmpty(fileName) && fileName.Length > scriptSchemaExtension.Length && fileName.EndsWith(scriptSchemaExtension, StringComparison.OrdinalIgnoreCase))
{
parser._keywordModuleName = Path.GetFileName(fileName.Substring(0, fileName.Length - scriptSchemaExtension.Length));
parser._keywordModuleName = Path.GetFileName(fileName.AsSpan(0, fileName.Length - scriptSchemaExtension.Length)).ToString();
parseDscResource = true;
}
@@ -8107,7 +8107,7 @@ namespace System.Management.Automation.Language
internal static string GetFileOrScript(string fileName, string input)
{
return fileName ?? input.Substring(0, Math.Min(256, input.Length)).Trim();
return fileName ?? input.AsSpan(0, Math.Min(256, input.Length)).Trim().ToString();
}
}
}
@@ -150,7 +150,7 @@ namespace System.Management.Automation.Language
: sourceLine.Length - position.StartColumnNumber + 1;
// Expand tabs before figuring out if we need to truncate the line
if (sourceLine.IndexOf('\t') != -1)
if (sourceLine.Contains('\t'))
{
var copyLine = new StringBuilder(sourceLine.Length * 2);
@@ -1238,7 +1238,7 @@ namespace System.Management.Automation.Language
string varSign = varAst.Splatted ? "@" : "$";
string newVarName = varSign + UsingExpressionAst.UsingPrefix + varName;
newScript.Append(script.Substring(startOffset, astStartOffset - startOffset));
newScript.Append(script.AsSpan(startOffset, astStartOffset - startOffset));
newScript.Append(newVarName);
startOffset = astEndOffset;
}
@@ -1259,13 +1259,13 @@ namespace System.Management.Automation.Language
newParams += ",\n";
}
newScript.Append(script.Substring(startOffset, currentOffset - startOffset));
newScript.Append(script.AsSpan(startOffset, currentOffset - startOffset));
newScript.Append(newParams);
startOffset = currentOffset;
}
}
newScript.Append(script.Substring(startOffset, endOffset - startOffset));
newScript.Append(script.AsSpan(startOffset, endOffset - startOffset));
string result = newScript.ToString();
if (Parent != null && initialStartOffset == this.Extent.StartOffset && initialEndOffset == this.Extent.EndOffset)
@@ -2304,7 +2304,7 @@ namespace System.Management.Automation.Language
string varSign = varAst.Splatted ? "@" : "$";
string newVarName = varSign + UsingExpressionAst.UsingPrefix + varName;
newParamText.Append(paramText.Substring(startOffset, astStartOffset - startOffset));
newParamText.Append(paramText.AsSpan(startOffset, astStartOffset - startOffset));
newParamText.Append(newVarName);
startOffset = astEndOffset;
} while (orderedUsingVar.MoveNext());
@@ -2315,7 +2315,7 @@ namespace System.Management.Automation.Language
return paramText;
}
newParamText.Append(paramText.Substring(startOffset, endOffset - startOffset));
newParamText.Append(paramText.AsSpan(startOffset, endOffset - startOffset));
return newParamText.ToString();
}
@@ -6643,17 +6643,17 @@ namespace System.Management.Automation.Language
string paramName = entry.Key;
var paramValue = entry.Value;
if ((paramName.Length <= nameParam.Length) && (paramName.Equals(nameParam.Substring(0, paramName.Length), StringComparison.OrdinalIgnoreCase)))
if ((paramName.Length <= nameParam.Length) && (paramName.AsSpan().Equals(nameParam.AsSpan(0, paramName.Length), StringComparison.OrdinalIgnoreCase)))
{
resourceNames = paramValue;
}
// Since both parameters -ModuleName and -ModuleVersion has same start string i.e. Module so we will try to resolve it to -ModuleName
// if user specifies like -Module
if ((paramName.Length <= moduleNameParam.Length) && (paramName.Equals(moduleNameParam.Substring(0, paramName.Length), StringComparison.OrdinalIgnoreCase)))
if ((paramName.Length <= moduleNameParam.Length) && (paramName.AsSpan().Equals(moduleNameParam.AsSpan(0, paramName.Length), StringComparison.OrdinalIgnoreCase)))
{
moduleNames = paramValue;
}
else if ((paramName.Length <= moduleVersionParam.Length) && (paramName.Equals(moduleVersionParam.Substring(0, paramName.Length), StringComparison.OrdinalIgnoreCase)))
else if ((paramName.Length <= moduleVersionParam.Length) && (paramName.AsSpan().Equals(moduleVersionParam.AsSpan(0, paramName.Length), StringComparison.OrdinalIgnoreCase)))
{
moduleVersion = paramValue;
}
@@ -8401,7 +8401,7 @@ namespace System.Management.Automation.Language
int lastDotIndex = fullTypeName.LastIndexOf('.');
if (lastDotIndex >= 0)
{
return fullTypeName.Substring(lastDotIndex + 1).Equals(Name, StringComparison.OrdinalIgnoreCase);
return fullTypeName.AsSpan(lastDotIndex + 1).Equals(Name, StringComparison.OrdinalIgnoreCase);
}
return false;
@@ -510,7 +510,7 @@ namespace System.Management.Automation
{
Dbg.Assert(scriptExtent.StartScriptPosition.ColumnNumber > 0, "Column numbers start at 1");
Dbg.Assert(scriptExtent.StartScriptPosition.ColumnNumber <= scriptExtent.StartScriptPosition.Line.Length, "Column numbers are not greater than the length of a line");
return scriptExtent.StartScriptPosition.Line.Substring(scriptExtent.StartScriptPosition.ColumnNumber - 1).Trim();
return scriptExtent.StartScriptPosition.Line.AsSpan(scriptExtent.StartScriptPosition.ColumnNumber - 1).Trim().ToString();
}
return invocationInfo.InvocationName;
@@ -2151,7 +2151,7 @@ namespace System.Management.Automation.Runspaces.Internal
private static bool CheckForSSL(WSManConnectionInfo wsmanConnectionInfo)
{
return (!string.IsNullOrEmpty(wsmanConnectionInfo.Scheme) &&
wsmanConnectionInfo.Scheme.IndexOf(WSManConnectionInfo.HttpsScheme, StringComparison.OrdinalIgnoreCase) != -1);
wsmanConnectionInfo.Scheme.Contains(WSManConnectionInfo.HttpsScheme, StringComparison.OrdinalIgnoreCase));
}
private static int ConvertPSAuthToWSManAuth(AuthenticationMechanism psAuth)
@@ -648,7 +648,7 @@ namespace Microsoft.PowerShell.Commands
int pAppDomainIndex = namedPipe.IndexOf('.', pIdIndex + 1);
if (pAppDomainIndex > -1)
{
string idString = namedPipe.Substring(pIdIndex + 1, (pAppDomainIndex - pIdIndex - 1));
ReadOnlySpan<char> idString = namedPipe.AsSpan(pIdIndex + 1, (pAppDomainIndex - pIdIndex - 1));
int id = -1;
if (int.TryParse(idString, out id))
{
@@ -1439,7 +1439,7 @@ namespace System.Management.Automation.Runspaces
return (EnableNetworkAccess && // Interactive token requested
(Credential == null && // No credential provided
(ComputerName.Equals(DefaultComputerName, StringComparison.OrdinalIgnoreCase) || // Localhost computer name
ComputerName.IndexOf('.') == -1))); // Not FQDN computer name
!ComputerName.Contains('.')))); // Not FQDN computer name
}
}
@@ -1896,7 +1896,7 @@ namespace System.Management.Automation.Remoting
private string GetRoleCapabilityPath(string roleCapability)
{
string moduleName = "*";
if (roleCapability.IndexOf('\\') != -1)
if (roleCapability.Contains('\\'))
{
string[] components = roleCapability.Split(Utils.Separators.Backslash, 2);
moduleName = components[0];
@@ -760,7 +760,7 @@ namespace System.Management.Automation.Remoting.Client
try
{
// Route protocol message based on whether it is a session or command message.
if (data.IndexOf(SESSIONDMESSAGETAG, StringComparison.OrdinalIgnoreCase) > -1)
if (data.Contains(SESSIONDMESSAGETAG, StringComparison.OrdinalIgnoreCase))
{
// Session message
_sessionMessageQueue.Add(data);
@@ -1708,7 +1708,7 @@ namespace System.Management.Automation.Remoting.Client
}
if ((error.Length == 0) ||
error.IndexOf("WARNING:", StringComparison.OrdinalIgnoreCase) > -1)
error.Contains("WARNING:", StringComparison.OrdinalIgnoreCase))
{
// Handle as interactive warning message
Console.WriteLine(error);
@@ -890,8 +890,8 @@ namespace System.Management.Automation
}
else if (powershell.Commands.Commands.Count == 1 &&
!powershell.Commands.Commands[0].IsScript &&
((powershell.Commands.Commands[0].CommandText.IndexOf("Get-PSDebuggerStopArgs", StringComparison.OrdinalIgnoreCase) != -1) ||
(powershell.Commands.Commands[0].CommandText.IndexOf("Set-PSDebuggerAction", StringComparison.OrdinalIgnoreCase) != -1)))
(powershell.Commands.Commands[0].CommandText.Contains("Get-PSDebuggerStopArgs", StringComparison.OrdinalIgnoreCase) ||
powershell.Commands.Commands[0].CommandText.Contains("Set-PSDebuggerAction", StringComparison.OrdinalIgnoreCase)))
{
// We do not want to invoke debugger commands in the steppable pipeline.
// Consider adding IsSteppable message to PSRP to handle this.
@@ -551,7 +551,7 @@ namespace System.Management.Automation
"^(global:){0,1}(PID|PSVersionTable|PSEdition|PSHOME|HOST|TRUE|FALSE|NULL)$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant).Success == false)
{
updatedScriptblock.Append(scriptblockBodyString.Substring(position, v.Extent.StartOffset - pipelineOffset - position));
updatedScriptblock.Append(scriptblockBodyString.AsSpan(position, v.Extent.StartOffset - pipelineOffset - position));
updatedScriptblock.Append("${using:");
updatedScriptblock.Append(CodeGeneration.EscapeVariableName(variableName));
updatedScriptblock.Append('}');
@@ -559,7 +559,7 @@ namespace System.Management.Automation
}
}
updatedScriptblock.Append(scriptblockBodyString.Substring(position));
updatedScriptblock.Append(scriptblockBodyString.AsSpan(position));
var sb = ScriptBlock.Create(updatedScriptblock.ToString());
var commandInfo = new CmdletInfo("Start-Job", typeof(StartJobCommand));
commandProcessor = context.CommandDiscovery.LookupCommandProcessor(commandInfo, CommandOrigin.Internal, false, context.EngineSessionState);
@@ -53,12 +53,16 @@ namespace System.Management.Automation
return new string(s[0], times);
}
// Convert the string to a char array, use the array multiplication code,
// then construct a new string from the resulting char array. This uses
// extra memory compared to the naive algorithm, but is faster (measured
// against a V2 CLR, should be measured against V4 as the StringBuilder
// implementation changed.)
return new string(ArrayOps.Multiply(s.ToCharArray(), (uint)times));
return string.Create(s.Length * times, (s, times), (dst, args) =>
{
ReadOnlySpan<char> src = args.s.AsSpan();
int length = src.Length;
for (int i = 0; i < args.times; i++)
{
src.CopyTo(dst);
dst = dst.Slice(length);
}
});
}
internal static string FormatOperator(string formatString, object formatArgs)
@@ -274,7 +274,7 @@ namespace System.Management.Automation
// foo "-abc"
// This is important when splatting, we reconstruct the parameter if the
// value is splatted.
var parameterText = new PSObject(new string(parameter.ParameterText.ToCharArray()));
var parameterText = new PSObject(new string(parameter.ParameterText));
if (parameterText.Properties[NotePropertyNameForSplattingParametersInArgs] == null)
{
var noteProperty = new PSNoteProperty(NotePropertyNameForSplattingParametersInArgs,
@@ -1869,9 +1869,7 @@ namespace System.Management.Automation
if (normalizedPath.StartsWith(normalizedRoot, StringComparison.OrdinalIgnoreCase))
{
isPathForCurrentDrive = true;
path = path.Substring(normalizedRoot.Length);
path = path.TrimStart(StringLiterals.DefaultPathSeparator);
path = StringLiterals.DefaultPathSeparator + path;
path = string.Concat(StringLiterals.DefaultPathSeparatorString, path.AsSpan(normalizedRoot.Length).TrimStart(StringLiterals.DefaultPathSeparator));
workingDriveForPath = _sessionState.Drive.Current;
}
}
@@ -3087,8 +3085,7 @@ namespace System.Management.Automation
}
else
{
string possibleDriveName = path.Substring(0, index);
if (string.Equals(possibleDriveName, drive.Name, StringComparison.OrdinalIgnoreCase))
if (path.AsSpan(0, index).Equals(drive.Name, StringComparison.OrdinalIgnoreCase))
{
treatAsRelative = false;
}
@@ -4438,17 +4435,17 @@ namespace System.Management.Automation
const char mshEscapeChar = '`';
const char regexEscapeChar = '\\';
char[] workerArray = path.ToCharArray();
ReadOnlySpan<char> workerArray = path;
StringBuilder result = new StringBuilder();
for (int index = 0; index < workerArray.GetLength(0); ++index)
for (int index = 0; index < workerArray.Length; ++index)
{
// look for an escape character
if (workerArray[index] == mshEscapeChar)
{
if (index + 1 < workerArray.GetLength(0))
if (index + 1 < workerArray.Length)
{
if (workerArray[index + 1] == mshEscapeChar)
{
@@ -873,12 +873,12 @@ namespace System.Management.Automation.Provider
// normalize it, then we will get a wrong path.
//
// Fast return if nothing to normalize.
if (path.IndexOf(StringLiterals.AlternatePathSeparator) == -1)
if (!path.Contains(StringLiterals.AlternatePathSeparator))
{
return path;
}
bool pathHasBackSlash = path.IndexOf(StringLiterals.DefaultPathSeparator) != -1;
bool pathHasBackSlash = path.Contains(StringLiterals.DefaultPathSeparator);
string normalizedPath;
// There is a mix of slashes & the path is rooted & the path exists without normalization.
@@ -1723,7 +1723,7 @@ namespace Microsoft.PowerShell.Commands.Internal
internal static string FixupName(string name)
{
BCLDebug.Assert(name != null, "[FixupName]name!=null");
if (name.IndexOf('\\') == -1)
if (name.Contains('\\'))
return name;
StringBuilder sb = new StringBuilder(name);
@@ -218,7 +218,7 @@ namespace System.Management.Automation
if (dirInfo != null)
{
// Relative path of the file is the path inside the containing folder excluding folder Name
relativePath = fileToHash.FullName.Substring(dirInfo.FullName.Length).TrimStart('\\');
relativePath = fileToHash.FullName.AsSpan(dirInfo.FullName.Length).TrimStart('\\').ToString();
}
else
{
@@ -613,7 +613,7 @@ namespace System.Management.Automation
if (dirInfo != null)
{
// Relative path of the file is the path inside the containing folder excluding folder Name
relativePath = fileToHash.FullName.Substring(dirInfo.FullName.Length).TrimStart('\\');
relativePath = fileToHash.FullName.AsSpan(dirInfo.FullName.Length).TrimStart('\\').ToString();
exclude = fileToHash.Name;
}
else
@@ -131,7 +131,7 @@ namespace Microsoft.PowerShell
{
for (int i = 0; i < dataLen; i++)
{
data[i] = byte.Parse(s.Substring(2 * i, 2),
data[i] = byte.Parse(s.AsSpan(2 * i, 2),
NumberStyles.AllowHexSpecifier,
System.Globalization.CultureInfo.InvariantCulture);
}
@@ -204,7 +204,7 @@ namespace System.Management.Automation
// has 'dot' in it, the file will be treated as in Internet security zone. Otherwise, it's
// in Intranet security zone.
string hostName = uri.Host;
return hostName.IndexOf('.') == -1 ? SecurityZone.Intranet : SecurityZone.Internet;
return hostName.Contains('.') ? SecurityZone.Intranet : SecurityZone.Internet;
}
string root = Path.GetPathRoot(filePath);