Re: [Rd] wchar and wstring.

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sat 27 Aug 2005 - 11:33:15 GMT

On Fri, 26 Aug 2005, James Bullard wrote:

> Hello all, I am writing an R interface to some C++ files which make use
> of std::wstring classes for internationalization. Previously (when I
> wanted to make R strings from C++ std::strings), I would do something
> like this to construct a string in R from the results of the parse.
>
> SET_VECTOR_ELT(vals, i++, mkString(header.GetHeader().c_str()));

That creates a list of one-element character vectors. It would be more usual to do

   SET_STRING_ELT(vals, i++, mkChar(header.GetHeader().c_str()));

> However, now the call header.GetHeader().c_str() returns a pointer to an
> array of wchar_t's. I was going to use wcstombs() to convert the
> wchar_t* to char*, but I wanted to see if there was a similar function
> in R for the mkString function which I had initially used which deals
> with wchar_ts as opposed to chars.

No (nor an analogue of mkChar). R uses MBCS and not wchar_t internally (and Unix-alike systems do externally). There is no wchar_t internal R type (a much-debated design decision at the time).

> Also, since I have no experience with the wctombs() function I wanted to
> ask if anyone knew if this will handle the internationilzation issues
> from within R.

Did you mean wcstombs or wctomb (if the latter, wcrtomb is preferred)? There are tens of examples in the R sources for you to consult.

Note that not all R platforms support wchar_t, hence this code is surrounded by #ifdef SUPPORT_MBCS macros (exported in Rconfig.h for package writers).

-- 
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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat Aug 27 21:39:23 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:19 GMT