Skip to content

Commit bea7272

Browse files
committed
fix sprintf WIP
1 parent ad547d8 commit bea7272

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/main/java/org/perlonjava/operators/SprintfOperator.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,35 @@ public static RuntimeScalar sprintf(RuntimeScalar runtimeScalar, RuntimeList lis
148148
}
149149
// System.err.println("DEBUG sprintf: before MISSING check - valueArgIndex=" + valueArgIndex + ", list.size=" + list.size() + ", vectorFlag=" + spec.vectorFlag);
150150
if (valueArgIndex >= list.size()) {
151-
if (spec.conversionChar == 'n') {
152-
// %n is a no-op for now - just continue without throwing
153-
continue;
154-
}
155-
156-
// Different formats have different MISSING patterns
151+
// Generate warning
152+
WarnDie.warn(new RuntimeScalar("Missing argument in sprintf"), new RuntimeScalar(""));
153+
154+
// Append appropriate default value directly to result
157155
if (spec.conversionChar == 'f' || spec.conversionChar == 'F') {
158-
// Check the specific format
159-
if (spec.raw.matches("%.0f")) {
160-
result.append("0 MISSING");
161-
} else if (spec.raw.matches(" %.0f")) {
162-
result.append(" 0 MISSING");
163-
} else if (spec.raw.matches("%.2f")) {
164-
result.append("0.00 MISSING");
156+
if (spec.precision >= 0) {
157+
result.append(String.format("%." + spec.precision + "f", 0.0));
165158
} else {
166-
result.append(" MISSING");
159+
result.append("0.000000"); // Default precision is 6
167160
}
168161
} else if (spec.conversionChar == 'g' || spec.conversionChar == 'G') {
169-
if (spec.raw.matches("%.0g")) {
170-
result.append("0 MISSING");
171-
} else if (spec.raw.matches(" %.0g")) {
172-
result.append(" 0 MISSING");
173-
} else if (spec.raw.matches("%.2g")) {
174-
result.append("0 MISSING");
162+
if (spec.precision == 0) {
163+
result.append("0");
175164
} else {
176-
result.append(" MISSING");
165+
result.append("0"); // %g removes trailing zeros
177166
}
167+
} else if (spec.conversionChar == 'd' || spec.conversionChar == 'i' ||
168+
spec.conversionChar == 'u' || spec.conversionChar == 'o' ||
169+
spec.conversionChar == 'x' || spec.conversionChar == 'X') {
170+
result.append("0");
171+
} else if (spec.conversionChar == 's') {
172+
result.append(""); // Empty string for %s
173+
} else if (spec.conversionChar == 'c') {
174+
result.append("\0"); // Null character for %c
178175
} else {
179-
result.append(" MISSING");
176+
result.append(""); // Default empty
180177
}
181-
continue;
178+
179+
continue; // Skip to next format specifier
182180
}
183181

184182
RuntimeScalar value = (RuntimeScalar) list.elements.get(valueArgIndex);

0 commit comments

Comments
 (0)