-
Notifications
You must be signed in to change notification settings - Fork 5.3k
add OSPlatform.macOS, make OSPlatform.Equal use OrdinalIgnoreCaseComparison #39064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5909ffe
introduce new OSPlatforms
adamsitnik d586408
use OrdinalIgnoreCase instead of Ordinal for os platform name compari…
adamsitnik 76783a6
hide OSX, make sure OSX == macOS
adamsitnik 2d2ed3e
add more tests
adamsitnik f388f03
Merge remote-tracking branch 'upstream/master' into 33331newOsPlatforms
adamsitnik 7a8f548
simplify the code based on ideas from code review
adamsitnik 8a3277e
add more test cases
adamsitnik d3e751d
add suggested test cases
adamsitnik 8ae72cf
address code review feedback
adamsitnik f3c439a
fix a bug that I've introduced
adamsitnik 84d959a
use StringComparison.OrdinalIgnoreCase in GetHashCode as well
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.ComponentModel; | ||
|
|
||
| namespace System.Runtime.InteropServices | ||
| { | ||
| public readonly struct OSPlatform : IEquatable<OSPlatform> | ||
|
|
@@ -15,8 +17,11 @@ namespace System.Runtime.InteropServices | |
|
|
||
| public static OSPlatform Linux { get; } = new OSPlatform("LINUX"); | ||
|
|
||
| [EditorBrowsable(EditorBrowsableState.Never)] // https://github.com/dotnet/runtime/issues/33331#issuecomment-650326500 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this link really useful for context? It points to several screens of API review notes, and it does not say much useful about the OSX. Should this rather say "// superseded by macOS" ? |
||
| public static OSPlatform OSX { get; } = new OSPlatform("OSX"); | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static OSPlatform macOS { get; } = new OSPlatform(nameof(macOS)); | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static OSPlatform iOS { get; } = new OSPlatform("IOS"); | ||
|
|
||
| public static OSPlatform tvOS { get; } = new OSPlatform("TVOS"); | ||
|
|
@@ -40,17 +45,12 @@ public static OSPlatform Create(string osPlatform) | |
|
|
||
| public bool Equals(OSPlatform other) | ||
| { | ||
| return Equals(other._osPlatform); | ||
| } | ||
|
|
||
| internal bool Equals(string? other) | ||
| { | ||
| return string.Equals(_osPlatform, other, StringComparison.Ordinal); | ||
| return string.Equals(_osPlatform, other._osPlatform, StringComparison.OrdinalIgnoreCase) || AreOSXAndMacOS(_osPlatform, other._osPlatform); | ||
| } | ||
|
|
||
| public override bool Equals(object? obj) | ||
| { | ||
| return obj is OSPlatform && Equals((OSPlatform)obj); | ||
| return obj is OSPlatform osPlatform && Equals(osPlatform); | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public override int GetHashCode() | ||
|
|
@@ -72,5 +72,31 @@ public override string ToString() | |
| { | ||
| return !(left == right); | ||
| } | ||
|
|
||
| // this ugly method exists to not break backward compatibility | ||
| // for cases where users use the old `OSX` property on macOS | ||
| private bool AreOSXAndMacOS(string left, string right) | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (left == null || right == null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| const string OSX = "OSX"; | ||
| const string macOS = "macOS"; | ||
|
|
||
| if (left.Length == OSX.Length && right.Length == macOS.Length) | ||
| { | ||
| return string.Equals(left, OSX, StringComparison.OrdinalIgnoreCase) | ||
| && string.Equals(right, macOS, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| else if (left.Length == macOS.Length && right.Length == OSX.Length) | ||
| { | ||
| return string.Equals(left, macOS, StringComparison.OrdinalIgnoreCase) | ||
| && string.Equals(right, OSX, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.