Re: [Rd] duplicate STRSXP : shallow copy ?

From: Romain Francois <romain_at_r-enthusiasts.com>
Date: Sun, 07 Mar 2010 12:01:20 +0100

Hello,

I understand now that this is very intended after reading: http://cran.r-project.org/doc/manuals/R-ints.html#The-CHARSXP-cache

So we are not supposed to modify a CHARSXP character by character.

Sorry for the noise.

Romain

On 03/07/2010 10:48 AM, Romain Francois wrote:
>
> Hello,
>
> As this little program illustrates, duplicating a STRSXP does not seem
> deep enough.
>
> require( inline )
>
> fx <- cfunction( signature( x = "character"), '
> SEXP y = PROTECT( duplicate( x ) );
> int n = LENGTH(x);
> int nc = 0 ;
> char* p = 0 ;
> for( int i=0; i<n; i++){
> p = (char*)( CHAR( STRING_ELT( y , i ) ) );
> nc = strlen( p ) ;
> for( int j=0; j<nc; j++){
> p[j] = tolower( p[j] ) ;
> }
> }
> SEXP res = PROTECT( allocVector( VECSXP, 2 ) );
> SET_VECTOR_ELT( res, 0, x );
> SET_VECTOR_ELT( res, 1, y );
> UNPROTECT(2) ;
> return res ;
> ', includes = "#include <ctype.h>" )
>
> I get :
>
> > fx( c("Tick", "Tack", "Tock" ) )
> [[1]]
> [1] "tick" "tack" "tock"
>
> [[2]]
> [1] "tick" "tack" "tock"
>
> where I would expect the second element of the list to not be modified.
> Is this intended ?
>
> If not, I can track it down to the DUPLICATE_ATOMIC_VECTOR in
> duplicate.c and submit a patch.
>
> Also, CHARSXP don't seem to be actually duplicated :
>
> case CHARSXP:
> return s;
>
> Romain
>

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sun 07 Mar 2010 - 11:17:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 08 Mar 2010 - 03:30:58 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive