Skip to content
Prev 36143 / 63421 Next

duplicate STRSXP : shallow copy ?

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