Skip to content

Comparison operators handle [char] instances inconsistently #18659

Closed

Description

Prerequisites

Steps to reproduce

Fundamentally, as in C#, [char] instances are ultimately compared ordinally, i.e. by their code points rather than by the linguistic rules oft he invariant culture (which is how [string]` instances are compared).

  • However, only the -eq / -ne operator honors case-sensitivity / case-insensitivity, as implied by the operator variant chosen; e.g. -eq (-ieq) for case-insensitivity, -ceq for case-sensitivity.

    • If memory serves, case-insensitivity is honored as follows: .ToUpperInvariant() is called on both operands, and the resulting code points are then tested for equality.
    • As an aside: this can still behave differently from [string] comparisons, because linguistic equivalence isn't considered; e.g. (K is the KELVIN SIGN, U+212A):
      • 'k' -eq 'K' # -> $true, because lowercase ASCII 'k' is considered the case-insensitive equivalent of the Kelvin sign.
      • [char] 'k' -eq [char] 'K' # -> $false, because the code points resulting from .ToUpperVariant() aren't identical.
  • By contrast, -gt / -ge and -lt / -le ignore the requested case-(in)sensitivity, and perform direct code-point comparison.

This is a surprising inconsistency, as the following contradictory results show:

[char] 'a' -eq [char] 'A'  # -> $true; same as: [int] [char]::ToUpperInvariant('a') -eq [int] [char]::ToUpperInvariant('A')

[char] 'a' -le [char] 'A' # !! -> $false, because code points were compared as-is and 97 is greater than 65

Note that fixing this consistency would technically be a breaking change, but hopefully falls into bucket 3.

Expected behavior

True
True

Actual behavior

True
False

Error details

No response

Environment data

PowerShell Core 7.3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Nov 24, 2022
  2. added
    WG-Engine
    WG-Language
    on Nov 27, 2022
  3. added
    PowerShell-Docs needed
    and removed
    Needs-Triage
    on Jul 24, 2023
  4. added
    Resolution-No Activity
    on Jan 21, 2024
  5. microsoft-github-policy-service commented on Jan 21, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

PowerShell-Docs neededResolution-No ActivityWG-EngineWG-Language

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions