Skip to content

Commit

Permalink
Showing 3 changed files with 9 additions and 26 deletions.
1 change: 1 addition & 0 deletions spec/truffle/tags/core/string/modulo_tags.txt
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ 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,17 +32,12 @@ 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,
boolean isLeftJustified, boolean hasSpaceFlag) {
public FormatIntegerBinaryNode(RubyContext context, char format, boolean hasPlusFlag, boolean useAlternativeFormat) {
super(context);
this.format = format;
this.hasPlusFlag = hasPlusFlag;
this.useAlternativeFormat = useAlternativeFormat;
this.isLeftJustified = isLeftJustified;
this.hasSpaceFlag = hasSpaceFlag;
}

@Specialization
@@ -54,7 +49,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.isLeftJustified, this.hasSpaceFlag);
this.useAlternativeFormat, this.format);
}

@TruffleBoundary
@@ -66,17 +61,13 @@ 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.isLeftJustified, this.hasSpaceFlag);
this.useAlternativeFormat, this.format);
}

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

if (hasSpaceFlag || hasPlusFlag) {
if (isSpacePadded || hasPlusFlag) {
if (isNegative) {
formatted = "-" + formatted;
} else {
@@ -113,14 +104,7 @@ 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,7 +87,6 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
int spacePadding = DEFAULT;
int zeroPadding = DEFAULT;
boolean hasPlusFlag = false;
boolean hasSpaceFlag = false;
boolean useAlternativeFormat = false;
int absoluteArgumentIndex = DEFAULT;

@@ -97,11 +96,12 @@ 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) {
spacePadding = PADDING_FROM_ARGUMENT;
// Handled in space and zero, above
} else if (flag.PLUS() != null) {
hasPlusFlag = true;
} else if (flag.HASH() != null) {
@@ -226,8 +226,6 @@ 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 ad1edc8

Please sign in to comment.