@@ -103,7 +103,8 @@ public static RuntimeScalar sprintf(RuntimeScalar runtimeScalar, RuntimeList lis
103
103
}
104
104
105
105
if (widthArgIndex >= list .size ()) {
106
- result .append (" MISSING" );
106
+ WarnDie .warn (new RuntimeScalar ("Missing argument in sprintf" ), new RuntimeScalar ("" ));
107
+ width = 0 ; // Use default width
107
108
continue ;
108
109
}
109
110
width = ((RuntimeScalar ) list .elements .get (widthArgIndex )).getInt ();
@@ -128,7 +129,8 @@ public static RuntimeScalar sprintf(RuntimeScalar runtimeScalar, RuntimeList lis
128
129
}
129
130
130
131
if (precArgIndex >= list .size ()) {
131
- result .append (" MISSING" );
132
+ WarnDie .warn (new RuntimeScalar ("Missing argument in sprintf" ), new RuntimeScalar ("" ));
133
+ precision = -1 ; // Use default precision
132
134
continue ;
133
135
}
134
136
precision = ((RuntimeScalar ) list .elements .get (precArgIndex )).getInt ();
@@ -150,16 +152,13 @@ public static RuntimeScalar sprintf(RuntimeScalar runtimeScalar, RuntimeList lis
150
152
if (valueArgIndex >= list .size ()) {
151
153
// Generate warning
152
154
WarnDie .warn (new RuntimeScalar ("Missing argument in sprintf" ), new RuntimeScalar ("" ));
153
-
155
+
154
156
// Append appropriate default value directly to result
155
157
if (spec .conversionChar == 'f' || spec .conversionChar == 'F' ) {
156
- if (spec .precision >= 0 ) {
157
- result .append (String .format ("%." + spec .precision + "f" , 0.0 ));
158
- } else {
159
- result .append ("0.000000" ); // Default precision is 6
160
- }
158
+ int prec = (spec .precision != null ) ? spec .precision : 6 ; // Default precision is 6
159
+ result .append (String .format ("%." + prec + "f" , 0.0 ));
161
160
} else if (spec .conversionChar == 'g' || spec .conversionChar == 'G' ) {
162
- if (spec .precision == 0 ) {
161
+ if (spec .precision != null && spec . precision == 0 ) {
163
162
result .append ("0" );
164
163
} else {
165
164
result .append ("0" ); // %g removes trailing zeros
@@ -200,11 +199,32 @@ public static RuntimeScalar sprintf(RuntimeScalar runtimeScalar, RuntimeList lis
200
199
precision , spec .conversionChar );
201
200
}
202
201
result .append (formatted );
203
-
204
202
} catch (Exception e ) {
205
- // Reset arg index and append error
203
+ // Reset arg index and generate warning
206
204
argIndex = savedArgIndex ;
207
- result .append (" MISSING" );
205
+ WarnDie .warn (new RuntimeScalar ("Missing argument in sprintf" ), new RuntimeScalar ("" ));
206
+
207
+ // Append appropriate default value based on spec.conversionChar
208
+ if (spec .conversionChar == 'f' || spec .conversionChar == 'F' ) {
209
+ int prec = (spec .precision != null ) ? spec .precision : 6 ; // Default 6
210
+ result .append (String .format ("%." + prec + "f" , 0.0 ));
211
+ } else if (spec .conversionChar == 'g' || spec .conversionChar == 'G' ) {
212
+ if (spec .precision != null && spec .precision == 0 ) {
213
+ result .append ("0" );
214
+ } else {
215
+ result .append ("0" ); // %g removes trailing zeros
216
+ }
217
+ } else if (spec .conversionChar == 'd' || spec .conversionChar == 'i' ||
218
+ spec .conversionChar == 'u' || spec .conversionChar == 'o' ||
219
+ spec .conversionChar == 'x' || spec .conversionChar == 'X' ) {
220
+ result .append ("0" );
221
+ } else if (spec .conversionChar == 's' ) {
222
+ result .append ("" ); // Empty string for %s
223
+ } else if (spec .conversionChar == 'c' ) {
224
+ result .append ("\0 " ); // Null character for %c
225
+ } else {
226
+ result .append ("" ); // Default empty
227
+ }
208
228
}
209
229
}
210
230
}
0 commit comments