Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Created March 23, 2017 22:05
Show Gist options
  • Save yutannihilation/dd92347c504dcc2ce620ec813505b805 to your computer and use it in GitHub Desktop.
Save yutannihilation/dd92347c504dcc2ce620ec813505b805 to your computer and use it in GitHub Desktop.
cppfile <- tempfile(fileext = ".cpp")
cat("
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
std::string std2std(const std::string &x) {
  return x;
}

// [[Rcpp::export]]
std::string str2std(const String &x) {
  std::string y(x);
  return y;
}

// [[Rcpp::export]]
std::string str2std_utf8(const String &x) {
  String y(x);
  y.set_encoding(CE_UTF8);
  return y;
}

// [[Rcpp::export]]
String str2str(const String &x) {
  std::string y(x);
  return y;
}

// [[Rcpp::export]]
String str2str_utf8(const String &x) {
  String y(x);
  y.set_encoding(CE_UTF8);
  return y;
}
", file = cppfile)
Rcpp::sourceCpp(cppfile)
purrr::invoke_map_chr(list(std2std, str2std, str2std_utf8, str2str, str2str_utf8),
                      "ニンジャスレイヤー")
#> [1] "ニンジャスレイヤー"                                                                 
#> [2] "ニンジャスレイヤー"                                                                 
#> [3] "繝九Φ繧ク繝」繧ケ繝ャ繧、繝、繝シ"                                                        
#> [4] "<U+0083>j<U+0083><U+0093><U+0083>W<U+0083><U+0083><U+0083>X<U+0083><U+008C><U+0083>C<U+0083><U+0084><U+0081>["
#> [5] "ニンジャスレイヤー"
purrr::invoke_map_chr(list(std2std, str2std, str2std_utf8, str2str, str2str_utf8),
                      enc2utf8("ニンジャスレイヤー"))
#> [1] "繝九Φ繧ク繝」繧ケ繝ャ繧、繝、繝シ" "繝九Φ繧ク繝」繧ケ繝ャ繧、繝、繝シ"
#> [3] "繝九Φ繧ク繝」繧ケ繝ャ繧、繝、繝シ" "ニンジャスレイヤー"         
#> [5] "ニンジャスレイヤー"
Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.3.3 (2017-03-06)
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  Japanese_Japan.932          
#>  tz       Asia/Tokyo                  
#>  date     2017-03-24
#> Packages -----------------------------------------------------------------
#>  package   * version     date       source                          
#>  backports   1.0.5       2017-01-18 CRAN (R 3.3.2)                  
#>  devtools    1.12.0.9000 2016-11-30 Github (hadley/devtools@e6df6cf)
#>  digest      0.6.12      2017-01-27 CRAN (R 3.3.2)                  
#>  evaluate    0.10        2016-10-11 CRAN (R 3.3.1)                  
#>  htmltools   0.3.5       2016-03-21 CRAN (R 3.3.1)                  
#>  knitr       1.15.1      2016-11-22 CRAN (R 3.3.2)                  
#>  lazyeval    0.2.0       2016-06-12 CRAN (R 3.3.1)                  
#>  magrittr    1.5         2014-11-22 CRAN (R 3.3.1)                  
#>  memoise     1.0.0       2016-01-29 CRAN (R 3.3.1)                  
#>  pkgbuild    0.0.0.9000  2017-03-19 Github (r-pkgs/pkgbuild@5ed87aa)
#>  pkgload     0.0.0.9000  2017-03-19 Github (r-pkgs/pkgload@3a96cf2) 
#>  purrr       0.2.2       2016-06-18 CRAN (R 3.3.1)                  
#>  Rcpp        0.12.10     2017-03-19 CRAN (R 3.3.3)                  
#>  rmarkdown   1.3         2016-12-21 CRAN (R 3.3.2)                  
#>  rprojroot   1.2         2017-01-16 CRAN (R 3.3.2)                  
#>  stringi     1.1.2       2016-10-01 CRAN (R 3.3.1)                  
#>  stringr     1.2.0       2017-02-18 CRAN (R 3.3.2)                  
#>  withr       1.0.2       2016-06-20 CRAN (R 3.3.1)                  
#>  yaml        2.1.14      2016-11-12 CRAN (R 3.3.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment