Skip to content

Compare-Object with [char] instances is unexpectedly case-sensitive by default #17758

Closed

Description

Prerequisites

Steps to reproduce

If [char] instances were consistently compared case-insensitively to each other and to strings, all of the following commands would output 0.

# OK: [string] vs. [string]
(Compare-Object ('a') ('A')).Count
# OK: [string] vs. [char]
(Compare-Object ('a') ([char] 'A')).Count
# !! BROKEN: [char] vs. [char]
(Compare-Object ([char] 'a') ([char] 'A')).Count
# !! BROKEN: [char] vs. [string]
(Compare-Object ([char] 'a') ('A')).Count

Note that [char] 'a' -eq [char] 'A' is $true (even though with -gt, -lt, ... it is the code points that get compared, resulting in case-sensitive behavior).

#12498 is conceptually related, but would not fix this issue, because use of the IEquatable<T> interface would also result in case-sensitive behavior.

Expected behavior

0
0
0
0

Actual behavior

0
0
2
2

Error details

No response

Environment data

PowerShell Core 7.3.0-preview.6

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Jul 24, 2022
  2. 237dmitry commented on Jul 24, 2022

    @237dmitry

    How do chars compare, as chars or as strings? Compare-Object'sCaseSensitive parameter does not affect the result (and this is expected behavior for non strings, IMO). But:

    [char] 'a' -eq [char] 'A'     # True
    [char] 'a' -ceq [char] 'A'    # False
    
  3. mklement0 commented on Jul 24, 2022

    @mklement0
    ContributorAuthor

    237dmitry: They're not technically compared as strings with -eq (if memory serves, .ToUpperInvariant() is called before comparing), but the intent is clearly the same: compare [char]s case-INsensitively.

    Therefore, Compare-Object, which is also built on equality comparison, should behave the same (with -CaseSensitive acting as opt-out).

  4. 237dmitry commented on Jul 24, 2022

    @237dmitry

    Thanks for the clarification.

  5. jhoneill commented on Jul 27, 2022

    @jhoneill

    How do chars compare, as chars or as strings? Compare-Object'sCaseSensitive parameter does not affect the result (and this is expected behavior for non strings, IMO). But:

    [char] 'a' -eq [char] 'A'     # True
    [char] 'a' -ceq [char] 'A'    # False
    

    I think -eq is always -ieq The char type itself only does case sensitive comparisons

    PS>  [char]::Equals(([char]'a'),([char]'A'))
    False
    

    But string comparisons are case sensitive by default with the option to select culture and case sensitivity.

    PS>  [string]::Equals(([char]'a'),([char]'A'))
    False
    
    PS>  [string]::Equals(([char]'a'),([char]'A'), [System.StringComparison]::CurrentCultureIgnoreCase)
    True
    

    IEQ might call ToUpper or ToLower on both strings and a chars and compare the result or it might specify the comparison type for strings. My hunch is Compare-Object is only requesting insensitive for strings and not for chars.
    Because there is only a -CaseSensitive option for Compare-Object the natural expectation is chars and strings both use case insensitive comparisons by default and only case sensitive when the switch is specified.

  6. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. added
    Resolution-No Activity
    on Nov 15, 2023
  8. microsoft-github-policy-service commented on Nov 22, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageResolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions