groovy
  1. groovy
  2. GROOVY-4665

incorrect behavior of array subscript operator with reverse ranges

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.7.7, 1.8-beta-4
    • Fix Version/s: 2.2.0-rc-1
    • Component/s: groovy-jdk
    • Labels:
      None
    • Environment:
      any
    • Number of attachments :
      0

      Description

      reverse ranges like X..-Y are used in lists to represent forward ranges of the form X..list.size()-Y.
      this is not the case with arrays, as these special cases return a list that match the reverse range.

      this can easily be tested with this snippet

      def list = [1,2,3,4]
      int[] array = list
      assert list[2..-1] == [3,4]
      assert array[2..-1] == [3,4]
      

      which currently yields

      Assertion failed: 
      
      assert array[2..-1] == [3,4]
             |    |       |
             |    |       false
             |    [3, 2, 1, 4]
             [1, 2, 3, 4]
      

      we can track this to the protected DGM.primitiveArrayGet(Object,Range) that is called from DGM.getAt(int[],Range) and its siblings.

      this method just iterates the range to generate the returned list, whereas the list equivalent DGM.getAt(List,Range) makes use of DGMS.subListBorders(int,Range) to detect the reverse ranges.

        Activity

          People

          • Assignee:
            Unassigned
            Reporter:
            Pertino Juan
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: