Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4665

incorrect behavior of array subscript operator with reverse ranges

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.7.7, 1.8-beta-4
    • 2.2.0-rc-1
    • groovy-jdk
    • None
    • any

    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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            jpertino Pertino Juan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: