Re: String encoding problem in R wrappers on Windows

William S Fulton <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftAcxxDeFJTX5AwO4UipxiXSm965EYXThEWnfMu-xBmqqg@mail.gmail.com>
The code in question looks like it comes from the 'out' typemap for char *,
so you are probably wrapping a function returning a char *:

%typemap(out,noblock=1) char *
 {  $result = $1 ? Rf_mkString(%reinterpret_cast($1,char *)) : R_NilValue; }

Just add in your own version with the code you want (your code doesn't
compile for me, but here it is converted into a typemap:)

%typemap(out,noblock=1) char *
// {  $result = $1 ? Rf_mkString(%reinterpret_cast($1,char *)) :
R_NilValue; }
{
  if ( $1 ) {
    PROTECT($result = Rf_mkCharCE((char *)($1), CE_UTF8));
    $result = Rf_mkString($result);
    UNPROTECT(1);
  } else {
    $result = R_NilValue;
  }
}

William



On Thu, 30 Jul 2020 at 21:42, Kevin Smith <[email protected]>
wrote:

> I’m wrapping a library that uses all UTF-8 strings as results.  I noticed
> recently that non-ASCII characters on Windows contained incorrect
> characters.  I tracked it down to the use of Rf_mkString in the wrappers
> which I’m assuming must think the C string is encoded in the native
> encoding for the platform, not necessarily UTF-8.  The lines looks like:
>
>   r_ans = result ? Rf_mkString(result) : R_NilValue;
>
> I’m not sure how to make the wrappers work properly with UTF-8 result
> strings though.  The only thing I can think of is to post-process the
> wrappers to generate code like the following.  It forces the encoding to
> UTF-8.  This approach seems pretty heavy-handed though and I was wondering
> if there was a better solution.
>
>   if ( result ) {
>     PROTECT(r_ans = Rf_mkCharCE((char *)(result), CE_UTF8));
>     r_ans = Rf_mkString(r_ans);
>     UNPROTECT(1);
>   } else {
>     r_ans = R_NilValue;
>   }
>
> Kevin Smith
> [email protected]
>
>
>
>
>
>
> _______________________________________________
> Swig-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.