@@ -202,16 +202,11 @@ private static int maxLengthAndConvertToScalar(Object[] values) {
202
202
for (int i = 0 ; i < values .length ; i ++) {
203
203
if (values [i ] instanceof RAbstractVector ) {
204
204
int vecLength = ((RAbstractVector ) values [i ]).getLength ();
205
- if (vecLength == 0 ) {
206
- // result will be empty character vector in this case, as in:
207
- // sprintf("%d %d", as.integer(c(7,42)), integer())
208
- return 0 ;
209
- } else {
210
- if (vecLength == 1 ) {
211
- values [i ] = ((RAbstractVector ) values [i ]).getDataAtAsObject (0 );
212
- }
213
- length = Math .max (vecLength , length );
205
+ assert vecLength != 0 ;
206
+ if (vecLength == 1 ) {
207
+ values [i ] = ((RAbstractVector ) values [i ]).getDataAtAsObject (0 );
214
208
}
209
+ length = Math .max (vecLength , length );
215
210
} else {
216
211
length = Math .max (1 , length );
217
212
}
@@ -231,12 +226,12 @@ private static Object[] createSprintfArgs(Object[] values, int index, int maxLen
231
226
return sprintfArgs ;
232
227
}
233
228
234
- @ Specialization (guards = {"!oneElement(args)" , "hasNull (args)" })
229
+ @ Specialization (guards = {"!oneElement(args)" , "hasNullOrEmptyVec (args)" })
235
230
protected RStringVector sprintf (@ SuppressWarnings ("unused" ) Object fmt , @ SuppressWarnings ("unused" ) RArgsValuesAndNames args ) {
236
231
return RDataFactory .createEmptyStringVector ();
237
232
}
238
233
239
- @ Specialization (guards = {"!oneElement(args)" , "!hasNull (args)" })
234
+ @ Specialization (guards = {"!oneElement(args)" , "!hasNullOrEmptyVec (args)" })
240
235
@ TruffleBoundary
241
236
protected RStringVector sprintf (String fmt , RArgsValuesAndNames args ) {
242
237
Object [] values = args .getArguments ();
@@ -267,7 +262,7 @@ protected Object sprintfOneElement(String fmt, RArgsValuesAndNames args) {
267
262
return sprintfRecursive .executeObject (fmt , args .getArgument (0 ));
268
263
}
269
264
270
- @ Specialization (guards = {"!oneElement(args)" , "!hasNull (args)" })
265
+ @ Specialization (guards = {"!oneElement(args)" , "!hasNullOrEmptyVec (args)" })
271
266
@ TruffleBoundary
272
267
protected RStringVector sprintf (RStringVector fmt , RArgsValuesAndNames args ) {
273
268
if (fmt .getLength () == 0 ) {
@@ -276,8 +271,15 @@ protected RStringVector sprintf(RStringVector fmt, RArgsValuesAndNames args) {
276
271
String [] data = new String [fmt .getLength ()];
277
272
for (int i = 0 ; i < data .length ; i ++) {
278
273
RStringVector formatted = sprintf (fmt .getDataAt (i ), args );
279
- assert formatted .getLength () > 0 ;
280
- data [i ] = formatted .getDataAt (args .getLength () == 0 ? 0 : i % Math .min (args .getLength (), formatted .getLength ()));
274
+ if (args .getLength () == 0 ) {
275
+ if (formatted .getLength () == 0 ) {
276
+ data [i ] = null ;
277
+ } else {
278
+ data [i ] = formatted .getDataAt (0 );
279
+ }
280
+ } else {
281
+ data [i ] = formatted .getDataAt (i % Math .min (args .getLength (), formatted .getLength ()));
282
+ }
281
283
}
282
284
return RDataFactory .createStringVector (data , RDataFactory .COMPLETE_VECTOR );
283
285
}
@@ -739,10 +741,15 @@ protected boolean oneElement(RArgsValuesAndNames args) {
739
741
return args .getLength () == 1 ;
740
742
}
741
743
742
- protected boolean hasNull (RArgsValuesAndNames args ) {
744
+ protected boolean hasNullOrEmptyVec (RArgsValuesAndNames args ) {
743
745
for (int i = 0 ; i < args .getLength (); i ++) {
744
746
if (args .getArgument (i ) == RNull .instance ) {
745
747
return true ;
748
+ } else if (args .getArgument (i ) instanceof RAbstractVector ) {
749
+ RAbstractVector vector = (RAbstractVector ) args .getArgument (i );
750
+ if (vector .getLength () == 0 ) {
751
+ return true ;
752
+ }
746
753
}
747
754
}
748
755
0 commit comments