Skip to content

Commit

Permalink
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/string/modulo_tags.txt
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@ fails:String#% raises a TypeError if #to_ary does not return an Array
fails:String#% tries to convert the argument to Array by calling #to_ary
fails:String#% doesn't return subclass instances when called on a subclass
fails:String#% always taints the result when the format string is tainted
fails:String#% supports binary formats using %b for positive numbers
fails:String#% supports binary formats using %b for negative numbers
fails:String#% supports character formats using %c
fails:String#% supports single character strings as argument for %c
Original file line number Diff line number Diff line change
@@ -32,12 +32,17 @@ public abstract class FormatIntegerBinaryNode extends FormatNode {
private final char format;
private final boolean hasPlusFlag;
private final boolean useAlternativeFormat;
private final boolean isLeftJustified;
private final boolean hasSpaceFlag;

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

@Specialization
@@ -49,7 +54,7 @@ public byte[] format(int spacePadding,
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,
this.useAlternativeFormat, this.format);
this.useAlternativeFormat, this.format, this.isLeftJustified, this.hasSpaceFlag);
}

@TruffleBoundary
@@ -61,13 +66,17 @@ public byte[] format(int spacePadding, int zeroPadding, DynamicObject value) {
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,
this.useAlternativeFormat, this.format);
this.useAlternativeFormat, this.format, this.isLeftJustified, this.hasSpaceFlag);
}

@TruffleBoundary
private static byte[] getFormattedString(String formatted, int spacePadding, int zeroPadding, boolean isNegative,
boolean isSpacePadded, boolean hasPlusFlag, boolean useAlternativeFormat,
char format) {
char format, boolean leftJustified, boolean hasSpaceFlag) {
final boolean isLeftJustified = leftJustified || spacePadding < 0;
if(spacePadding < 0){
spacePadding = -spacePadding;
}
if (isNegative && !(isSpacePadded || hasPlusFlag)) {
if (formatted.contains("0")) {
formatted = formatted.substring(formatted.indexOf('0'), formatted.length());
@@ -95,7 +104,7 @@ private static byte[] getFormattedString(String formatted, int spacePadding, int
}
}

if (isSpacePadded || hasPlusFlag) {
if (hasSpaceFlag || hasPlusFlag) {
if (isNegative) {
formatted = "-" + formatted;
} else {
@@ -104,7 +113,14 @@ private static byte[] getFormattedString(String formatted, int spacePadding, int
} else {
formatted = " " + formatted;
}
}
}

while (formatted.length() < spacePadding) {
if(isLeftJustified){
formatted = formatted + " ";
} else {
formatted = " " + formatted;
}
}

Original file line number Diff line number Diff line change
@@ -87,6 +87,7 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
int spacePadding = DEFAULT;
int zeroPadding = DEFAULT;
boolean hasPlusFlag = false;
boolean hasSpaceFlag = false;
boolean useAlternativeFormat = false;
int absoluteArgumentIndex = DEFAULT;

@@ -96,12 +97,11 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
if (flag.MINUS() != null) {
leftJustified = true;
} else if (flag.SPACE() != null) {
hasSpaceFlag = true;
if (n + 1 < ctx.flag().size() && ctx.flag(n + 1).STAR() != null) {
spacePadding = PADDING_FROM_ARGUMENT;
} else if(width != DEFAULT) {
spacePadding = width;
} else {
spacePadding = 1;
}
} else if (flag.ZERO() != null) {
if (n + 1 < ctx.flag().size() && ctx.flag(n + 1).STAR() != null) {
@@ -110,7 +110,7 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
zeroPadding = width;
}
} else if (flag.STAR() != null) {
// Handled in space and zero, above
spacePadding = PADDING_FROM_ARGUMENT;
} else if (flag.PLUS() != null) {
hasPlusFlag = true;
} else if (flag.HASH() != null) {
@@ -226,6 +226,8 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
if(type == 'b' || type == 'B'){
node = WriteBytesNodeGen.create(context,
FormatIntegerBinaryNodeGen.create(context, format, hasPlusFlag, useAlternativeFormat,
leftJustified,
hasSpaceFlag,
spacePaddingNode,
zeroPaddingNode,
ToIntegerNodeGen.create(context, valueNode)));

0 comments on commit 4cb7c63

Please sign in to comment.