From murdoch.duncan at gmail.com  Thu Mar 27 22:12:29 2014
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Thu, 27 Mar 2014 17:12:29 -0400
Subject: [Rd] internal string comparison (Scollate)
In-Reply-To: <CAJXgQP3sVi8wKo8tdu_8xckCPj7=Goh9SZC90_-WKiMwVSOHHA@mail.gmail.com>
References: <D3531066-EF05-4C8D-8B83-581C7F042D0D@r-enthusiasts.com>	<21299.1377.790733.398726@max.nulle.part>	<CADwqtCOwJCCWYVEk5KHs1auzyBKr1iL1OdDxpCMwV0rAHLEdPA@mail.gmail.com>	<C2D5C840-B9A6-430C-B16F-8ED59460516B@r-enthusiasts.com>	<21300.26905.645966.939976@max.nulle.part>
	<CAJXgQP3sVi8wKo8tdu_8xckCPj7=Goh9SZC90_-WKiMwVSOHHA@mail.gmail.com>
Message-ID: <5334943D.1020901@gmail.com>

On 14-03-27 3:01 PM, Kevin Ushey wrote:
> I too think it would be useful if R exported some version of its
> string sorting routines, since sorting strings while respecting
> locale, and doing so in a portable fashion while respecting the user's
> environment, is not trivial. R holds a fast, portable, well-tested
> solution, and I think package developers would be very appreciative if
> some portion of this was exposed at the C level.

It does.  You can put your strings in an R STRSXP vector, and call the R 
sort function on it.

The usual objection to constructing an R expression and evaluating it is 
that it is slow, but if you are talking about sorting, the time spent in 
the sort is likely to dominate the time spent in the setup.

>
> If not `Scollate`, then perhaps other candidates could be the more
> generic `sortVector`, or the more string-specific (and NA-respecting)
> `scmp`.

Evaluating an R expression gives you sortVector.

I can see an argument for Scollate being useful (sorting isn't the only 
reason to compare strings), but I can see arguments against exposing it 
too.  Take a look at the source:  it needs to be used carefully.  In 
particular, it can return a 0 for unequal strings, and users are likely 
to get messed up by that, or to submit bogus bug reports.  And it's not 
impossible to work around: if you can collect the universe of strings to 
compare in advance, then just use order() to convert them to integer 
values, and compare those.

Duncan Murdoch

>
> I understand that the volunteers at R Core have limited time and
> resources, and exposing an API imposes additional maintenance burdens
> on an already thinly stretched team, but this is a situation where the
> R users and package authors alike could benefit. Or, if there are
> other reasons why exporting such routines is not possible nor
> recommended, it would be very informative to know why.
>
> Thanks,
> Kevin
>
> On Thu, Mar 27, 2014 at 11:08 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>>
>> On 26 March 2014 at 19:09, Romain Fran?ois wrote:
>> | That's one part of the problem. Indeed I'd rather use something rather than
>> | copy and paste it and run the risk of being outdated. The answer to that is
>>
>> We all would. But "they" won't let us by refusing to create more API access points.
>>
>> | testing though. I can develop a test suite that can let me know I'm out of
>>
>> Correct.
>>
>> | date and I need to copy and paste some new code, etc ... Done that before, this
>> | is tedious, but so what.
>> |
>> | The other part of the problem (the real part of the problem actually) is that,
>> | at least when R is built with ICU support, Scollate will depend on a the
>> | collator pointer in util.c
>> | https://github.com/wch/r-source/blob/trunk/src/main/util.c#L1777
>> |
>> | And this can be controlled by the base::icuSetCollate function. Of course the
>> | collator pointer is not public.
>>
>> So the next (and even less pleasant) answer is to build a new package which
>> links to, (or worse yet, embeds) libicu.
>>
>> As you want ICU behaviour, you will need ICU code.
>>
>> Dirk
>>
>> --
>> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] internal string comparison (Scollate)
Duncan Murdoch