Skip to content

Instantly share code, notes, and snippets.

@jjallaire
Created March 26, 2015 21:58
Show Gist options
  • Save jjallaire/008622dabd5730cf74cf to your computer and use it in GitHub Desktop.
Save jjallaire/008622dabd5730cf74cf to your computer and use it in GitHub Desktop.
UTF8 CharacterVector
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
CharacterVector asUTF8(CharacterVector x) {
const void *vmax = vmaxget();
CharacterVector xUTF8(x.length());
for (int i = 0; i<x.length(); i++) {
const char* utf8 = Rf_translateCharUTF8(x[i]);
xUTF8[i] = Rf_mkCharCE(utf8, CE_UTF8);
}
vmaxset(vmax);
return xUTF8;
}
// [[Rcpp::export]]
std::string asUTF8String(CharacterVector x, int i) {
if (i > x.length())
stop("Invalid index specified");
std::string strUTF8;
const void *vmax = vmaxget();
strUTF8 = std::string(Rf_translateCharUTF8(x[i-1]));
vmaxset(vmax);
return strUTF8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment