Re: [Rd] sub('^', .....) bugs (PR#7742)

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 23 Mar 2005 - 09:07:47 GMT

On Wed, 23 Mar 2005 maechler@stat.math.ethz.ch wrote:

> 1) In your cases, the integer 'x' argument is auto-coerced to
> character, however that fails as soon as 'perl = TRUE' is used.
>
> > sub('^','v_', 1:3, perl=TRUE)
> Error in sub.perl(pattern, replacement, x, ignore.case) :
> invalid argument
>
> {one can argue that this is not a bug, since the help file asks
> for 'x' to be a character vector; OTOH, we have
> as.character(.) magic in many other places, i.e. quite
> naturally here;
> at least perl=TRUE and perl=FALSE should behave consistently.}

I believe the bug is in the PERL=FALSE case. This coercion is undocumented, and e.g.

> grep('^1', 1:3, perl=TRUE)

Error in grep.perl(pattern, x, ignore.case, value, useBytes) :

         invalid argument
> grep('^1', 1:3, perl=FALSE)

Error in grep(pattern, x, ignore.case, extended, value, fixed, useBytes) :

         invalid argument

do not accept non-character arguments. The only one that does AFAICS is [g]sub(perl=FALSE), and the other functions like tolower, substr, strsplit, chartr, agrep do not.

The consistent thing to do seems to be to remove the anomalous coercion. Otherwise we need to at least change grep and regexpr.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed Mar 23 20:13:01 2005

This archive was generated by hypermail 2.1.8 : Mon 24 Oct 2005 - 22:26:31 GMT