Skip to content

Commit 50b1ce8

Browse files
committed
fix sprintf WIP
1 parent 546e7c0 commit 50b1ce8

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ FormatSpecifier parseSpecifier() {
270270

271271
spec.endPos = pos;
272272
spec.raw = input.substring(spec.startPos, spec.endPos);
273-
// // ADD THIS DEBUG LINE:
274-
// System.err.println("DEBUG: spec.raw='" + spec.raw + "', vectorFlag=" + spec.vectorFlag + ", widthFromArg=" + spec.widthFromArg + ", conversionChar='" + spec.conversionChar + "'");
273+
274+
// Add debug here to check the state before returning
275+
System.err.println("DEBUG: Before return - isValid=" + spec.isValid +
276+
", errorMessage='" + spec.errorMessage + "'");
275277

276278
// Mark as invalid if we found spaces in the format
277279
if (hasInvalidSpace) {
@@ -282,6 +284,10 @@ FormatSpecifier parseSpecifier() {
282284
validateSpecifier(spec);
283285
}
284286

287+
// Add debug after validation
288+
System.err.println("DEBUG: After validation - isValid=" + spec.isValid +
289+
", errorMessage='" + spec.errorMessage + "'");
290+
285291
return spec;
286292
}
287293

@@ -297,8 +303,9 @@ Integer parseNumber() {
297303
}
298304

299305
void validateSpecifier(FormatSpecifier spec) {
300-
// // ADD THIS DEBUG LINE:
301-
// System.err.println("DEBUG: Validating spec: raw='" + spec.raw + "', vectorFlag=" + spec.vectorFlag + ", widthFromArg=" + spec.widthFromArg);
306+
System.err.println("DEBUG validateSpecifier: raw='" + spec.raw +
307+
"', vectorFlag=" + spec.vectorFlag +
308+
", conversionChar='" + spec.conversionChar + "'");
302309
// Special case: %*v formats are valid
303310
if (spec.vectorFlag && spec.widthFromArg) {
304311
// This is a valid vector format with custom separator
@@ -313,11 +320,14 @@ void validateSpecifier(FormatSpecifier spec) {
313320
return;
314321
}
315322

316-
// Check for vector formats BEFORE other validations
323+
// Check for vector formats FIRST (before %n check)
317324
if (spec.vectorFlag) {
318325
// Vector flag is only valid with certain conversions
319326
String validVectorConversions = "diouxXbBs";
327+
System.err.println("DEBUG: Checking vector conversion '" + spec.conversionChar +
328+
"' in '" + validVectorConversions + "'");
320329
if (validVectorConversions.indexOf(spec.conversionChar) < 0) {
330+
System.err.println("DEBUG: Setting invalid for vector format");
321331
spec.isValid = false;
322332
spec.errorMessage = "INVALID";
323333
return;
@@ -375,17 +385,6 @@ void validateSpecifier(FormatSpecifier spec) {
375385
}
376386
}
377387

378-
// Validate vector flag combinations
379-
if (spec.vectorFlag) {
380-
// Vector flag is only valid with certain conversions
381-
String validVectorConversions = "diouxXbBs";
382-
if (validVectorConversions.indexOf(spec.conversionChar) < 0) {
383-
spec.isValid = false;
384-
spec.errorMessage = "INVALID";
385-
return;
386-
}
387-
}
388-
389388
// Validate flag combinations
390389
validateFlags(spec);
391390

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public static RuntimeScalar sprintf(RuntimeScalar runtimeScalar, RuntimeList lis
5050
result.append(literal);
5151
charsWritten += literal.length();
5252
} else if (element instanceof SprintfFormatParser.FormatSpecifier spec) {
53+
// Check if spec is invalid FIRST
54+
if (!spec.isValid) {
55+
String formatted = processFormatSpecifier(spec, list, argIndex, formatter);
56+
result.append(formatted);
57+
charsWritten += formatted.length();
58+
continue;
59+
}
60+
5361
if (spec.conversionChar == 'n') {
5462
// %n doesn't produce output, but does consume an argument
5563
handlePercentN(spec, list, argIndex);
@@ -70,7 +78,7 @@ public static RuntimeScalar sprintf(RuntimeScalar runtimeScalar, RuntimeList lis
7078

7179
// Update argument index if not using positional parameters
7280
// BUT don't update if the specifier was invalid
73-
if (spec.parameterIndex == null && spec.conversionChar != '%' && spec.isValid) {
81+
if (spec.parameterIndex == null && spec.conversionChar != '%') {
7482
argIndex = updateArgIndex(spec, argIndex);
7583
}
7684
}
@@ -330,6 +338,8 @@ private static int updateArgIndex(SprintfFormatParser.FormatSpecifier spec,
330338
* Handle invalid format specifiers.
331339
*/
332340
private static String handleInvalidSpecifier(SprintfFormatParser.FormatSpecifier spec) {
341+
System.err.println("DEBUG handleInvalidSpecifier: raw='" + spec.raw +
342+
"', errorMessage='" + spec.errorMessage + "'");
333343
String formatOnly = spec.raw;
334344
String trailing = "";
335345

0 commit comments

Comments
 (0)