@@ -241,14 +241,15 @@ protected Object getIndex(RubyMatchData matchData, int index, NotProvided length
241
241
final RubyString source = matchData .source ;
242
242
final Rope sourceRope = source .rope ;
243
243
final Region region = matchData .region ;
244
- final int normalizedIndex = ArrayOperations
245
- .normalizeIndex (region .beg .length , index , normalizedIndexProfile );
244
+ if (normalizedIndexProfile .profile (index < 0 )) {
245
+ index += region .beg .length ;
246
+ }
246
247
247
- if (indexOutOfBoundsProfile .profile ((normalizedIndex < 0 ) || (normalizedIndex >= region .beg .length ))) {
248
+ if (indexOutOfBoundsProfile .profile ((index < 0 ) || (index >= region .beg .length ))) {
248
249
return nil ;
249
250
} else {
250
- final int start = region .beg [normalizedIndex ];
251
- final int end = region .end [normalizedIndex ];
251
+ final int start = region .beg [index ];
252
+ final int end = region .end [index ];
252
253
if (hasValueProfile .profile (start > -1 && end > -1 )) {
253
254
Rope rope = substringNode .executeSubstring (sourceRope , start , end - start );
254
255
final Shape shape = allocateHelperNode .getCachedShape (source .getLogicalClass ());
@@ -262,11 +263,14 @@ protected Object getIndex(RubyMatchData matchData, int index, NotProvided length
262
263
}
263
264
264
265
@ Specialization
265
- protected RubyArray getIndex (RubyMatchData matchData , int index , int length ) {
266
+ protected RubyArray getIndex (RubyMatchData matchData , int index , int length ,
267
+ @ Cached ConditionProfile normalizedIndexProfile ) {
266
268
// TODO BJF 15-May-2015 Need to handle negative indexes and lengths and out of bounds
267
269
final Object [] values = getValuesNode .execute (matchData );
268
- final int normalizedIndex = ArrayOperations .normalizeIndex (values .length , index );
269
- final Object [] store = Arrays .copyOfRange (values , normalizedIndex , normalizedIndex + length );
270
+ if (normalizedIndexProfile .profile (index < 0 )) {
271
+ index += values .length ;
272
+ }
273
+ final Object [] store = Arrays .copyOfRange (values , index , index + length );
270
274
return createArray (store );
271
275
}
272
276
@@ -310,13 +314,19 @@ protected Object getIndex(RubyMatchData matchData, Object index, NotProvided len
310
314
@ Specialization
311
315
protected RubyArray getIndex (RubyMatchData matchData , RubyIntRange range , NotProvided len ) {
312
316
final Object [] values = getValuesNode .execute (matchData );
313
- final int normalizedIndex = ArrayOperations .normalizeIndex (values .length , range .begin );
314
- final int end = ArrayOperations .normalizeIndex (values .length , range .end );
317
+ int index = range .begin ;
318
+ if (range .begin < 0 ) {
319
+ index += values .length ;
320
+ }
321
+ int end = range .end ;
322
+ if (end < 0 ) {
323
+ end += values .length ;
324
+ }
315
325
final int exclusiveEnd = ArrayOperations
316
326
.clampExclusiveIndex (values .length , range .excludedEnd ? end : end + 1 );
317
- final int length = exclusiveEnd - normalizedIndex ;
327
+ final int length = exclusiveEnd - index ;
318
328
319
- return createArray (Arrays .copyOfRange (values , normalizedIndex , normalizedIndex + length ));
329
+ return createArray (Arrays .copyOfRange (values , index , index + length ));
320
330
}
321
331
322
332
@ TruffleBoundary
0 commit comments