Created
March 26, 2015 21:58
-
-
Save jjallaire/008622dabd5730cf74cf to your computer and use it in GitHub Desktop.
UTF8 CharacterVector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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