From ripley at stats.ox.ac.uk  Thu Aug  7 17:20:13 2008
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu, 7 Aug 2008 16:20:13 +0100 (BST)
Subject: [Rd] string comparison
In-Reply-To: <D894FE1B-FBCD-4A57-B8F4-A36AD8B305A5@epfl.ch>
References: <D894FE1B-FBCD-4A57-B8F4-A36AD8B305A5@epfl.ch>
Message-ID: <alpine.LFD.1.10.0808071614551.26106@gannet.stats.ox.ac.uk>

It is not safe to rely on pointer comparisons until R 2.8.0.  Currently it 
is easy to get CHARSXPs that are not in the cache: it is not impossible in 
2.8.0, but you have to know how to 'break and enter'.

In your code you are comparing a CHARSXP and a SYMSXP.  You want to 
use charChance = mkChar("chance") and then make it precious 
(R_PreciousObject).

On Thu, 7 Aug 2008, Kjell Konis wrote:

> Hi,
>
> I have a package where I do things like this a lot:
>
> if(strncmp(CHAR(asChar(Scategory)), "chance", 6) == 0)
>   category = h_category_chance;
> else if(strncmp(CHAR(asChar(Scategory)), "utility", 7) == 0)
>   category = h_category_utility;
> else if(strncmp(CHAR(asChar(Scategory)), "decision", 8) == 0)
>   category = h_category_decision;
> else if(strncmp(CHAR(asChar(Scategory)), "instance", 8) == 0)
>   category = h_category_instance;
> else
>   category = h_category_error;
>
> I would like to replace the strncmps with pointer comparisons so I set up 
> some global variables in the R_init_myDLL function like this:
>
> charChance = install("chance")
> <etc.>
>
> but I can't seem to get the comparison to work. I want to do something like
>
> if(CHAR(asChar(Scategory)) == charChance)
>   category = h_category_chance;
>
> but category always ends up being h_category_error. Any pointers would be 
> appreciated.
>
> Thanks,
> Kjell
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Brian D. Ripley,                  ripley at 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] string comparison
Prof Brian Ripley