Skip to content

Commit

Permalink
Showing 6 changed files with 23 additions and 9 deletions.
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/kernel/sprintf_tags.txt
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@ fails:Kernel#sprintf passes some tests for negative %x
fails:Kernel#sprintf passes some tests for negative %u
fails:Kernel#sprintf passes some tests for positive %u
fails:Kernel#sprintf passes some tests for positive %d
fails:Kernel#sprintf passes some tests for positive %f
fails:Kernel#sprintf passes some tests for negative %f
fails:Kernel#sprintf passes kstephens's tests
fails:Kernel#sprintf returns a String in the same encoding as the format String if compatible
fails:Kernel#sprintf returns a String in the argument encoding if format encoding is more restrictive
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/string/modulo_tags.txt
Original file line number Diff line number Diff line change
@@ -57,8 +57,6 @@ fails:String#% supports hex formats using %x for positive numbers
fails:String#% supports hex formats using %x for negative numbers
fails:String#% supports hex formats using %X for positive numbers
fails:String#% supports hex formats using %X for negative numbers
fails:String#% formats zero without prefix using %#x
fails:String#% formats zero without prefix using %#X
fails:String#% tries to convert the passed argument to an Array using #to_ary
fails:String#% behaves as if calling Kernel#Float for %e arguments, when the passed argument does not respond to #to_ary
fails:String#% behaves as if calling Kernel#Float for %e arguments, when the passed argument is hexadecimal string
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ MINUS : '-' ;
STAR : '*' ;
DOLLAR : '$' ;
DOT : '.' ;
HASH : '#' ;
CURLY_KEY : '{' .*? '}' -> mode(DEFAULT_MODE) ;
TYPE : [bBdiouxXeEfgGaAcps] -> mode(DEFAULT_MODE) ;
ESCAPED : '%' -> mode(DEFAULT_MODE) ;
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ flag : SPACE
| PLUS
| MINUS
| STAR
| HASH
| NUMBER DOLLAR ;

literal : LITERAL ;
Original file line number Diff line number Diff line change
@@ -31,11 +31,13 @@ public abstract class FormatIntegerBinaryNode extends FormatNode {

private final char format;
private final boolean hasPlusFlag;
private final boolean useAlternativeFormat;

public FormatIntegerBinaryNode(RubyContext context, char format, boolean hasPlusFlag) {
public FormatIntegerBinaryNode(RubyContext context, char format, boolean hasPlusFlag, boolean useAlternativeFormat) {
super(context);
this.format = format;
this.hasPlusFlag = hasPlusFlag;
this.useAlternativeFormat = useAlternativeFormat;
}

@Specialization
@@ -46,7 +48,8 @@ public byte[] format(int spacePadding,
final boolean isNegative = value < 0;
final boolean negativeAndPadded = isNegative && (isSpacePadded || this.hasPlusFlag);
final String formatted = negativeAndPadded ? Integer.toBinaryString(-value) : Integer.toBinaryString(value);
return getFormattedString(formatted, spacePadding, zeroPadding, isNegative, isSpacePadded, this.hasPlusFlag);
return getFormattedString(formatted, spacePadding, zeroPadding, isNegative, isSpacePadded, this.hasPlusFlag,
this.useAlternativeFormat, this.format);
}

@TruffleBoundary
@@ -57,12 +60,14 @@ public byte[] format(int spacePadding, int zeroPadding, DynamicObject value) {
final boolean isNegative = bigInteger.signum() == -1;
final boolean negativeAndPadded = isNegative && (isSpacePadded || this.hasPlusFlag);
final String formatted = negativeAndPadded ? bigInteger.abs().toString(2) : bigInteger.toString(2);
return getFormattedString(formatted, spacePadding, zeroPadding, isNegative, isSpacePadded, this.hasPlusFlag);
return getFormattedString(formatted, spacePadding, zeroPadding, isNegative, isSpacePadded, this.hasPlusFlag,
this.useAlternativeFormat, this.format);
}

@TruffleBoundary
private static byte[] getFormattedString(String formatted, int spacePadding, int zeroPadding, boolean isNegative,
boolean isSpacePadded, boolean hasPlusFlag) {
boolean isSpacePadded, boolean hasPlusFlag, boolean useAlternativeFormat,
char format) {
if (isNegative && !(isSpacePadded || hasPlusFlag)) {
if (formatted.contains("0")) {
formatted = formatted.substring(formatted.indexOf('0'), formatted.length());
@@ -82,6 +87,14 @@ private static byte[] getFormattedString(String formatted, int spacePadding, int
}
}

if (useAlternativeFormat) {
if(format == 'B'){
formatted = "0B" + formatted;
} else {
formatted = "0b" + formatted;
}
}

if (isSpacePadded || hasPlusFlag) {
if (isNegative) {
formatted = "-" + formatted;
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
int spacePadding = DEFAULT;
int zeroPadding = DEFAULT;
boolean hasPlusFlag = false;
boolean useAlternativeFormat = false;

for (int n = 0; n < ctx.flag().size(); n++) {
final PrintfParser.FlagContext flag = ctx.flag(n);
@@ -110,6 +111,8 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
// Handled in space and zero, above
} else if (flag.PLUS() != null) {
hasPlusFlag = true;
} else if (flag.HASH() != null) {
useAlternativeFormat = true;
} else {
throw new UnsupportedOperationException();
}
@@ -216,7 +219,7 @@ public void exitFormat(PrintfParser.FormatContext ctx) {

if(type == 'b' || type == 'B'){
node = WriteBytesNodeGen.create(context,
FormatIntegerBinaryNodeGen.create(context, format, hasPlusFlag,
FormatIntegerBinaryNodeGen.create(context, format, hasPlusFlag, useAlternativeFormat,
spacePaddingNode,
zeroPaddingNode,
ToIntegerNodeGen.create(context, valueNode)));

0 comments on commit 8ef7da7

Please sign in to comment.