Skip to content

Commit

Permalink
Showing 2 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.source.SourceSection;
import java.util.Locale;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.RubyContext;
@@ -722,17 +723,45 @@ public double toF(double value) {

}

@CoreMethod(names = { "java_to_s" }, visibility = Visibility.PRIVATE)
public abstract static class JavaToSNode extends CoreMethodArrayArgumentsNode {
@CoreMethod(names = { "to_s", "inspect" })
public abstract static class ToSNode extends CoreMethodArrayArgumentsNode {

public JavaToSNode(RubyContext context, SourceSection sourceSection) {
public ToSNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@TruffleBoundary
@Specialization
public DynamicObject javaToS(double value) {
return create7BitString(String.format("%.15g", value), USASCIIEncoding.INSTANCE);
public DynamicObject toS(double value) {
if (Double.isInfinite(value) || Double.isNaN(value)) {
return create7BitString(Double.toString(value), USASCIIEncoding.INSTANCE);
}

String str = String.format(Locale.ENGLISH, "%.15g", value);

// If no dot, add one to show it's a floating point number
if (str.indexOf('.') == -1) {
assert str.indexOf('e') == -1;
str += ".0";
}

final int e = str.indexOf('e');
final boolean hasE = e != -1;

// Remove trailing zeroes
final int start = hasE ? e : str.length();
int i = start;
while (i > 0 && str.charAt(i - 1) == '0') {
i--;
}

// But keep at least one after the dot
if (i > 0 && str.charAt(i - 1) == '.') {
i++;
}

final String formatted = str.substring(0, i) + str.substring(start, str.length());
return create7BitString(formatted, USASCIIEncoding.INSTANCE);
}

}
18 changes: 0 additions & 18 deletions truffle/src/main/ruby/core/float.rb
Original file line number Diff line number Diff line change
@@ -74,22 +74,4 @@ def equal_fallback(other)

private :equal_fallback

# TODO (pitr 27-Nov-2015): needs better implementation
def to_s
return format('%g', self) if infinite? || nan?

str = java_to_s

# remove extra zeroes
str.gsub! /^(-?)(\d+\.((\d*[1-9])|0))0+/, '\1\2'

return str if str =~ /e/

# add trailing zero if none
str << '.0' unless str =~ /\./
str
end

alias_method :inspect, :to_s

end

0 comments on commit dc83f5d

Please sign in to comment.