Skip to content

Commit

Permalink
Cleaned up some 1.8 cruft from RubyNumeric.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Apr 3, 2015
1 parent f4653e5 commit 2832da5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyFloat.java
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ public static RubyFloat unmarshalFrom(UnmarshalStream input) throws java.io.IOEx
result = RubyFloat.newFloat(input.getRuntime(), Double.POSITIVE_INFINITY);
} else {
result = RubyFloat.newFloat(input.getRuntime(),
ConvertDouble.byteListToDouble(value, false));
ConvertDouble.byteListToDouble19(value, false));
}
input.registerLinkTarget(result);
return result;
Expand Down
19 changes: 2 additions & 17 deletions core/src/main/java/org/jruby/RubyNumeric.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,6 @@ public static RubyInteger str2inum(Ruby runtime, RubyString str, int base, boole
return ConvertBytes.byteListToInum(runtime, s, base, strict);
}

public static RubyFloat str2fnum(Ruby runtime, RubyString arg) {
return str2fnum(runtime,arg,false);
}

/**
* Converts a string representation of a floating-point number to the
* numeric value. Parsing starts at the beginning of the string (after
Expand All @@ -362,10 +358,6 @@ public static RubyFloat str2fnum(Ruby runtime, RubyString arg) {
* @return a RubyFloat representing the result of the conversion, which
* will be 0.0 if the conversion failed.
*/
public static RubyFloat str2fnum(Ruby runtime, RubyString arg, boolean strict) {
return str2fnumCommon(runtime, arg, strict, biteListCaller18);
}

public static RubyFloat str2fnum19(Ruby runtime, RubyString arg, boolean strict) {
return str2fnumCommon(runtime, arg, strict, biteListCaller19);
}
Expand All @@ -383,16 +375,9 @@ private static RubyFloat str2fnumCommon(Ruby runtime, RubyString arg, boolean st
}
}

private static interface ByteListCaller {
public double yield(RubyString arg, boolean strict);
}

private static class ByteListCaller18 implements ByteListCaller {
public double yield(RubyString arg, boolean strict) {
return ConvertDouble.byteListToDouble(arg.getByteList(),strict);
}
private interface ByteListCaller {
double yield(RubyString arg, boolean strict);
}
private static final ByteListCaller18 biteListCaller18 = new ByteListCaller18();

private static class ByteListCaller19 implements ByteListCaller {
public double yield(RubyString arg, boolean strict) {
Expand Down
21 changes: 5 additions & 16 deletions core/src/main/java/org/jruby/util/ConvertDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,15 @@ public class ConvertDouble {
* Converts supplied ByteList into a double. strict-mode will not like
* extra text non-numeric text or multiple sequention underscores.
*/
public static double byteListToDouble(ByteList bytes, boolean strict) {
return new DoubleConverter().parse(bytes, strict, false);
}

/**
* Converts supplied ByteList into a double. This is almost exactly the
* same as byteListToDouble, but leading underscores are no longer skipped
* in 1.9 (e.g. __1 == 0.0 in 1.9 while 1.8 parses it as 1.0).
*/
public static double byteListToDouble19(ByteList bytes, boolean strict) {
return new DoubleConverter().parse(bytes, strict, true);
return new DoubleConverter().parse(bytes, strict);
}

public static class DoubleConverter {
private byte[] bytes;
private int index;
private int endIndex;
private boolean isStrict;
private boolean is19;
private char[] chars; // result string we use to parse.
private int charsIndex;
private int significantDigitsProcessed;
Expand All @@ -72,12 +62,11 @@ public static class DoubleConverter {

public DoubleConverter() {}

public void init(ByteList list, boolean isStrict, boolean is19) {
public void init(ByteList list, boolean isStrict) {
bytes = list.getUnsafeBytes();
index = list.begin();
endIndex = index + list.length();
this.isStrict = isStrict;
this.is19 = is19;
// +2 for added exponent: E...
// The algorithm trades digits for inc/dec exponent.
// Worse case is adding E-1 when no exponent,
Expand Down Expand Up @@ -175,8 +164,8 @@ private boolean strictError() {
}
}

public double parse(ByteList list, boolean strict, boolean is19) {
init(list, strict, is19);
public double parse(ByteList list, boolean strict) {
init(list, strict);

if (skipWhitespace()) return completeCalculation();
if (parseOptionalSign()) return completeCalculation();
Expand All @@ -203,7 +192,7 @@ private boolean skipWhitespace() {
byte value = next();

if (isWhitespace(value)) continue;
if (value != '_' || isStrict || is19) return previous();
return previous();
}

return true;
Expand Down

0 comments on commit 2832da5

Please sign in to comment.