-
-
Notifications
You must be signed in to change notification settings - Fork 921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ruby 2.4] Fixes for Integer#digits in PR #4375 #4490
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,6 +236,54 @@ public static BigInteger long2big(long arg) { | |
* ================ | ||
*/ | ||
|
||
/** rb_big_digits | ||
* | ||
*/ | ||
@Override | ||
public RubyArray digits(ThreadContext context, IRubyObject base) { | ||
BigInteger self = (this).getValue(); | ||
if (self.compareTo(new BigInteger("0")) == -1) { | ||
throw context.getRuntime().newMathDomainError("out of domain"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throw context.runtime into a local variable at the top and reuse it, in the style of other methods. |
||
} | ||
if (!(base instanceof RubyInteger)) { | ||
try { | ||
base = base.convertToInteger(); | ||
} catch (ClassCastException e) { | ||
String cname = base.getMetaClass().getRealClass().getName(); | ||
throw context.getRuntime().newTypeError("wrong argument type " + cname + " (expected Integer)"); | ||
} | ||
} | ||
|
||
BigInteger bigBase; | ||
if (base instanceof RubyBignum) { | ||
bigBase = ((RubyBignum) base).getValue(); | ||
} else { | ||
bigBase = long2big( ((RubyFixnum) base).getLongValue() ); | ||
} | ||
|
||
if (bigBase.compareTo(BigInteger.ZERO) == -1) { | ||
throw context.getRuntime().newArgumentError("negative radix"); | ||
} | ||
if (bigBase.compareTo(new BigInteger("2")) == -1) { | ||
throw context.getRuntime().newArgumentError("invalid radix: " + bigBase); | ||
} | ||
|
||
RubyArray res = RubyArray.newArray(context.runtime, 0); | ||
|
||
if (self.compareTo(new BigInteger("0")) == 0) { | ||
res.append(RubyFixnum.newFixnum(context.getRuntime(), 0)); | ||
return res; | ||
} | ||
|
||
while (self.compareTo(BigInteger.ZERO) > 0) { | ||
BigInteger q = self.mod(bigBase); | ||
res.append(RubyBignum.newBignum(context.getRuntime(), q)); | ||
self = self.divide(bigBase); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
/** rb_big_to_s | ||
* | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,6 +302,53 @@ public IRubyObject times(ThreadContext context, Block block) { | |
return RubyEnumerator.enumeratorizeWithSize(context, this, "times", timesSizeFn(context.runtime)); | ||
} | ||
|
||
/** rb_fix_digits | ||
* | ||
*/ | ||
@Override | ||
public RubyArray digits(ThreadContext context, IRubyObject base) { | ||
|
||
long self = ((RubyFixnum)this).getLongValue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not necessary... |
||
if (self < 0) { | ||
throw context.getRuntime().newMathDomainError("out of domain"); | ||
} | ||
if (!(base instanceof RubyInteger)) { | ||
try { | ||
base = base.convertToInteger(); | ||
} catch (ClassCastException e) { | ||
String cname = base.getMetaClass().getRealClass().getName(); | ||
throw context.getRuntime().newTypeError("wrong argument type " + cname + " (expected Integer)"); | ||
} | ||
} | ||
if (base instanceof RubyBignum){ | ||
return RubyArray | ||
.newArray(context.runtime, 1) | ||
.append(RubyFixnum.newFixnum(context.getRuntime(), self)); | ||
} | ||
long longBase = ((RubyFixnum)base).getLongValue(); | ||
if (longBase < 0) { | ||
throw context.getRuntime().newArgumentError("negative radix"); | ||
} | ||
if (longBase < 2) { | ||
throw context.getRuntime().newArgumentError("invalid radix: " + longBase); | ||
} | ||
|
||
RubyArray res = RubyArray.newArray(context.runtime, 0); | ||
|
||
if (self == 0) { | ||
res.append(RubyFixnum.newFixnum(context.getRuntime(), 0)); | ||
return res; | ||
} | ||
|
||
while (self > 0) { | ||
long q = self % longBase; | ||
res.append(RubyFixnum.newFixnum(context.getRuntime(), q)); | ||
self /= longBase; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
/** fix_to_s | ||
* | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just access the field here, or at least call it without the parens. I assume this is just a mistake converting the RubyFixnum-only logic.