From ripley at stats.ox.ac.uk  Wed Mar 23 10:07:47 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed Mar 23 10:07:59 2005
Subject: [Rd] sub('^', .....) bugs (PR#7742)
In-Reply-To: <20050323075601.0C916BAF4@slim.kubism.ku.dk>
References: <20050323075601.0C916BAF4@slim.kubism.ku.dk>
Message-ID: <Pine.LNX.4.61.0503230900490.31124@gannet.stats>

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


[Rd] sub('^', .....) bugs (PR#7742)
Prof Brian Ripley