Skip to content

Commit 8722181

Browse files
committed
Fixes jruby#9044. Not parsing some valid hex values in Float()
1 parent 6329fe0 commit 8722181

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

core/src/main/java/org/jruby/RubyKernel.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ public static IRubyObject new_float(ThreadContext context, IRubyObject object, b
540540
throw runtime.newArgumentError("invalid value for Float(): " + object.inspect());
541541
}
542542

543-
if (bytes.startsWith(ZEROx)) { // startsWith("0x")
543+
if (isHexValue(bytes)) {
544544
if (bytes.indexOf('p') != -1 || bytes.indexOf('P') != -1) {
545545
return runtime.newFloat(parseHexidecimalExponentString2(runtime, bytes));
546546
}
@@ -567,6 +567,12 @@ public static IRubyObject new_float(ThreadContext context, IRubyObject object, b
567567
return TypeConverter.handleUncoercibleObject(runtime, object, runtime.getFloat(), true);
568568
}
569569

570+
static boolean isHexValue(ByteList bytes) {
571+
int length = bytes.getRealSize();
572+
int index = length >= 1 && bytes.get(0) == '-' ? 1 : 0;
573+
return length >= index + 2 && bytes.get(index) == '0' && bytes.get(index + 1) == 'x' || bytes.get(index + 1) == 'X';
574+
}
575+
570576
static RubyFloat new_float(final Ruby runtime, RubyInteger num) {
571577
if (num instanceof RubyBignum) {
572578
return RubyFloat.newFloat(runtime, RubyBignum.big2dbl((RubyBignum) num));

0 commit comments

Comments
 (0)