Skip to content

Commit

Permalink
Cleanup rest of string.Compare[Ordinal] equality checks and replace i…
Browse files Browse the repository at this point in the history
…t with string.Equals (#9740)

* Clean-up most of string.Compare[Ordinal] and replace it with string.Equals instead

* Remove null check that's not needed
  • Loading branch information
h3xds1nz authored Nov 20, 2024
1 parent 09008c0 commit bbfc24f
Show file tree
Hide file tree
Showing 51 changed files with 140 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,14 @@ private void InitializeTypeMapper()

private bool SwitchStatementSupported()
{
return (IsLanguageCSharp || (CompilerInfo != null && (string.Compare(CompilerInfo.GetLanguages()[0], JSCRIPT, StringComparison.OrdinalIgnoreCase) == 0)));
return IsLanguageCSharp || (CompilerInfo != null && string.Equals(CompilerInfo.GetLanguages()[0], JSCRIPT, StringComparison.OrdinalIgnoreCase));
}

private bool IsInternalAccessSupported
{
get
{
return (CompilerInfo == null || (string.Compare(CompilerInfo.GetLanguages()[0], JSHARP, StringComparison.OrdinalIgnoreCase) != 0));
return CompilerInfo == null || !string.Equals(CompilerInfo.GetLanguages()[0], JSHARP, StringComparison.OrdinalIgnoreCase);
}
}

Expand Down Expand Up @@ -2112,7 +2112,7 @@ private CodeDomProvider EnsureCodeProvider()
private bool IsLanguageSupported(string language)
{
_language = language;
_isLangCSharp = string.Compare(language, CSHARP, StringComparison.OrdinalIgnoreCase) == 0;
_isLangCSharp = string.Equals(language, CSHARP, StringComparison.OrdinalIgnoreCase);

if (IsLanguageCSharp)
{
Expand All @@ -2121,7 +2121,7 @@ private bool IsLanguageSupported(string language)
}
else
{
_isLangVB = string.Compare(language, VB, StringComparison.OrdinalIgnoreCase) == 0;
_isLangVB = string.Equals(language, VB, StringComparison.OrdinalIgnoreCase);
if (IsLanguageVB)
{
_codeProvider = new Microsoft.VisualBasic.VBCodeProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public override bool GetElementType(
{
// This direct comparison is ok to do in pass2 as it has already been validated in pass1.
// This is to avoid a costly instantiation of the CodeDomProvider in pass2.
_isInternalRoot = string.Compare("public", xmlReader.Value.Trim(), StringComparison.OrdinalIgnoreCase) != 0;
_isInternalRoot = !string.Equals("public", xmlReader.Value.Trim(), StringComparison.OrdinalIgnoreCase);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ internal bool DoCompilation(string assemblyName, string language, string rootNam

if (asmReference != null)
{
if (String.Compare(asmReference.AssemblyName, assemblyName, StringComparison.OrdinalIgnoreCase) == 0)
if (string.Equals(asmReference.AssemblyName, assemblyName, StringComparison.OrdinalIgnoreCase))
{
// Set the local assembly file to markupCompiler
_mc.LocalAssemblyFile = asmReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ private bool IsSettingModified(string textSource, string textTarget)
}
else // Both source and target strings are not empty.
{
IsSettingModified = String.Compare(textSource, textTarget, StringComparison.OrdinalIgnoreCase) == 0 ? false : true;
IsSettingModified = !string.Equals(textSource, textTarget, StringComparison.OrdinalIgnoreCase);
}
}

Expand Down Expand Up @@ -426,7 +426,7 @@ private void UpdateFileListForIncrementalBuild(List<FileUnit> modifiedXamlFiles)
{
for (int j = 0; j < numLocalTypeXamls; j++)
{
if (String.Compare(xamlfile.Path, CompilerLocalReference.LocalMarkupPages[j].FilePath, StringComparison.OrdinalIgnoreCase) == 0)
if (string.Equals(xamlfile.Path, CompilerLocalReference.LocalMarkupPages[j].FilePath, StringComparison.OrdinalIgnoreCase))
{
addToList = false;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,25 +329,19 @@ private void Classify(IEnumerable<ITaskItem> inputItems, List<ITaskItem> mainLis
// <returns></returns>
private bool IsItemLocalizable(ITaskItem fileItem)
{
bool isLocalizable = false;

// if the default culture is not set, by default all
// the items are not localizable.
bool isLocalizable = false;

if (Culture != null && Culture.Equals("") == false)
if (!string.IsNullOrEmpty(Culture))
{
string localizableString;
string localizableString = fileItem.GetMetadata(SharedStrings.Localizable);

// Default culture is set, by default the item is localizable
// unless it is set as false in the Localizable attribute.

isLocalizable = true;

localizableString = fileItem.GetMetadata(SharedStrings.Localizable);

if (localizableString != null && String.Compare(localizableString, "false", StringComparison.OrdinalIgnoreCase) ==0 )
// unless it is set as false in the Localizable attribute.
if (!string.Equals(localizableString, "false", StringComparison.OrdinalIgnoreCase))
{
isLocalizable = false;
isLocalizable = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public GenerateTemporaryTargetAssembly()
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public override bool Execute()
{
if (string.Compare(IncludePackageReferencesDuringMarkupCompilation, "false", StringComparison.OrdinalIgnoreCase) != 0)
if (!string.Equals(IncludePackageReferencesDuringMarkupCompilation, "false", StringComparison.OrdinalIgnoreCase))
{
return ExecuteGenerateTemporaryTargetAssemblyWithPackageReferenceSupport();
}
Expand Down Expand Up @@ -638,7 +638,7 @@ private void RemoveEntityByName(XmlDocument xmlProjectDoc, string sItemName, str
{
XmlElement nodeGroup = root.ChildNodes[i] as XmlElement;

if (nodeGroup != null && String.Compare(nodeGroup.Name, groupName, StringComparison.OrdinalIgnoreCase) == 0)
if (nodeGroup != null && string.Equals(nodeGroup.Name, groupName, StringComparison.OrdinalIgnoreCase))
{
//
// This is ItemGroup element.
Expand All @@ -651,7 +651,7 @@ private void RemoveEntityByName(XmlDocument xmlProjectDoc, string sItemName, str
{
XmlElement nodeItem = nodeGroup.ChildNodes[j] as XmlElement;

if (nodeItem != null && String.Compare(nodeItem.Name, sItemName, StringComparison.OrdinalIgnoreCase) == 0)
if (nodeItem != null && string.Equals(nodeItem.Name, sItemName, StringComparison.OrdinalIgnoreCase))
{
// This is the item that need to remove.
// Add it into the temporary array list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ private LocalReferenceFile GenerateLocalTypeItem(string localTypeXamlFile, ITask

string xamlInputFullPath = TaskHelper.CreateFullFilePath(inputXamlItem.ItemSpec, SourceDir);

if (String.Compare(localTypeXamlFile, xamlInputFullPath, StringComparison.OrdinalIgnoreCase) == 0)
if (string.Equals(localTypeXamlFile, xamlInputFullPath, StringComparison.OrdinalIgnoreCase))
{
//
// Got this file from the original XamlFile TaskItem list.
Expand Down Expand Up @@ -1710,27 +1710,19 @@ private TaskItem ProcessLocFileForXamlItem(ITaskItem xamlItem)
//
private bool IsItemLocalizable(ITaskItem ti)
{
bool bIsLocalizable;
// if UICulture is not set, all baml files are not localizable.
// The Localizable metadata value is ignored for this case.
bool bIsLocalizable = false;

if (String.IsNullOrEmpty(UICulture))
{
// if UICulture is not set, all baml files are not localizable.
// The Localizable metadate value is ignored for this case.
bIsLocalizable = false;
}
else
{
string strLocalizable;
if (!string.IsNullOrEmpty(UICulture))
{
string strLocalizable = ti.GetMetadata(SharedStrings.Localizable);

// if UICulture is set, by default all the baml files are localizable unless
// an explicit value "false" is set to Localizable metadata.
bIsLocalizable = true;

strLocalizable = ti.GetMetadata(SharedStrings.Localizable);

if (strLocalizable != null && String.Compare(strLocalizable, "false", StringComparison.OrdinalIgnoreCase) == 0)
if (!string.Equals(strLocalizable, "false", StringComparison.OrdinalIgnoreCase))
{
bIsLocalizable = false;
bIsLocalizable = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ private class LanguageComparerClass : IComparer<LocalizedName>

int IComparer<LocalizedName>.Compare(LocalizedName x, LocalizedName y)
{
return String.Compare(x._language.IetfLanguageTag, y._language.IetfLanguageTag, StringComparison.OrdinalIgnoreCase);
return string.Compare(x._language.IetfLanguageTag, y._language.IetfLanguageTag, StringComparison.OrdinalIgnoreCase);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal static LaunchResult SafeLaunchBrowserOnlyIfPossible(Uri originatingUri,
(Object.ReferenceEquals(destinationUri.Scheme, Uri.UriSchemeHttps)) ||
destinationUri.IsFile;

bool fIsMailTo = String.Compare(destinationUri.Scheme, Uri.UriSchemeMailto, StringComparison.OrdinalIgnoreCase) == 0;
bool fIsMailTo = string.Equals(destinationUri.Scheme, Uri.UriSchemeMailto, StringComparison.OrdinalIgnoreCase);

// We elevate to navigate the browser iff:
// We are user initiated AND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ private Stream EnsureResourceLocationSet()
{
// We do not allow the use of .baml in any Avalon public APIs. This is the code pass needed to go through for loading baml file.
// Throw here we will catch all those cases.
if (String.Compare(Path.GetExtension(_name), ResourceContainer.BamlExt, StringComparison.OrdinalIgnoreCase) == 0)
if (string.Equals(Path.GetExtension(_name), ResourceContainer.BamlExt, StringComparison.OrdinalIgnoreCase))
{
throw new IOException(SR.Format(SR.UnableToLocateResource, _name));
}

if (String.Compare(Path.GetExtension(_name), ResourceContainer.XamlExt, StringComparison.OrdinalIgnoreCase) == 0)
if (string.Equals(Path.GetExtension(_name), ResourceContainer.XamlExt, StringComparison.OrdinalIgnoreCase))
{
// try baml extension first since it's our most common senario.
string newName = Path.ChangeExtension(_name, ResourceContainer.BamlExt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal void _Attach(Object caller, PageFunctionBase child)
// E.g. - if we had a listener to OnFinish from a Button on the calling page.
// "Return event never fired from PageFunction hosted in its own window"
//
if (string.Compare(_returnList[i]._targetTypeName, caller.GetType().AssemblyQualifiedName, StringComparison.Ordinal) != 0)
if (!string.Equals(_returnList[i]._targetTypeName, caller.GetType().AssemblyQualifiedName, StringComparison.Ordinal))
{
throw new NotSupportedException(SR.ReturnEventHandlerMustBeOnParentPage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ internal CustomCategoryAttribute(string name) : base(name)
protected override string GetLocalizedString(string value)
{
// Return a localized version of the custom category
if (String.Compare(value, "Content", StringComparison.Ordinal) == 0)
if (string.Equals(value, "Content", StringComparison.Ordinal))
return SR.DesignerMetadata_CustomCategory_Content;
else if(String.Compare(value, "Accessibility", StringComparison.Ordinal) == 0)
else if (string.Equals(value, "Accessibility", StringComparison.Ordinal))
return SR.DesignerMetadata_CustomCategory_Accessibility;
else /*if(String.Compare(value, "Navigation", StringComparison.Ordinal) == 0)*/
else // if (string.Equals(value, "Navigation", StringComparison.Ordinal))
return SR.DesignerMetadata_CustomCategory_Navigation;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public void BeforeNavigate2(object pDisp, ref object url, ref object flags, ref
// on a hyperlink, Goback/Forward), or programmatically call GoBack and Forward,
// When we get the navigating event, NavigatingToAboutBlank is true, but the source is not "about:blank".
// Clear the NavigatingToAboutBlank bit in that case.
if ((_parent.NavigatingToAboutBlank) &&
String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) != 0)
if ((_parent.NavigatingToAboutBlank) && !string.Equals(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase))
{
_parent.NavigatingToAboutBlank = false;
}
Expand Down Expand Up @@ -181,8 +180,7 @@ public void NavigateComplete2(object pDisp, ref object url)
// If we are loading from stream.
if (_parent.DocumentStream != null)
{
Invariant.Assert(_parent.NavigatingToAboutBlank &&
(String.Compare((string)url, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) == 0));
Invariant.Assert(_parent.NavigatingToAboutBlank && string.Equals((string)url, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase));

try
{
Expand Down Expand Up @@ -210,10 +208,10 @@ public void NavigateComplete2(object pDisp, ref object url)
// internally. Make sure we pass null in the event args.
if (_parent.NavigatingToAboutBlank)
{
Invariant.Assert(String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) == 0);
Invariant.Assert(string.Equals(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase));
urlString = null;
}
Uri source = (String.IsNullOrEmpty(urlString) ? null : new Uri(urlString));
Uri source = string.IsNullOrEmpty(urlString) ? null : new Uri(urlString);
NavigationEventArgs e = new NavigationEventArgs(source, null, null, null, null, true);

_parent.OnNavigated(e);
Expand All @@ -238,10 +236,10 @@ public void DocumentComplete(object pDisp, ref object url)
// internally. Make sure we pass null in the event args.
if (_parent.NavigatingToAboutBlank)
{
Invariant.Assert(String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) == 0);
Invariant.Assert(string.Equals(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase));
urlString = null;
}
Uri source = (String.IsNullOrEmpty(urlString) ? null : new Uri(urlString));
Uri source = string.IsNullOrEmpty(urlString) ? null : new Uri(urlString);
NavigationEventArgs e = new NavigationEventArgs(source, null, null, null, null, true);

_parent.OnLoadCompleted(e);
Expand All @@ -255,8 +253,7 @@ public void DocumentComplete(object pDisp, ref object url)
private bool ShouldIgnoreCompletionEvent(ref object url)
{
string urlString = url as string;
return (_parent.NavigatingToAboutBlank &&
String.Compare(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase) != 0);
return _parent.NavigatingToAboutBlank && !string.Equals(urlString, WebBrowser.AboutBlankUriString, StringComparison.OrdinalIgnoreCase);
}

public void CommandStateChange(long command, bool enable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ internal bool TryGet(string enumName, out int enumIndex)

for (int i = 0; i < _enumNames.Length; i++)
{
if (string.Compare(enumName, _enumNames[i], StringComparison.Ordinal) == 0)
if (string.Equals(enumName, _enumNames[i], StringComparison.Ordinal))
{
enumIndex = i;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private static void GetThemeNameAndColor(out string themeName, out string themeC
themeName = themeNameSB.ToString();
themeName = Path.GetFileNameWithoutExtension(themeName);

if(String.Compare(themeName, "aero", StringComparison.OrdinalIgnoreCase) == 0 && Utilities.IsOSWindows8OrNewer)
if(string.Equals(themeName, "aero", StringComparison.OrdinalIgnoreCase) && Utilities.IsOSWindows8OrNewer)
{
themeName = "Aero2";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,7 @@ private static bool IsComponentBeingLoadedFromOuterLoadBaml(Uri curComponentUri)
Invariant.Assert(fileInBamlConvert != null, "fileInBamlConvert should not be null");
Invariant.Assert(fileCurrent != null, "fileCurrent should not be null");

if (String.Compare(fileInBamlConvert, fileCurrent, StringComparison.OrdinalIgnoreCase) == 0)
if (string.Equals(fileInBamlConvert, fileCurrent, StringComparison.OrdinalIgnoreCase))
{
//
// This is the root element of the xaml page which is being loaded to creat a tree
Expand All @@ -2465,7 +2465,7 @@ private static bool IsComponentBeingLoadedFromOuterLoadBaml(Uri curComponentUri)
if (Math.Abs(diff) == 1)
{
// Check whether the file name is the same.
if (String.Compare(bamlConvertUriSegments[l - 1], curUriSegments[m - 1], StringComparison.OrdinalIgnoreCase) == 0)
if (string.Equals(bamlConvertUriSegments[l - 1], curUriSegments[m - 1], StringComparison.OrdinalIgnoreCase))
{
string component = (diff == 1) ? bamlConvertUriSegments[1] : curUriSegments[1];

Expand Down
Loading

0 comments on commit bbfc24f

Please sign in to comment.