GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Add char range overload to DotDot operator by IISResetMe · Pull Request #5026 · PowerShell/PowerShell Conversation
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What do you think about returning char[] instead?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mathias R. Jessen (@IISResetMe) Could you please address the comment?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I like the idea, but for now I think we should keep operator output consistent (ie. return object[] with individual items of the expected type).
There are are number of builtin operators that return object[] in this fashion (@(), -split, any comparison operator in array/filter mode etc.), changing the behavior should be a concerted effort across all of them IMO
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think this special case is correctly handled by the code below and it happens rarely, so maybe we should remove it. (And I see you copied the code from the int version, so you could remove it there too.)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mainly did it this way to keep it similar to the existing code in Compiler.cs. I'm inclined to make a single Range method that takes the operands as Expression's, de-duplicate the loop that generates the actual sequence and then cast the entire thing to char[], int[] or long[] based on the lhs operand before returning. Will update the code later today
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mathias R. Jessen (@IISResetMe) Is this addresed?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've removed the redundant branch in both overloads now. 0406cb3
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Will this work:
'A'..'Z' | % { ... }
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yes, this will produce the sequence A (0x41) through Z (0x5A)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Apparently it does not work on the Powershell Core 6.0.0 GA
> 'A'..'Z' | % {$_} Cannot convert value "A" to type "System.Int32". Error: "Input string was not in a correct format." At line:1 char:1 + 'A'..'Z' | % {$_} + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : InvalidCastFromStringToInteger
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Kris Borowinski (@kborowinski) there is an open issue for that #5519
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mark Kraus (@markekraus): sorry, I've missed that issue
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I wonder - is this ASCII Arithmetic work correctly for unicode?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It won't work with surrogate pairs because those can't be represented in a char, but I don't think that is an interesting scenario.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Since we added `u{} should we detect surrogate pairs and write an error?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I guess we should error on invalid characters (half of the pair). We could also support surrogate pair ranges if we didn't convert the string to char immediately, instead analyzing the string more thoroughly. But I'm not sure I'd block this PR on either of those.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I guess it will work with traditional languages in common scenarios but before I saw issues from China and Japan mans - if the new operator does not matter to them I agree with you.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mathias R. Jessen (@IISResetMe) Could you please address the comment in code or add relevant comments in the PR description for future documentation?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ilya (@iSazonov) I don't feel confident enough in my unicode-fu to attempt to implement the appropriate handling in code for this at the moment, I've updated the PR description to reflect this deficiency instead.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Oops, rebased powershell:master into the branch, looks like all parent commits are in the PR now, how do I clean it up? 😞 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Mathias R. Jessen (@IISResetMe) did you only have the 2 commits? if so you could do this (assuming git fetch git checkout -b 20171015fix PowerShell/master git cherry-pick 6e6dfffc2d0150b504585597ad4df997a174867a git cherry-pick 6d6f20d28157ee49071f018051ac7b35be57bbb0 git checkout -b 20171015backup overload-DotDot-CharRange-1 git checkout overload-DotDot-CharRange-1 git reset --hard 20171015fix git push --force This will create a new |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
This commit attempts to allow the DotDot operator (..) to generate character ranges. Since PowerShell has no shorthand syntax for char literals, this change tests whether the left-hand-side is of type String, and subsequently attempts to convert both operands to a char. NOTE: This breaks a number of currently supported range operations, such as implicit numerical conversion of strings: '1'..'100'
This commit adds basic tests for the current integer range (..) operator
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
These were the most basic tests I could think of for the existing Range operator. All examples I could find in the about_* help files pass these.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks Mark Kraus (@markekraus), that seems to have done the trick. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Add tests for the [char] overload feature. RangeOperator.Tests.ps1 encoding changed to UTF16LE (Windows "Unicode") to allows inline unicode chars in test case
|
Can you make sure |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Changed RangeOperator.Tests.ps1 encoding to UTF-8 (no BOM), substituted UTF-16 char literals with [char] casts
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| It "Range operator generates arrays of integers" { | ||
| $Range = 5..8 | ||
| $Range.count | Should Be 4 | ||
| $Range[0] | Should BeOfType [int] |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please add tests for values - $Range[0] | Should Be 5
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| It "Range operator accepts negative integer values" { | ||
| { | ||
| -8..-5 | ||
| } |Should Not Throw |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please remove the should - next line do the same and Pester can catch a throw there correctly.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| $Range = -8..-5 | ||
| $Range.count | Should Be 4 | ||
| $Range[0] |Should Be -8 |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Formatting - please add space after |
Please add type check as in previous test.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| It "Range operator support single-item sequences" { | ||
| { | ||
| 0..0 | ||
| } |Should Not Throw |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please remove.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| $Range[0] | Should Be 0 | ||
| } | ||
|
|
||
| It "Range operator works in ascending and descending order" { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Previous tests already test "ascending". I think we could test here only descending order for positive and negative numbers. But I believe we should replace some tests with one - "-2 .. 2" and "2 .. -2" - so we check transition through zero too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| Context "Range operator operand types" { | ||
|
|
||
| It "Range operator works on [int]" { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please remove the test - we do the checks above already.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | Should Not Throw | ||
| $Range = ([long]1)..([long]10) | ||
| $Range.count | Should Be 10 | ||
| $Range.Where({$_ -is [int]}).count | Should Be 10 |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is it really [int]? I think we should use values > int.MaxValue.
And please check values too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Current implementation doesn't support values > int.MaxValue, intended to add this in a separate branch/PR.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarify. Please remove the confuse test.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mathias R. Jessen (@IISResetMe) Please address the comment about the tests for [long] and [bigint].
We should remove its - they don't make sense.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You're right, they don't make too much sense at the moment. I've removed them now
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | Should Not Throw | ||
| $Range = ([bigint]1)..([bigint]10) | ||
| $Range.count | Should Be 10 | ||
| $Range.Where({$_ -is [int]}).count | Should Be 10 |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is it really [int]? I think we should use values > long.MaxValue.
And please check values too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The existing numeric range operator only supports int regardless of input type
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| { | ||
| 1.2d..2.9d | ||
| } | Should Not Throw | ||
| $Range = 1.1d..9.9d |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why so much? I think enough $Range = 1.1d..3.9d
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| $Range[9] | Should Be 10 | ||
| } | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please add new line at EOF.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Fixed formatting, removed unnecessary checks
|
Jason Shirk (@lzybkr) Can you update your review? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ilya (@iSazonov) Can you indicate if you think this is ready or not (feel free to say if someone else signs off)? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Travis Plunk (@TravisEz13) I updated three comment above - after they have been resolved we'll ready to merge. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Both Range() overloads have unnecessary branches for the special case of lhs == rhs, removed for simplicity.
Removed tests for .. using [long] and [bigint] input operands. I believe these will make more sense once we add support for ranges with values outside Int32, but for now might be confusing
|
Ilya (@iSazonov) I believe I've addressed all outstanding comments :-) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Mathias R. Jessen (@IISResetMe) Many thanks! We are near to merge. Please address Jason Shirk (@lzybkr) comment above. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ilya (@iSazonov) Done |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Jason Shirk (@lzybkr) Could you please take another look if you have a bit free time? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.