Replace string.Compare == 0 patterns with Equals/StartsWith/AsSpan calls#124566
Replace string.Compare == 0 patterns with Equals/StartsWith/AsSpan calls#124566
Conversation
Replace all instances of the verbose pattern: (q.Length >= keyword.Length) && (string.Compare(q, 0, keyword, 0, keyword.Length, StringComparison.OrdinalIgnoreCase) == 0) and its != 0 / 0 != variants with the simpler: q.StartsWith(keyword, StringComparison.OrdinalIgnoreCase) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace string.Compare == 0 / != 0 patterns with more idiomatic Equals, StartsWith, and AsSpan alternatives across System.Net libraries. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…sWith/AsSpan in remaining source files Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCasesRunner/AssemblyQualifiedToken.cs
Outdated
Show resolved
Hide resolved
…ls(..., Ordinal) Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/StringUtil.cs
Show resolved
Hide resolved
...es/System.ComponentModel.TypeConverter/src/System/ComponentModel/ComponentResourceManager.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpConnection.Auth.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Globalization/HijriCalendar.Win32.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XNamespace.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Schema/XdrBuilder.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/XPath/Internal/StringFunctions.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Xsl/Runtime/XsltFunctions.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/FileChangeNotificationSystem.cs
Outdated
Show resolved
Hide resolved
|
Tagging subscribers to this area: @dotnet/area-meta |
…dant length checks Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
All comments addressed in 9479aff: null safety fixes in StringUtil and HijriCalendar, simplified ComponentResourceManager/DirectoryServices patterns, replaced the remaining Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
src/libraries/System.Private.CoreLib/src/System/Globalization/IdnMapping.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/FileChangeNotificationSystem.cs
Outdated
Show resolved
Hide resolved
…n FileChangeNotificationSystem.cs Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This pull request systematically replaces verbose string.Compare(...) == 0 / != 0 patterns with more idiomatic and performant alternatives across the codebase. The changes improve code readability and maintainability by using appropriate methods like Equals, StartsWith, EndsWith, and span-based comparisons.
Changes:
- Replace equality checks using
string.Compare(...) == 0withstring.Equals()or direct equality operators - Replace prefix checks using
string.Compare(str, 0, prefix, 0, length) == 0withStartsWith()orAsSpan().StartsWith() - Replace suffix checks with
EndsWith()methods - Simplify substring comparisons using
AsSpan().Equals()for better performance - Add appropriate null checks where needed (e.g., HijriCalendar, SmtpConnection, StringUtil)
Reviewed changes
Copilot reviewed 34 out of 34 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| RuntimeModuleBuilder.Mono.cs | Replace case-insensitive Compare with Equals using InvariantCultureIgnoreCase |
| FileChangeNotificationSystem.cs | Simplify length check and comparison to single Equals call |
| XsltFunctions.cs | Replace substring comparison with StartsWith for prefix checking |
| StringFunctions.cs | Replace substring comparison with StartsWith for prefix checking |
| XdrBuilder.cs | Replace substring comparison with StartsWith, remove redundant length check |
| XNamespace.cs | Use AsSpan().Equals() for substring comparison with special namespace constants |
| XHashtable.cs | Use AsSpan().Equals() for key comparison in hash table lookup |
| EnumDataContract.cs | Use AsSpan().Equals() for enum member name matching |
| SecurityElement.cs | Replace substring comparison with AsSpan().StartsWith() for escape sequence detection |
| IdnMapping.cs | Replace ACE prefix check with AsSpan().StartsWith() (preserves short-circuit behavior) |
| HijriCalendar.Win32.cs | Add null check and use StartsWith for registry key prefix matching |
| Environment.GetFolderPathCore.Unix.cs | Replace substring comparisons with AsSpan().StartsWith() for XDG directory parsing |
| WebProxy.NonWasm.cs | Replace substring comparison with AsSpan().Equals() for domain name matching |
| SmtpConnection.Auth.cs | Add null check and replace multiple substring comparisons with StartsWith |
| HttpListener.Windows.cs | Replace authentication header comparisons with AsSpan().Equals() |
| HttpListenerContext.Managed.cs | Replace Basic auth header check with AsSpan().Equals() |
| HttpListener.cs | Replace URL scheme checks with StartsWith for http:// and https:// |
| HttpWindowsProxy.cs | Replace proxy bypass string checks with AsSpan().StartsWith() |
| AuthenticationHeaderValue.cs | Use AsSpan().Equals() for authentication scheme comparison |
| ManagementQuery.cs | Replace 32+ occurrences of keyword matching with StartsWith calls |
| Utils.cs (DirectoryServices) | Simplify character comparison and use AsSpan().StartsWith() for property name matching |
| EventLog.cs | Replace substring comparison with AsSpan().Equals() for event source validation |
| OdbcUtils.cs | Use AsSpan().StartsWith() for SQL token matching |
| DataTable.cs | Replace suffix comparisons with EndsWith for " ASC" and " DESC" detection |
| UrlPath.cs | Use AsSpan().Equals() for case-insensitive path comparison |
| StringUtil.cs | Add null checks and use StartsWith for both ordinal and ignore-case variants |
| AppSettingsReader.cs | Use AsSpan().Equals() for substring comparison in null string detection |
| ReflectPropertyDescriptor.cs | Replace case-insensitive Compare with Equals for property name matching |
| DesignerOptionService.cs | Replace case-insensitive Compare with Equals for child collection lookup |
| ComponentResourceManager.cs | Simplify if/else logic to single StartsWith with conditional StringComparison |
| CredentialCacheKey.cs | Use AsSpan().Equals() for URI prefix path comparison |
| Program.cs (dotnet-pgo) | Replace file extension comparisons with string.Equals |
| AssemblyQualifiedToken.cs | Use direct string equality operator instead of CompareOrdinal |
| EcmaModule.Sorting.cs | Use StringComparer.Equals in assertion instead of Compare != 0 |
|
An outsider might imagine that AI would be put to better use by improving CA2251, if these cases are not already caught by it |
string.Compare(...) == 0/!= 0patterns withEquals/StartsWith/EndsWith/AsSpanacross source filesStartsWithreplacements💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.