forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRubyRange.java
More file actions
725 lines (630 loc) · 30 KB
/
Copy pathRubyRange.java
File metadata and controls
725 lines (630 loc) · 30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Chad Fowler <chadfowler@chadfowler.com>
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001 Ed Sinjiashvili <slorcim@users.sourceforge.net>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2002-2006 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import static org.jruby.RubyEnumerator.enumeratorize;
import java.io.IOException;
import java.util.List;
import org.jcodings.Encoding;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.JumpException;
import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.BlockBody;
import org.jruby.runtime.BlockCallback;
import org.jruby.runtime.CallBlock;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ObjectMarshal;
import org.jruby.runtime.ThreadContext;
import static org.jruby.runtime.Visibility.*;
import static org.jruby.CompatVersion.*;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.builtin.Variable;
import org.jruby.runtime.component.VariableEntry;
import org.jruby.runtime.marshal.MarshalStream;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.util.ByteList;
import org.jruby.util.TypeConverter;
import static org.jruby.javasupport.util.RuntimeHelpers.invokedynamic;
import static org.jruby.runtime.MethodIndex.HASH;
import static org.jruby.runtime.MethodIndex.OP_CMP;
/**
* @author jpetersen
*/
@JRubyClass(name="Range", include="Enumerable")
public class RubyRange extends RubyObject {
private IRubyObject begin;
private IRubyObject end;
private boolean isExclusive;
public static RubyClass createRangeClass(Ruby runtime) {
RubyClass result = runtime.defineClass("Range", runtime.getObject(), RANGE_ALLOCATOR);
runtime.setRange(result);
result.index = ClassIndex.RANGE;
result.setReifiedClass(RubyRange.class);
result.kindOf = new RubyModule.KindOf() {
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyRange;
}
};
result.setMarshal(RANGE_MARSHAL);
result.includeModule(runtime.getEnumerable());
result.defineAnnotatedMethods(RubyRange.class);
return result;
}
private static final ObjectAllocator RANGE_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubyRange(runtime, klass);
}
};
private RubyRange(Ruby runtime, RubyClass klass) {
super(runtime, klass);
begin = end = runtime.getNil();
}
public static RubyRange newRange(Ruby runtime, ThreadContext context, IRubyObject begin, IRubyObject end, boolean isExclusive) {
RubyRange range = new RubyRange(runtime, runtime.getRange());
range.init(context, begin, end, isExclusive);
return range;
}
public static RubyRange newExclusiveRange(Ruby runtime, ThreadContext context, IRubyObject begin, IRubyObject end) {
RubyRange range = new RubyRange(runtime, runtime.getRange());
range.init(context, begin, end, true);
return range;
}
public static RubyRange newInclusiveRange(Ruby runtime, ThreadContext context, IRubyObject begin, IRubyObject end) {
RubyRange range = new RubyRange(runtime, runtime.getRange());
range.init(context, begin, end, false);
return range;
}
@Override
public void copySpecialInstanceVariables(IRubyObject clone) {
RubyRange range = (RubyRange)clone;
range.begin = begin;
range.end = end;
range.isExclusive = isExclusive;
}
final boolean checkBegin(long length) {
long beg = RubyNumeric.num2long(this.begin);
if(beg < 0) {
beg += length;
if(beg < 0) {
return false;
}
} else if(length < beg) {
return false;
}
return true;
}
final long[] begLen(long len, int err){
long beg = RubyNumeric.num2long(this.begin);
long end = RubyNumeric.num2long(this.end);
if (beg < 0) {
beg += len;
if (beg < 0) {
if (err != 0) throw getRuntime().newRangeError(beg + ".." + (isExclusive ? "." : "") + end + " out of range");
return null;
}
}
if (err == 0 || err == 2) {
if (beg > len) {
if (err != 0) throw getRuntime().newRangeError(beg + ".." + (isExclusive ? "." : "") + end + " out of range");
return null;
}
if (end > len) end = len;
}
if (end < 0) end += len;
if (!isExclusive) end++;
len = end - beg;
if (len < 0) len = 0;
return new long[]{beg, len};
}
final int[] begLenInt(int len, int err){
int beg = RubyNumeric.num2int(this.begin);
int end = RubyNumeric.num2int(this.end);
if (beg < 0) {
beg += len;
if (beg < 0) {
if (err != 0) throw getRuntime().newRangeError(beg + ".." + (isExclusive ? "." : "") + end + " out of range");
return null;
}
}
if (err == 0 || err == 2) {
if (beg > len) {
if (err != 0) throw getRuntime().newRangeError(beg + ".." + (isExclusive ? "." : "") + end + " out of range");
return null;
}
if (end > len) end = len;
}
if (end < 0) end += len;
if (!isExclusive) end++;
len = end - beg;
if (len < 0) len = 0;
return new int[]{beg, len};
}
private void init(ThreadContext context, IRubyObject begin, IRubyObject end, boolean isExclusive) {
if (!(begin instanceof RubyFixnum && end instanceof RubyFixnum)) {
try {
IRubyObject result = invokedynamic(context, begin, OP_CMP, end);
if (result.isNil()) throw getRuntime().newArgumentError("bad value for range");
} catch (RaiseException re) {
throw getRuntime().newArgumentError("bad value for range");
}
}
this.begin = begin;
this.end = end;
this.isExclusive = isExclusive;
}
@JRubyMethod(required = 2, optional = 1, visibility = PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block unusedBlock) {
if (!begin.isNil() || !end.isNil()) {
throw getRuntime().newNameError("`initialize' called twice", "initialize");
}
init(context, args[0], args[1], args.length > 2 && args[2].isTrue());
return getRuntime().getNil();
}
@JRubyMethod(name = {"first", "begin"})
public IRubyObject first() {
return begin;
}
@JRubyMethod(name = {"last", "end"})
public IRubyObject last() {
return end;
}
@JRubyMethod(name = "hash")
public RubyFixnum hash(ThreadContext context) {
long hash = isExclusive ? 1 : 0;
long h = hash;
long v = invokedynamic(context, begin, HASH).convertToInteger().getLongValue();
hash ^= v << 1;
v = invokedynamic(context, end, HASH).convertToInteger().getLongValue();
hash ^= v << 9;
hash ^= h << 24;
return getRuntime().newFixnum(hash);
}
private static byte[] DOTDOTDOT = "...".getBytes();
private static byte[] DOTDOT = "..".getBytes();
@JRubyMethod(name = "inspect")
public IRubyObject inspect(ThreadContext context) {
RubyString str = inspect(context, begin).strDup(context.getRuntime());
RubyString str2 = inspect(context, end);
str.cat(isExclusive ? DOTDOTDOT : DOTDOT);
str.concat(str2);
str.infectBy(str2);
return str;
}
@JRubyMethod(name = "to_s")
public IRubyObject to_s(ThreadContext context) {
RubyString str = RubyString.objAsString(context, begin).strDup(context.getRuntime());
RubyString str2 = RubyString.objAsString(context, end);
str.cat(isExclusive ? DOTDOTDOT : DOTDOT);
str.concat(str2);
str.infectBy(str2);
return str;
}
@JRubyMethod(name = "exclude_end?")
public RubyBoolean exclude_end_p() {
return getRuntime().newBoolean(isExclusive);
}
@JRubyMethod(name = "==", required = 1)
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
if (this == other) return getRuntime().getTrue();
if (!(other instanceof RubyRange)) return getRuntime().getFalse();
RubyRange otherRange = (RubyRange) other;
if (equalInternal(context, begin, otherRange.begin) &&
equalInternal(context, end, otherRange.end) &&
isExclusive == otherRange.isExclusive) return getRuntime().getTrue();
return getRuntime().getFalse();
}
@JRubyMethod(name = "eql?", required = 1)
public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
if (this == other) return getRuntime().getTrue();
if (!(other instanceof RubyRange)) return getRuntime().getFalse();
RubyRange otherRange = (RubyRange)other;
if (eqlInternal(context, begin, otherRange.begin) &&
eqlInternal(context, end, otherRange.end) &&
isExclusive == otherRange.isExclusive) return getRuntime().getTrue();
return getRuntime().getFalse();
}
private static abstract class RangeCallBack {
abstract void call(ThreadContext context, IRubyObject arg);
}
private static final class StepBlockCallBack extends RangeCallBack implements BlockCallback {
final Block block;
IRubyObject iter;
final IRubyObject step;
StepBlockCallBack(Block block, IRubyObject iter, IRubyObject step) {
this.block = block;
this.iter = iter;
this.step = step;
}
public IRubyObject call(ThreadContext context, IRubyObject[] args, Block originalBlock) {
call(context, args[0]);
return context.getRuntime().getNil();
}
void call(ThreadContext context, IRubyObject arg) {
if (iter instanceof RubyFixnum) {
iter = RubyFixnum.newFixnum(context.getRuntime(), ((RubyFixnum)iter).getLongValue() - 1);
} else {
iter = iter.callMethod(context, "-", RubyFixnum.one(context.getRuntime()));
}
if (iter == RubyFixnum.zero(context.getRuntime())) {
block.yield(context, arg);
iter = step;
}
}
}
private IRubyObject rangeLt(ThreadContext context, IRubyObject a, IRubyObject b) {
IRubyObject result = invokedynamic(context, a, OP_CMP, b);
if (result.isNil()) return null;
return RubyComparable.cmpint(context, result, a, b) < 0 ? getRuntime().getTrue() : null;
}
private IRubyObject rangeLe(ThreadContext context, IRubyObject a, IRubyObject b) {
IRubyObject result = invokedynamic(context, a, OP_CMP, b);
if (result.isNil()) return null;
int c = RubyComparable.cmpint(context, result, a, b);
if (c == 0) return RubyFixnum.zero(getRuntime());
return c < 0 ? getRuntime().getTrue() : null;
}
private void rangeEach(ThreadContext context, RangeCallBack callback) {
IRubyObject v = begin;
if (isExclusive) {
while (rangeLt(context, v, end) != null) {
callback.call(context, v);
v = v.callMethod(context, "succ");
}
} else {
IRubyObject c;
while ((c = rangeLe(context, v, end)) != null && c.isTrue()) {
callback.call(context, v);
if (c == RubyFixnum.zero(getRuntime())) break;
v = v.callMethod(context, "succ");
}
}
}
@JRubyMethod
public IRubyObject to_a(ThreadContext context, final Block block) {
final Ruby runtime = context.getRuntime();
if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
long lim = ((RubyFixnum) end).getLongValue();
if (!isExclusive) lim++;
long base = ((RubyFixnum) begin).getLongValue();
long size = lim - base;
if (size > Integer.MAX_VALUE) {
throw runtime.newRangeError("Range size too large for to_a");
}
if (size < 0) return RubyArray.newEmptyArray(runtime);
IRubyObject[] array = new IRubyObject[(int)size];
for (int i = 0; i < size; i++) {
array[i] = RubyFixnum.newFixnum(runtime, base + i);
}
return RubyArray.newArrayNoCopy(runtime, array);
} else {
return RubyEnumerable.to_a(context, this);
}
}
@JRubyMethod(compat = RUBY1_8)
public IRubyObject each(ThreadContext context, final Block block) {
final Ruby runtime = context.getRuntime();
if (!block.isGiven()) return enumeratorize(runtime, this, "each");
if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
fixnumEach(context, runtime, block);
} else if (begin instanceof RubyString) {
((RubyString) begin).uptoCommon18(context, end, isExclusive, block);
} else {
if (!begin.respondsTo("succ")) throw getRuntime().newTypeError(
"can't iterate from " + begin.getMetaClass().getName());
rangeEach(context, new RangeCallBack() {
@Override
void call(ThreadContext context, IRubyObject arg) {
block.yield(context, arg);
}
});
}
return this;
}
private void fixnumEach(ThreadContext context, Ruby runtime, Block block) {
long lim = ((RubyFixnum) end).getLongValue();
if (!isExclusive) lim++;
if (block.getBody().getArgumentType() == BlockBody.ZERO_ARGS) {
final IRubyObject nil = runtime.getNil();
for (long i = ((RubyFixnum) begin).getLongValue(); i < lim; i++) {
block.yield(context, nil);
}
} else {
for (long i = ((RubyFixnum) begin).getLongValue(); i < lim; i++) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
}
}
@JRubyMethod(name = "each", compat = RUBY1_9)
public IRubyObject each19(final ThreadContext context, final Block block) {
Ruby runtime = context.getRuntime();
if (!block.isGiven()) return enumeratorize(runtime, this, "each");
if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
fixnumEach(context, runtime, block);
} else if (begin instanceof RubyString) {
((RubyString) begin).uptoCommon19(context, end, isExclusive, block);
} else if (begin instanceof RubySymbol) {
begin.asString().uptoCommon19(context, end.asString(), isExclusive, block, true);
} else {
if (!begin.respondsTo("succ")) throw getRuntime().newTypeError(
"can't iterate from " + begin.getMetaClass().getName());
rangeEach(context, new RangeCallBack() {
@Override
void call(ThreadContext context, IRubyObject arg) {
block.yield(context, arg);
}
});
}
return this;
}
@JRubyMethod(compat = RUBY1_8)
public IRubyObject step(ThreadContext context, IRubyObject step, Block block) {
return block.isGiven() ? stepCommon(context, step, block) : enumeratorize(context.getRuntime(), this, "step", step);
}
@JRubyMethod(compat = RUBY1_8)
public IRubyObject step(ThreadContext context, Block block) {
return block.isGiven() ? stepCommon(context, RubyFixnum.one(context.getRuntime()), block) : enumeratorize(context.getRuntime(), this, "step");
}
private IRubyObject stepCommon(ThreadContext context, IRubyObject step, Block block) {
final Ruby runtime = context.getRuntime();
long unit = RubyNumeric.num2long(step);
if (unit < 0) throw runtime.newArgumentError("step can't be negative");
if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
if (unit == 0) throw runtime.newArgumentError("step can't be 0");
fixnumStep(context, runtime, unit, block);
} else {
IRubyObject tmp = begin.checkStringType();
if (!tmp.isNil()) {
if (unit == 0) throw runtime.newArgumentError("step can't be 0");
// rb_iterate((VALUE(*)_((VALUE)))str_step, (VALUE)args, step_i, (VALUE)iter);
StepBlockCallBack callback = new StepBlockCallBack(block, RubyFixnum.one(runtime), step);
Block blockCallback = CallBlock.newCallClosure(this, runtime.getRange(), Arity.singleArgument(), callback, context);
((RubyString)tmp).uptoCommon18(context, end, isExclusive, blockCallback);
} else if (begin instanceof RubyNumeric) {
if (equalInternal(context, step, RubyFixnum.zero(runtime))) throw runtime.newArgumentError("step can't be 0");
numericStep(context, runtime, step, block);
} else {
if (unit == 0) throw runtime.newArgumentError("step can't be 0");
if (!begin.respondsTo("succ")) throw runtime.newTypeError("can't iterate from " + begin.getMetaClass().getName());
// range_each_func(range, step_i, b, e, args);
rangeEach(context, new StepBlockCallBack(block, RubyFixnum.one(runtime), step));
}
}
return this;
}
private void fixnumStep(ThreadContext context, Ruby runtime, long unit, Block block) {
long e = ((RubyFixnum)end).getLongValue();
if (!isExclusive) e++;
for (long i = ((RubyFixnum)begin).getLongValue(); i < e; i += unit) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
}
private void numericStep(ThreadContext context, Ruby runtime, IRubyObject step, Block block) {
final String method = isExclusive ? "<" : "<=";
IRubyObject beg = begin;
while (beg.callMethod(context, method, end).isTrue()) {
block.yield(context, beg);
beg = beg.callMethod(context, "+", step);
}
}
@JRubyMethod(name = "step", compat = RUBY1_9)
public IRubyObject step19(final ThreadContext context, final Block block) {
return block.isGiven() ? stepCommon19(context, RubyFixnum.zero(context.getRuntime()), block) : enumeratorize(context.getRuntime(), this, "step");
}
@JRubyMethod(name = "step", compat = RUBY1_9)
public IRubyObject step19(final ThreadContext context, IRubyObject step, final Block block) {
Ruby runtime = context.getRuntime();
if (!block.isGiven()) return enumeratorize(runtime, this, "step", step);
if (!(step instanceof RubyNumeric)) step = step.convertToInteger("to_int");
IRubyObject zero = RubyFixnum.zero(runtime);
if (step.callMethod(context, "<", zero).isTrue()) throw runtime.newArgumentError("step can't be negative");
if (!step.callMethod(context, ">", zero).isTrue()) throw runtime.newArgumentError("step can't be 0");
return stepCommon19(context, step, block);
}
private IRubyObject stepCommon19(ThreadContext context, IRubyObject step, Block block) {
Ruby runtime = context.getRuntime();
if (begin instanceof RubyFixnum && end instanceof RubyFixnum && step instanceof RubyFixnum) {
fixnumStep(context, runtime, ((RubyFixnum)step).getLongValue(), block);
} else if (begin instanceof RubyFloat || end instanceof RubyFloat || step instanceof RubyFloat) {
RubyNumeric.floatStep19(context, runtime, begin, end, step, isExclusive, block);
} else if (begin instanceof RubyNumeric ||
!TypeConverter.checkIntegerType(runtime, begin, "to_int").isNil() ||
!TypeConverter.checkIntegerType(runtime, end, "to_int").isNil()) {
numericStep19(context, runtime, step, block);
} else {
IRubyObject tmp = begin.checkStringType();
if (!tmp.isNil()) {
StepBlockCallBack callback = new StepBlockCallBack(block, RubyFixnum.one(runtime), step);
Block blockCallback = CallBlock.newCallClosure(this, runtime.getRange(), Arity.singleArgument(), callback, context);
((RubyString)tmp).uptoCommon19(context, end, isExclusive, blockCallback);
} else {
if (!begin.respondsTo("succ")) throw runtime.newTypeError("can't iterate from " + begin.getMetaClass().getName());
// range_each_func(range, step_i, b, e, args);
rangeEach(context, new StepBlockCallBack(block, RubyFixnum.one(runtime), step));
}
}
return this;
}
private void numericStep19(ThreadContext context, Ruby runtime, IRubyObject step, Block block) {
final String method = isExclusive ? "<" : "<=";
IRubyObject beg = begin;
long i = 0;
while (beg.callMethod(context, method, end).isTrue()) {
block.yield(context, beg);
i++;
beg = begin.callMethod(context, "+", RubyFixnum.newFixnum(runtime, i).callMethod(context, "*", step));
}
}
@JRubyMethod(name = {"include?", "member?", "==="}, required = 1, compat = RUBY1_8)
public RubyBoolean include_p(ThreadContext context, IRubyObject obj) {
if (rangeLe(context, begin, obj) != null) {
if (isExclusive) {
if (rangeLt(context, obj, end) != null) return context.getRuntime().getTrue();
} else {
if (rangeLe(context, obj, end) != null) return context.getRuntime().getTrue();
}
}
return context.getRuntime().getFalse();
}
// framed for invokeSuper
@JRubyMethod(name = {"include?", "member?"}, frame = true, compat = RUBY1_9)
public IRubyObject include_p19(ThreadContext context, IRubyObject obj) {
Ruby runtime = context.getRuntime();
if (begin instanceof RubyNumeric || end instanceof RubyNumeric ||
!TypeConverter.convertToTypeWithCheck(begin, runtime.getInteger(), "to_int").isNil() ||
!TypeConverter.convertToTypeWithCheck(end, runtime.getInteger(), "to_int").isNil()) {
if (rangeLe(context, begin, obj) != null) {
if (isExclusive) {
if (rangeLt(context, obj, end) != null) return runtime.getTrue();
} else {
if (rangeLe(context, obj, end) != null) return runtime.getTrue();
}
}
return runtime.getFalse();
} else if (begin instanceof RubyString && end instanceof RubyString &&
((RubyString) begin).getByteList().getRealSize() == 1 &&
((RubyString) end).getByteList().getRealSize() == 1) {
if (obj.isNil()) return runtime.getFalse();
if (obj instanceof RubyString) {
ByteList Vbytes = ((RubyString)obj).getByteList();
if (Vbytes.getRealSize() != 1) return runtime.getFalse();
int v = Vbytes.getUnsafeBytes()[Vbytes.getBegin()] & 0xff;
ByteList Bbytes = ((RubyString)begin).getByteList();
int b = Bbytes.getUnsafeBytes()[Bbytes.getBegin()] & 0xff;
ByteList Ebytes = ((RubyString)end).getByteList();
int e = Ebytes.getUnsafeBytes()[Ebytes.getBegin()] & 0xff;
if (Encoding.isAscii(v) && Encoding.isAscii(b) && Encoding.isAscii(e)) {
if ((b <= v && v < e) || (!isExclusive && v == e)) return runtime.getTrue();
return runtime.getFalse();
}
}
}
return RuntimeHelpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
}
@JRubyMethod(name = "===", compat = RUBY1_9)
public IRubyObject eqq_p19(ThreadContext context, IRubyObject obj) {
return callMethod(context, "include?", obj);
}
@JRubyMethod(name = "cover?", compat = RUBY1_9)
public IRubyObject cover_p(ThreadContext context, IRubyObject obj) {
return include_p(context, obj); // 1.8 "include?"
}
@JRubyMethod(compat = RUBY1_9, frame = true)
public IRubyObject min(ThreadContext context, Block block) {
if (block.isGiven()) {
return RuntimeHelpers.invokeSuper(context, this, block);
} else {
int c = RubyComparable.cmpint(context, invokedynamic(context, begin, OP_CMP, end), begin, end);
if (c > 0 || (c == 0 && isExclusive)) return context.getRuntime().getNil();
return begin;
}
}
@JRubyMethod(compat = RUBY1_9, frame = true)
public IRubyObject max(ThreadContext context, Block block) {
if (begin.callMethod(context, ">", end).isTrue()) {
return context.getRuntime().getNil();
}
if (block.isGiven() || isExclusive && !(end instanceof RubyNumeric)) {
return RuntimeHelpers.invokeSuper(context, this, block);
} else {
int c = RubyComparable.cmpint(context, invokedynamic(context, begin, OP_CMP, end), begin, end);
Ruby runtime = context.getRuntime();
if (isExclusive) {
if (!(end instanceof RubyInteger)) throw runtime.newTypeError("cannot exclude non Integer end value");
if (c == 0) return runtime.getNil();
if (end instanceof RubyFixnum) return RubyFixnum.newFixnum(runtime, ((RubyFixnum)end).getLongValue() - 1);
return end.callMethod(context, "-", RubyFixnum.one(runtime));
}
return end;
}
}
@JRubyMethod(name = "first", compat = RUBY1_9)
public IRubyObject first(ThreadContext context) {
return begin;
}
@JRubyMethod(name = "first", compat = RUBY1_9)
public IRubyObject first(ThreadContext context, IRubyObject arg) {
final Ruby runtime = context.getRuntime();
final int num = RubyNumeric.num2int(arg);
final RubyArray result = runtime.newArray(num);
try {
RubyEnumerable.callEach(runtime, context, this, Arity.ONE_ARGUMENT, new BlockCallback() {
int n = num;
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
if (n-- <= 0) throw JumpException.SPECIAL_JUMP;
result.append(largs[0]);
return runtime.getNil();
}
});
} catch (JumpException.SpecialJump sj) {}
return result;
}
@JRubyMethod(name = "last", compat = RUBY1_9)
public IRubyObject last(ThreadContext context) {
return end;
}
@JRubyMethod(name = "last", compat = RUBY1_9)
public IRubyObject last(ThreadContext context, IRubyObject arg) {
return ((RubyArray)RubyKernel.new_array(context, this, this)).last(arg);
}
private static final ObjectMarshal RANGE_MARSHAL = new ObjectMarshal() {
public void marshalTo(Ruby runtime, Object obj, RubyClass type,
MarshalStream marshalStream) throws IOException {
RubyRange range = (RubyRange)obj;
marshalStream.registerLinkTarget(range);
List<Variable<Object>> attrs = range.getVariableList();
attrs.add(new VariableEntry<Object>("begin", range.begin));
attrs.add(new VariableEntry<Object>("end", range.end));
attrs.add(new VariableEntry<Object>("excl", range.isExclusive ? runtime.getTrue() : runtime.getFalse()));
marshalStream.dumpVariables(attrs);
}
public Object unmarshalFrom(Ruby runtime, RubyClass type,
UnmarshalStream unmarshalStream) throws IOException {
RubyRange range = (RubyRange)type.allocate();
unmarshalStream.registerLinkTarget(range);
// FIXME: Maybe we can just gank these off the line directly?
unmarshalStream.defaultVariablesUnmarshal(range);
range.begin = (IRubyObject)range.removeInternalVariable("begin");
range.end = (IRubyObject)range.removeInternalVariable("end");
range.isExclusive = ((IRubyObject)range.removeInternalVariable("excl")).isTrue();
return range;
}
};
}