Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/System.Management.Automation/engine/regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ StringComparison GetStringComparison()
{
stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant)
? StringComparison.InvariantCultureIgnoreCase
: StringComparison.CurrentCultureIgnoreCase;
: CultureInfo.CurrentCulture.Name.Equals("en-US-POSIX", StringComparison.OrdinalIgnoreCase)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test case?

We have one for.. hashtable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Describe "Hashtable is case insensitive" -Tag CI {

Copy link
Member Author

@daxian-dbw daxian-dbw Jul 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added. Two things to be noted:

  1. Setting the current culture to be 'en-us-POSIX' doesn't do anything on Windows -- [string]::Equals("a", "A", [System.StringComparison]::CurrentCultureIgnoreCase) always returns true.
  2. It seems the existing way of changing culture works fine. The following script writes out false as expected.
try {
      $oldCulture = [System.Globalization.CultureInfo]::CurrentCulture
      [System.Globalization.CultureInfo]::CurrentCulture = [System.Globalization.CultureInfo]::new('en-US-POSIX')
      [string]::Equals("a", "A", [System.StringComparison]::CurrentCultureIgnoreCase)
}
finally {
     [System.Globalization.CultureInfo]::CurrentCulture = $oldCulture
}

// The collation behavior of the POSIX locale (also known as the C locale) is case sensitive.
// For this specific locale, we use 'OrdinalIgnoreCase'.
? StringComparison.OrdinalIgnoreCase
: StringComparison.CurrentCultureIgnoreCase;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ Describe "Hash expression with if statement as value" -Tags "CI" {
}
}

Describe "Hashtable is case insensitive" -Tag CI {
It "When current culture is en-US-POSIX" -Skip:($IsWindows) {
Describe "Support case-insensitive comparison in Posix locale" -Tag CI {
It "Hashtable is case insensitive" -Skip:($IsWindows) {
try {
$oldCulture = [System.Globalization.CultureInfo]::CurrentCulture
[System.Globalization.CultureInfo]::CurrentCulture = [System.Globalization.CultureInfo]::new('en-US-POSIX')
Expand All @@ -316,4 +316,20 @@ Describe "Hashtable is case insensitive" -Tag CI {
[System.Globalization.CultureInfo]::CurrentCulture = $oldCulture
}
}

It "Wildcard support case-insensitive matching" -Skip:($IsWindows) {
try {
$oldCulture = [System.Globalization.CultureInfo]::CurrentCulture
[System.Globalization.CultureInfo]::CurrentCulture = [System.Globalization.CultureInfo]::new('en-US-POSIX')

$wildcard1 = [WildcardPattern]::new("AbC*", [System.Management.Automation.WildcardOptions]::IgnoreCase)
$wildcard1.IsMatch("abcd") | Should -BeTrue

$wildcard2 = [WildcardPattern]::new("DeF", [System.Management.Automation.WildcardOptions]::IgnoreCase);
$wildcard2.IsMatch("def") | Should -BeTrue
}
finally {
[System.Globalization.CultureInfo]::CurrentCulture = $oldCulture
}
}
}