on master help sign in
Resolve issue with getting negative indices

Negative indices produced <internal value> instead of an error, and the safer versions of get weren’t safe for negative values.

commit3bf97126c1ddffab0adb8553a5d04666b2a9d4e5 authorEvan Czaplicki <info@elm-lang.org> dateThu 3 Apr 2014, 00:30 (13 years ago) parentea54e58
2 files changed+23−19
libraries/Array.elm+5−4 libraries/Native/Array.js+18−15
libraries/Array.elm+5−4
@@ -123,12 +123,13 @@ get = Native.Array.get
123123
124124{-| Return Just the element at the index or Nothing if the index is out of range.
125125
126− getMaybe 2 (fromList [3,2,1]) == Just 2
127− getMaybe 5 (fromList [3,2,1]) == Nothing
126+ getMaybe 2 (fromList [3,2,1]) == Just 2
127+ getMaybe 5 (fromList [3,2,1]) == Nothing
128+ getMaybe -1 (fromList [3,2,1]) == Nothing
128129-}
129130getMaybe : Int -> Array a -> Maybe a
130131getMaybe i array =
131− if Native.Array.length array > i
132+ if 0 <= i && i < Native.Array.length array
132133 then Just (Native.Array.get i array)
133134 else Nothing
134135
@@ -140,7 +141,7 @@ element is returned.
140141-}
141142getWithDefault : a -> Int -> Array a -> a
142143getWithDefault default i array =
143− if Native.Array.length array > i
144+ if 0 <= i && i < Native.Array.length array
144145 then Native.Array.get i array
145146 else default
146147
libraries/Native/Array.js+18−15
@@ -25,18 +25,21 @@ Elm.Native.Array.make = function(elm) {
2525 var empty = { ctor:"_Array", height:0, table:new Array() };
2626
2727 // Gets the value at index i recursively.
28− function get(i, a) {
29− if (a.height == 0) {
30− if (i < a.table.length) {
31− return a.table[i];
32− } else {
33− throw new Error("Index "+ i +" is out of range. Check the length of your array first or use getSafe or getMaybe.");
28+ function get(i, array) {
29+ if (i < 0 || i >= length(array)) {
30+ throw new Error("Index " + i + " is out of range. Check the length of " +
31+ "your array first or use getMaybe or getWithDefault.");
3432 }
35− }
33+ return unsafeGet(i, array);
34+ }
3635
37− var slot = getSlot(i, a);
38− var sub = slot > 0 ? a.lengths[slot-1] : 0;
39− return get(i - sub, a.table[slot]);
36+ function unsafeGet(i, array) {
37+ if (array.height == 0) {
38+ return array.table[i];
39+ }
40+ var slot = getSlot(i, array);
41+ var offset = slot > 0 ? array.lengths[slot-1] : 0;
42+ return unsafeGet(i - offset, array.table[slot]);
4043 }
4144
4245 // Sets the value at the index i. Only the nodes leading to i will get
@@ -65,7 +68,7 @@ Elm.Native.Array.make = function(elm) {
6568 return pushed;
6669 }
6770
68− newTree = create(item, a.height);
71+ var newTree = create(item, a.height);
6972 return siblise(a, newTree);
7073 }
7174
@@ -423,11 +426,11 @@ Elm.Native.Array.make = function(elm) {
423426 }
424427
425428 // Returns how many items are in the tree.
426− function length(a) {
427− if (a.height == 0) {
428− return a.table.length;
429+ function length(array) {
430+ if (array.height == 0) {
431+ return array.table.length;
429432 } else {
430− return a.lengths[a.lengths.length - 1];
433+ return array.lengths[array.lengths.length - 1];
431434 }
432435 }
433436