| Method from org.jruby.RubyRange Detail: |
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};
}
|
protected void copySpecialInstanceVariables(IRubyObject clone) {
RubyRange range = (RubyRange)clone;
range.begin = begin;
range.end = end;
range.isExclusive = isExclusive;
}
|
public static RubyClass createRangeClass(Ruby runtime) {
RubyClass result = runtime.defineClass("Range", runtime.getObject(), RANGE_ALLOCATOR);
runtime.setRange(result);
result.kindOf = new RubyModule.KindOf() {
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyRange;
}
};
result.setMarshal(RANGE_MARSHAL);
result.includeModule(runtime.getEnumerable());
// We override Enumerable#member? since ranges in 1.8.1 are continuous.
// result.defineMethod("member?", callbackFactory.getMethod("include_p", RubyKernel.IRUBY_OBJECT));
// result.defineMethod("===", callbackFactory.getMethod("include_p", RubyKernel.IRUBY_OBJECT));
result.defineAnnotatedMethods(RubyRange.class);
return result;
}
|
public IRubyObject each(ThreadContext context,
Block block) {
final Ruby runtime = context.getRuntime();
if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
long lim = ((RubyFixnum) end).getLongValue();
if (!isExclusive) lim++;
for (long i = ((RubyFixnum) begin).getLongValue(); i < lim; i++) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
} else if (begin instanceof RubyString) {
((RubyString) begin).upto(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;
}
|
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();
}
|
public RubyBoolean exclude_end_p() {
return getRuntime().newBoolean(isExclusive);
}
|
public IRubyObject first() {
return begin;
}
|
public RubyFixnum hash(ThreadContext context) {
long hash = isExclusive ? 1 : 0;
long h = hash;
long v = begin.callMethod(context, MethodIndex.HASH, "hash").convertToInteger().getLongValue();
hash ^= v < < 1;
v = end.callMethod(context, MethodIndex.HASH, "hash").convertToInteger().getLongValue();
hash ^= v < < 9;
hash ^= h < < 24;
return getRuntime().newFixnum(hash);
}
|
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();
}
|
public IRubyObject initialize(ThreadContext context,
IRubyObject[] args,
Block unusedBlock) {
init(context, args[0], args[1], args.length > 2 && args[2].isTrue());
return getRuntime().getNil();
}
|
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;
}
|
public IRubyObject last() {
return end;
}
|
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;
}
|
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 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();
}
|
public IRubyObject step(ThreadContext context,
IRubyObject[] args,
Block block) {
final Ruby runtime = context.getRuntime();
final IRubyObject step;
if (args.length == 0) {
step = RubyFixnum.one(runtime);
} else {
step = args[0];
}
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");
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));
}
} 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).upto(context, end, isExclusive, blockCallback);
} else if (begin instanceof RubyNumeric) {
if (equalInternal(context, step, RubyFixnum.zero(runtime))) {
throw runtime.newArgumentError("step can't be 0");
}
final String method;
final int methodIndex;
if (isExclusive) {
method = "< ";
methodIndex = MethodIndex.OP_LT;
} else {
method = "< =";
methodIndex = MethodIndex.OP_LE;
}
IRubyObject beg = begin;
while (beg.callMethod(context, methodIndex, method, end).isTrue()) {
block.yield(context, beg);
beg = beg.callMethod(context, MethodIndex.OP_PLUS, "+", step);
}
} 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;
}
|
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;
}
|