Skip to content

Commit

Permalink
Showing 2 changed files with 31 additions and 22 deletions.
45 changes: 23 additions & 22 deletions core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
Original file line number Diff line number Diff line change
@@ -506,24 +506,21 @@ private static RubyBigDecimal newInstance(ThreadContext context, IRubyObject rec
String strValue = arg.convertToString().toString().trim();

int sign = 1;
if(strValue.length() > 0) {
switch (strValue.charAt(0)) {
case '_' :
return newZero(context.runtime, 1); // leading "_" are not allowed
case 'N' :
if ( "NaN".equals(strValue) ) return newNaN(context.runtime);
break;
case 'I' :
if ( "Infinity".equals(strValue) ) return newInfinity(context.runtime, 1);
break;
case '-' :
if ( "-Infinity".equals(strValue) ) return newInfinity(context.runtime, -1);
sign = -1;
break;
case '+' :
if ( "+Infinity".equals(strValue) ) return newInfinity(context.runtime, +1);
break;
}
switch ( strValue.length() > 0 ? strValue.charAt(0) : ' ' ) {
case '_' : return newZero(context.runtime, 1); // leading "_" are not allowed
case 'N' :
if ( "NaN".equals(strValue) ) return newNaN(context.runtime);
break;
case 'I' :
if ( "Infinity".equals(strValue) ) return newInfinity(context.runtime, 1);
break;
case '-' :
if ( "-Infinity".equals(strValue) ) return newInfinity(context.runtime, -1);
sign = -1;
break;
case '+' :
if ( "+Infinity".equals(strValue) ) return newInfinity(context.runtime, +1);
break;
}

// Convert String to Java understandable format (for BigDecimal).
@@ -533,17 +530,21 @@ private static RubyBigDecimal newInstance(ThreadContext context, IRubyObject rec
Matcher matcher = NUMBER_PATTERN.matcher(strValue);
strValue = matcher.replaceFirst("$1"); // 3. MRI ignores the trailing junk

String exp = matcher.group(2);
if(!exp.isEmpty()) {
String exp = matcher.group(2); int idx;
if ( exp != null && ! exp.isEmpty() ) {
String expValue = matcher.group(3);
if (expValue.isEmpty() || expValue.equals("-") || expValue.equals("+")) {
strValue = strValue.concat("0"); // 4. MRI allows 1E, 1E-, 1E+
} else if (isExponentOutOfRange(expValue)) {
}
else if (isExponentOutOfRange(expValue)) {
// Handle infinity (Integer.MIN_VALUE + 1) < expValue < Integer.MAX_VALUE
return newInfinity(context.runtime, sign);
}
}

else if ( ( idx = matcher.start(3) ) > 0 ) {
strValue = strValue.substring(0, idx); // ignored tail junk e.g. "5-6" -> "-6"
}

BigDecimal decimal;
try {
decimal = new BigDecimal(strValue, mathContext);
8 changes: 8 additions & 0 deletions test/jruby/test_big_decimal.rb
Original file line number Diff line number Diff line change
@@ -348,6 +348,14 @@ def test_GH_2650
assert_equal(BigDecimal.new("10.9", 2).to_f, 10.9)
end

# GH-3527
def test_tail_junk
b = BigDecimal.new("5-6")
assert_equal BigDecimal('5'), b
b = BigDecimal.new("100+42")
assert_equal 100, b.to_i
end

class BigDeci < BigDecimal

# MRI does not invoke initialize on 1.8./1.9

0 comments on commit 5ffa6cd

Please sign in to comment.