-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Support negative numbers in -split operator #8960
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
690f7d3
ce4b48f
ae5a1e5
bbfbfcc
8ccb6c9
701b22f
def8036
73b28fd
02c04d9
ae6119c
a52711c
3b5ccbd
c11938a
8b7c843
c96c58e
21a7c66
e55b277
27c3203
261bd56
c06eed9
7089395
d71453f
c951ab6
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 |
|---|---|---|
|
|
@@ -33,12 +33,78 @@ Describe "Split Operator" -Tags CI { | |
| $res[2] | Should -Be "c" | ||
| $res[3] | Should -Be "d" | ||
|
|
||
| $res = "a b c d" -split " ", -2 | ||
ece-jacob-scott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $res.count | Should -Be 2 | ||
| $res[0] | Should -Be "a b c" | ||
| $res[1] | Should -Be "d" | ||
|
|
||
| $res = "a b c d" -split " ", -1 | ||
| $res.count | Should -Be 1 | ||
| $res[0] | Should -Be "a b c d" | ||
| } | ||
|
|
||
| It "Binary split operator can work with different delimeter than split string" { | ||
| $res = "a b c d" -split " ",8 | ||
| $res.count | Should -Be 4 | ||
| $res[0] | Should -Be "a" | ||
| $res[1] | Should -Be "b" | ||
| $res[2] | Should -Be "c" | ||
| $res[3] | Should -Be "d" | ||
|
|
||
| $res = "a b c d" -split " ",-8 | ||
|
Collaborator
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. Why not reverse order like test in line 80?
Contributor
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. Agreed this is something that I noticed today while showing off my changes to the judges. I will probably come back and add another commit fixing this functionality. Which way do you think is better? Personally I think that the first item should be at array index 0.
Contributor
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. Any formatting or implementation details would be greatly appreciated. Thank you! :)
Collaborator
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. Note that splitting from right to left might be considered orthogonal to the iteration from left to right. @mklement0's original example in #4765 is: This implementation currently accords with that.
Collaborator
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. Also worth noting that suggestion was approved by the @PowerShell/powershell-committee
Collaborator
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. Test in line 80 is wrong, yes?
Contributor
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. |
||
| $res.count | Should -Be 4 | ||
| $res[0] | Should -Be "a" | ||
| $res[1] | Should -Be "b" | ||
| $res[2] | Should -Be "c" | ||
| $res[3] | Should -Be "d" | ||
|
|
||
| $res = " " -split " ",-2 | ||
| $res.count | Should -Be 2 | ||
| $res[0] | Should -Be "" | ||
| $res[1] | Should -Be "" | ||
| } | ||
|
|
||
| It "Binary split operator with predicate can work with negative numbers" { | ||
| $res = "a b c d" -split {$_ -like ' '},-2 | ||
| $res.count | Should -Be 2 | ||
| $res[0] | Should -Be "a b c" | ||
| $res[1] | Should -Be "d" | ||
|
|
||
| $res = "a b c d" -split {$_ -like ' '},-4 | ||
| $res.count | Should -Be 4 | ||
| $res[0] | Should -Be "a" | ||
| $res[1] | Should -Be "b" | ||
| $res[2] | Should -Be "c" | ||
| $res[3] | Should -Be "d" | ||
|
|
||
| $res = "a b c d" -split {$_ -like ' '},-8 | ||
| $res.count | Should -Be 4 | ||
| $res[0] | Should -Be "a" | ||
| $res[1] | Should -Be "b" | ||
| $res[2] | Should -Be "c" | ||
| $res[3] | Should -Be "d" | ||
|
|
||
| $res = " " -split {$_ -like ' '},-4 | ||
| $res.count | Should -Be 2 | ||
| $res[0] | Should -Be "" | ||
| $res[1] | Should -Be "" | ||
|
|
||
| $res = "folder/path/to/file" -split {$_ -like '/'}, -2 | ||
| $res.count | Should -Be 2 | ||
| $res[0] | Should -Be "folder/path/to" | ||
| $res[1] | Should -Be "file" | ||
| } | ||
|
|
||
| It "Binary split operator can work with regex expression" { | ||
| $res = "a2b3c4d" -split '\d+',2 | ||
| $res.count | Should -Be 2 | ||
| $res[0] | Should -Be "a" | ||
| $res[1] | Should -Be "b3c4d" | ||
|
|
||
| $res = "a2b3c4d" -split '\d+',-2 | ||
| $res.count | Should -Be 2 | ||
| $res[0] | Should -Be "a2b3c" | ||
| $res[1] | Should -Be "d" | ||
| } | ||
|
|
||
| It "Binary split operator can works with freeform delimiter" { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.