-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
Sent: Thursday, March 19, 2009 3:34 AM
To: William Dunlap
Cc: r-devel at r-project.org
Subject: Re: [Rd] sprintf("%d", integer(0)) aborts
On Wed, 18 Mar 2009, William Dunlap wrote:
In R's sprintf() if any of the arguments has length 0
the function aborts. E.g.,
> sprintf("%d", integer(0))
Error in sprintf("%d", integer(0)) : zero-length argument
> sprintf(character(), integer(0))
Error in sprintf(character(), integer(0)) :
'fmt' is not a non-empty character vector
This comes up in code like
x[nchar(x)==0] <- sprintf("No. %d", seq_along(x)[nchar(x)==0])
which works if x contains any empty strings
x<-c("One","Two","") # changes "" -> "No. 3"
but not if it doesn't
x<-c("One","Two","Three") # throws error instead of doing nothing
When I wrote S+'s sprintf() I had it act like the binary
arithmetic operators, returning a zero long result if any
argument were zero long. (Otherwise its result is as long
as the longest input.) I think it would be nice if R's
sprintf did this also.
Currently you must add defensive code (if (any(nchar(x)==0))...)
to make functions using sprintf to work in all cases and that
muddies up the code and slows things down.
Do you think this is a reasonable thing to do? I've attached
a possible patch to src/main/sprintf.c makes the examples above
return character(0).
Yes. It was deliberate that it works (and is documented) the way it
is, and I've not previously seen any problematic examples.