-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add Culture parameter to Select-String cmdlet #10943
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
Changes from all commits
cca96ec
b75230e
2b605d0
af36d60
aa9cc09
36703ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1075,6 +1075,104 @@ void IContextTracker.TrackEOF() | |||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||
| /// Gets or sets a culture name. | ||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||
| [Parameter] | ||||||||||||||||||||
| [ValidateSet(typeof(ValidateMatchStringCultureNamesGenerator))] | ||||||||||||||||||||
| [ValidateNotNull] | ||||||||||||||||||||
| public string Culture | ||||||||||||||||||||
| { | ||||||||||||||||||||
| get | ||||||||||||||||||||
| { | ||||||||||||||||||||
| switch (_stringComparison) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| case StringComparison.Ordinal: | ||||||||||||||||||||
| case StringComparison.OrdinalIgnoreCase: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| return OrdinalCultureName; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| case StringComparison.InvariantCulture: | ||||||||||||||||||||
| case StringComparison.InvariantCultureIgnoreCase: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| return InvariantCultureName; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| case StringComparison.CurrentCulture: | ||||||||||||||||||||
| case StringComparison.CurrentCultureIgnoreCase: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| return CurrentCultureName; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| default: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return _cultureName; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| set | ||||||||||||||||||||
| { | ||||||||||||||||||||
| _cultureName = value; | ||||||||||||||||||||
| InitCulture(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| internal const string OrdinalCultureName = "Ordinal"; | ||||||||||||||||||||
| internal const string InvariantCultureName = "Invariant"; | ||||||||||||||||||||
| internal const string CurrentCultureName = "Current"; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private string _cultureName = CultureInfo.CurrentCulture.Name; | ||||||||||||||||||||
| private StringComparison _stringComparison = StringComparison.CurrentCultureIgnoreCase; | ||||||||||||||||||||
| private CompareOptions _compareOptions = CompareOptions.IgnoreCase; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private delegate int CultureInfoIndexOf(string source, string value, int startIndex, int count, CompareOptions options); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private CultureInfoIndexOf _cultureInfoIndexOf = CultureInfo.CurrentCulture.CompareInfo.IndexOf; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private void InitCulture() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| _stringComparison = default; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| switch (_cultureName) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| case OrdinalCultureName: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| _stringComparison = CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; | ||||||||||||||||||||
| _compareOptions = CaseSensitive ? CompareOptions.Ordinal : CompareOptions.OrdinalIgnoreCase; | ||||||||||||||||||||
| _cultureInfoIndexOf = CultureInfo.InvariantCulture.CompareInfo.IndexOf; | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| case InvariantCultureName: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| _stringComparison = CaseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase; | ||||||||||||||||||||
| _compareOptions = CaseSensitive ? CompareOptions.None : CompareOptions.IgnoreCase; | ||||||||||||||||||||
| _cultureInfoIndexOf = CultureInfo.InvariantCulture.CompareInfo.IndexOf; | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| case CurrentCultureName: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| _stringComparison = CaseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; | ||||||||||||||||||||
| _compareOptions = CaseSensitive ? CompareOptions.None : CompareOptions.IgnoreCase; | ||||||||||||||||||||
| _cultureInfoIndexOf = CultureInfo.CurrentCulture.CompareInfo.IndexOf; | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| default: | ||||||||||||||||||||
| { | ||||||||||||||||||||
| var _cultureInfo = CultureInfo.GetCultureInfo(_cultureName); | ||||||||||||||||||||
| _compareOptions = CaseSensitive ? CompareOptions.None : CompareOptions.IgnoreCase; | ||||||||||||||||||||
| _cultureInfoIndexOf = _cultureInfo.CompareInfo.IndexOf; | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||
| /// Gets or sets the current pipeline object. | ||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||
|
|
@@ -1322,6 +1420,15 @@ private IContextTracker GetContextTracker() => (Raw || (_preContext == 0 && _pos | |||||||||||||||||||
| /// </summary> | ||||||||||||||||||||
| protected override void BeginProcessing() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| if (this.MyInvocation.BoundParameters.ContainsKey(nameof(Culture)) && !this.MyInvocation.BoundParameters.ContainsKey(nameof(SimpleMatch))) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| InvalidOperationException exception = new InvalidOperationException(MatchStringStrings.CannotSpecifyCultureWithoutSimpleMatch); | ||||||||||||||||||||
| ErrorRecord errorRecord = new ErrorRecord(exception, "CannotSpecifyCultureWithoutSimpleMatch", ErrorCategory.InvalidData, null); | ||||||||||||||||||||
| this.ThrowTerminatingError(errorRecord); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| InitCulture(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| string suppressVt = Environment.GetEnvironmentVariable("__SuppressAnsiEscapeSequences"); | ||||||||||||||||||||
| if (!string.IsNullOrEmpty(suppressVt)) | ||||||||||||||||||||
| { | ||||||||||||||||||||
|
|
@@ -1728,13 +1835,11 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI | |||||||||||||||||||
| } | ||||||||||||||||||||
| else | ||||||||||||||||||||
| { | ||||||||||||||||||||
| StringComparison compareOption = CaseSensitive ? | ||||||||||||||||||||
| StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; | ||||||||||||||||||||
| while (patternIndex < Pattern.Length) | ||||||||||||||||||||
| { | ||||||||||||||||||||
|
Contributor
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. This old 'StringComparison compareOption = ...' code can be removed.
Collaborator
Author
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. Done. |
||||||||||||||||||||
| string pat = Pattern[patternIndex]; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| int index = operandString.IndexOf(pat, compareOption); | ||||||||||||||||||||
| int index = _cultureInfoIndexOf(operandString, pat, 0, operandString.Length, _compareOptions); | ||||||||||||||||||||
| if (index >= 0) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| if (shouldEmphasize) | ||||||||||||||||||||
|
|
@@ -1972,4 +2077,25 @@ private bool MeetsIncludeExcludeCriteria(string filename) | |||||||||||||||||||
| return ok; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||
| /// Get list of valid culture names for ValidateSet attribute. | ||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||
| public class ValidateMatchStringCultureNamesGenerator : IValidateSetValuesGenerator | ||||||||||||||||||||
|
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. Can this be internal?
Collaborator
Author
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. It must be public: PowerShell/src/System.Management.Automation/engine/Attributes.cs Lines 1715 to 1723 in fe40fbf
|
||||||||||||||||||||
| { | ||||||||||||||||||||
| string[] IValidateSetValuesGenerator.GetValidValues() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); | ||||||||||||||||||||
| var result = new List<string>(cultures.Length + 3); | ||||||||||||||||||||
| result.Add(SelectStringCommand.OrdinalCultureName); | ||||||||||||||||||||
| result.Add(SelectStringCommand.InvariantCultureName); | ||||||||||||||||||||
| result.Add(SelectStringCommand.CurrentCultureName); | ||||||||||||||||||||
| foreach (var cultureInfo in cultures) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| result.Add(cultureInfo.Name); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return result.ToArray(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.