Code cleanup: use IndexOf(char) overload (#9722)

- Use IndexOf(char) overload instead of IndexOf(string) to benefit from Ordinal search
- Replace IndexOf with Contains to get more understandable code
- Use StringComparison.Ordinal for ASCII chars
This commit is contained in:
Ilya 2019-06-26 11:02:07 +05:00 committed by GitHub
parent a5e27debc1
commit dd7e45fe2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 35 additions and 35 deletions

View File

@ -1808,7 +1808,7 @@ $result
}
else
{
int dotIndex = computerName.IndexOf(".", StringComparison.OrdinalIgnoreCase);
int dotIndex = computerName.IndexOf('.');
if (dotIndex == -1)
{
localUserName = computerName + "\\" + psLocalCredential.UserName;

View File

@ -227,7 +227,7 @@ $result
{
string canonicalName = (string)item.ExtendedProperty("System.ApplicationName");
canonicalName = !string.IsNullOrEmpty(canonicalName)
? canonicalName.Substring(0, canonicalName.IndexOf("\0", StringComparison.OrdinalIgnoreCase))
? canonicalName.Substring(0, canonicalName.IndexOf('\0'))
: null;
if (canonicalName != null && canonicalName.Equals(RegionCanonicalName, StringComparison.OrdinalIgnoreCase))
@ -452,7 +452,7 @@ $result
string path = item.Path;
string canonicalName = (string)item.ExtendedProperty("System.ApplicationName");
canonicalName = canonicalName != null
? canonicalName.Substring(0, canonicalName.IndexOf("\0", StringComparison.OrdinalIgnoreCase))
? canonicalName.Substring(0, canonicalName.IndexOf('\0'))
: null;
if (canonicalName == null)
@ -640,7 +640,7 @@ $result
string description = (string)item.ExtendedProperty("InfoTip");
string canonicalName = (string)item.ExtendedProperty("System.ApplicationName");
canonicalName = canonicalName != null
? canonicalName.Substring(0, canonicalName.IndexOf("\0", StringComparison.OrdinalIgnoreCase))
? canonicalName.Substring(0, canonicalName.IndexOf('\0'))
: null;
int[] categories = (int[])item.ExtendedProperty("System.ControlPanel.Category");
string[] cateStrings = new string[categories.Length];

View File

@ -300,7 +300,7 @@ namespace Microsoft.PowerShell.Commands
continue;
case qualifierSet:
int separatorIndex = pathsToParse[index].IndexOf(":", StringComparison.CurrentCulture);
int separatorIndex = pathsToParse[index].IndexOf(':');
if (separatorIndex < 0)
{
@ -449,7 +449,7 @@ namespace Microsoft.PowerShell.Commands
if (SessionState.Path.IsProviderQualified(path))
{
int index = path.IndexOf("::", StringComparison.CurrentCulture);
int index = path.IndexOf("::", StringComparison.Ordinal);
if (index != -1)
{

View File

@ -6600,7 +6600,7 @@ namespace System.Management.Automation
}
string tooltip = memberInfo.ToString();
if (tooltip.IndexOf("),", StringComparison.OrdinalIgnoreCase) != -1)
if (tooltip.IndexOf("),", StringComparison.Ordinal) != -1)
{
var overloads = tooltip.Split(new[] { ")," }, StringSplitOptions.RemoveEmptyEntries);
var newTooltip = new StringBuilder();

View File

@ -1505,7 +1505,7 @@ namespace System.Management.Automation
private static object NewConverterInstance(string assemblyQualifiedTypeName)
{
int typeSeparator = assemblyQualifiedTypeName.IndexOf(",", StringComparison.Ordinal);
int typeSeparator = assemblyQualifiedTypeName.IndexOf(',');
if (typeSeparator == -1)
{
typeConversion.WriteLine("Type name \"{0}\" should be assembly qualified.", assemblyQualifiedTypeName);

View File

@ -4680,7 +4680,7 @@ namespace Microsoft.PowerShell.Commands
filePath.StartsWith(@"../", StringComparison.Ordinal) ||
filePath.StartsWith(@"~/", StringComparison.Ordinal) ||
filePath.StartsWith(@"~\", StringComparison.Ordinal) ||
filePath.IndexOf(":", StringComparison.Ordinal) >= 0);
filePath.Contains(':'));
}
/// <summary>

View File

@ -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.OrdinalIgnoreCase) == -1))
if (type.IsArray && (parameterTypeString.IndexOf("[]", StringComparison.Ordinal) == -1))
{
var t = type;
while (t.IsArray)

View File

@ -8349,7 +8349,7 @@ namespace System.Management.Automation.Language
{
if (generic == null || !generic.ContainsGenericParameters)
{
if (TypeName.FullName.IndexOf("`", StringComparison.OrdinalIgnoreCase) == -1)
if (!TypeName.FullName.Contains('`'))
{
var newTypeName = new TypeName(Extent,
string.Format(CultureInfo.InvariantCulture, "{0}`{1}", TypeName.FullName, GenericArguments.Count));
@ -8376,7 +8376,7 @@ namespace System.Management.Automation.Language
Type generic = TypeName.GetReflectionAttributeType();
if (generic == null || !generic.ContainsGenericParameters)
{
if (TypeName.FullName.IndexOf("`", StringComparison.OrdinalIgnoreCase) == -1)
if (!TypeName.FullName.Contains('`'))
{
var newTypeName = new TypeName(Extent,
string.Format(CultureInfo.InvariantCulture, "{0}Attribute`{1}", TypeName.FullName, GenericArguments.Count));

View File

@ -5436,7 +5436,7 @@ Disable-PSRemoting -force:$args[0] -queryForSet $args[1] -captionForSet $args[2]
if (!string.IsNullOrEmpty(this.Username))
{
if (this.Username.IndexOf("\\", StringComparison.OrdinalIgnoreCase) >= 0)
if (this.Username.Contains('\\'))
{
validator = null;

View File

@ -1285,8 +1285,8 @@ namespace System.Management.Automation.Runspaces
(RemotingErrorIdStrings.RelativeUriForRunspacePathNotSupported));
}
if (uri.OriginalString.LastIndexOf(":", StringComparison.OrdinalIgnoreCase) >
uri.AbsoluteUri.IndexOf("//", StringComparison.OrdinalIgnoreCase))
if (uri.OriginalString.LastIndexOf(':') >
uri.AbsoluteUri.IndexOf("//", StringComparison.Ordinal))
{
UseDefaultWSManPort = false;
}
@ -1296,7 +1296,7 @@ namespace System.Management.Automation.Runspaces
// http://localhost , http://127.0.0.1 etc.
string appname;
if (uri.AbsolutePath.Equals("/", StringComparison.OrdinalIgnoreCase) &&
if (uri.AbsolutePath.Equals("/", StringComparison.Ordinal) &&
string.IsNullOrEmpty(uri.Query) && string.IsNullOrEmpty(uri.Fragment))
{
appname = s_defaultAppName;

View File

@ -192,7 +192,7 @@ namespace Microsoft.PowerShell.Commands
{
// Deal with wildcards
int index = path.IndexOf("*", StringComparison.OrdinalIgnoreCase);
int index = path.IndexOf('*');
if (index == 0)
{

View File

@ -5337,13 +5337,13 @@ namespace Microsoft.PowerShell.Commands
{
string testPath = path.Replace('/', '\\');
if (
(testPath.IndexOf("\\", StringComparison.OrdinalIgnoreCase) < 0) ||
testPath.StartsWith(".\\", StringComparison.OrdinalIgnoreCase) ||
testPath.StartsWith("..\\", StringComparison.OrdinalIgnoreCase) ||
testPath.EndsWith("\\.", StringComparison.OrdinalIgnoreCase) ||
testPath.EndsWith("\\..", StringComparison.OrdinalIgnoreCase) ||
(testPath.IndexOf("\\.\\", StringComparison.OrdinalIgnoreCase) > 0) ||
(testPath.IndexOf("\\..\\", StringComparison.OrdinalIgnoreCase) > 0))
!testPath.Contains('\\') ||
testPath.StartsWith(".\\", StringComparison.Ordinal) ||
testPath.StartsWith("..\\", StringComparison.Ordinal) ||
testPath.EndsWith("\\.", StringComparison.Ordinal) ||
testPath.EndsWith("\\..", StringComparison.Ordinal) ||
testPath.Contains("\\.\\", StringComparison.Ordinal) ||
testPath.Contains("\\..\\", StringComparison.Ordinal))
{
try
{

View File

@ -1506,7 +1506,7 @@ namespace System.Management.Automation
break;
}
int index = path.IndexOf(":", StringComparison.Ordinal);
int index = path.IndexOf(':');
if (index == -1)
{
@ -1604,7 +1604,7 @@ namespace System.Management.Automation
break;
}
int index = path.IndexOf(":", StringComparison.Ordinal);
int index = path.IndexOf(':');
if (index == -1)
{
@ -1863,7 +1863,7 @@ namespace System.Management.Automation
string normalizedRoot = _sessionState.Drive.Current.Root.Replace(
StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator);
if (normalizedRoot.IndexOf(":", StringComparison.Ordinal) >= 0)
if (normalizedRoot.Contains(':'))
{
string normalizedPath = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator);
if (normalizedPath.StartsWith(normalizedRoot, StringComparison.OrdinalIgnoreCase))
@ -3160,7 +3160,7 @@ namespace System.Management.Automation
// Find the drive separator only if it's before a path separator
int index = path.IndexOf(":", StringComparison.Ordinal);
int index = path.IndexOf(':');
if (index != -1)
{
int separator = path.IndexOf(StringLiterals.DefaultPathSeparator, 0, index);

View File

@ -2958,7 +2958,7 @@ namespace Microsoft.PowerShell.Commands
private bool HasRelativePathTokens(string path)
{
return (
(path.IndexOf("\\", StringComparison.OrdinalIgnoreCase) == 0) ||
path.StartsWith('\\') ||
path.Contains("\\.\\") ||
path.Contains("\\..\\") ||
path.EndsWith("\\..", StringComparison.OrdinalIgnoreCase) ||
@ -3332,12 +3332,12 @@ namespace Microsoft.PowerShell.Commands
path = NormalizePath(path);
int index = path.IndexOf("\\", StringComparison.Ordinal);
int index = path.IndexOf('\\');
if (index == 0)
{
// The user may precede a path with \
path = path.Substring(1);
index = path.IndexOf("\\", StringComparison.Ordinal);
index = path.IndexOf('\\');
}
if (index == -1)
@ -3522,14 +3522,14 @@ namespace Microsoft.PowerShell.Commands
do // false loop
{
int index = path.IndexOf("\\", StringComparison.Ordinal);
int index = path.IndexOf('\\');
if (index == 0)
{
// The user may proceed a path with \
path = path.Substring(1);
index = path.IndexOf("\\", StringComparison.Ordinal);
index = path.IndexOf('\\');
}
if (index == -1)

View File

@ -2037,7 +2037,7 @@ namespace Microsoft.PowerShell.Commands.Internal
throw new ArgumentNullException(RegistryProviderStrings.Arg_Name);
}
int nextSlash = name.IndexOf("\\", StringComparison.OrdinalIgnoreCase);
int nextSlash = name.IndexOf('\\');
int current = 0;
while (nextSlash != -1)
{
@ -2045,7 +2045,7 @@ namespace Microsoft.PowerShell.Commands.Internal
throw new ArgumentException(RegistryProviderStrings.Arg_RegKeyStrLenBug);
current = nextSlash + 1;
nextSlash = name.IndexOf("\\", current, StringComparison.OrdinalIgnoreCase);
nextSlash = name.IndexOf('\\', current);
}
if ((name.Length - current) > MaxKeyLength)