From 81c06aae73993397ba4ca742104b5d69d59b7e51 Mon Sep 17 00:00:00 2001 From: qinwf Date: Fri, 17 Jun 2016 23:09:09 +0800 Subject: [PATCH 1/2] add RCPP_USING_UTF8_ERROR_STRING for UTF-8 error string --- inst/include/Rcpp/exceptions.h | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/inst/include/Rcpp/exceptions.h b/inst/include/Rcpp/exceptions.h index 155abb311..d2007f544 100644 --- a/inst/include/Rcpp/exceptions.h +++ b/inst/include/Rcpp/exceptions.h @@ -137,7 +137,12 @@ inline SEXP get_last_call(){ inline SEXP get_exception_classes( const std::string& ex_class) { Rcpp::Shield res( Rf_allocVector( STRSXP, 4 ) ); + + #ifndef RCPP_USING_UTF8_ERROR_STRING SET_STRING_ELT( res, 0, Rf_mkChar( ex_class.c_str() ) ) ; + #else + SET_STRING_ELT( res, 0, Rf_mkCharLenCE( ex_class.c_str(), ex_class.size(), CE_UTF8 ) ); + #endif SET_STRING_ELT( res, 1, Rf_mkChar( "C++Error" ) ) ; SET_STRING_ELT( res, 2, Rf_mkChar( "error" ) ) ; SET_STRING_ELT( res, 3, Rf_mkChar( "condition" ) ) ; @@ -146,8 +151,13 @@ inline SEXP get_exception_classes( const std::string& ex_class) { inline SEXP make_condition(const std::string& ex_msg, SEXP call, SEXP cppstack, SEXP classes){ Rcpp::Shield res( Rf_allocVector( VECSXP, 3 ) ) ; - - SET_VECTOR_ELT( res, 0, Rf_mkString( ex_msg.c_str() ) ) ; + #ifndef RCPP_USING_UTF8_ERROR_STRING + SET_VECTOR_ELT( res, 0, Rf_mkString( ex_msg.c_str() ) ) ; + #else + Rcpp::Shield ex_msg_rstring( Rf_allocVector( STRSXP, 1 ) ) ; + SET_STRING_ELT( ex_msg_rstring, 0, Rf_mkCharLenCE( ex_msg.c_str(), ex_msg.size(), CE_UTF8 ) ); + SET_VECTOR_ELT( res, 0, ex_msg_rstring ) ; + #endif SET_VECTOR_ELT( res, 1, call ) ; SET_VECTOR_ELT( res, 2, cppstack ) ; @@ -174,10 +184,17 @@ inline SEXP exception_to_r_condition( const std::exception& ex){ inline SEXP string_to_try_error( const std::string& str){ using namespace Rcpp; - - Rcpp::Shield simpleErrorExpr( Rf_lang2(::Rf_install("simpleError"), Rf_mkString(str.c_str())) ); + + #ifndef RCPP_USING_UTF8_ERROR_STRING + Rcpp::Shield simpleErrorExpr( Rf_lang2(::Rf_install("simpleError"), Rf_mkString(str.c_str())) ); + Rcpp::Shield tryError( Rf_mkString( str.c_str() ) ); + #else + Rcpp::Shield tryError( Rf_allocVector( STRSXP, 1 ) ) ; + SET_STRING_ELT( tryError, 0, Rf_mkCharLenCE( str.c_str(), str.size(), CE_UTF8 ) ); + Rcpp::Shield simpleErrorExpr( Rf_lang2(::Rf_install("simpleError"), tryError )); + #endif + Rcpp::Shield simpleError( Rf_eval(simpleErrorExpr, R_GlobalEnv) ); - Rcpp::Shield tryError( Rf_mkString( str.c_str() ) ); Rf_setAttrib( tryError, R_ClassSymbol, Rf_mkString("try-error") ) ; Rf_setAttrib( tryError, Rf_install( "condition") , simpleError ) ; From a8077ee8a5821609b592ac8e6dc9ebe63d275bc0 Mon Sep 17 00:00:00 2001 From: qinwf Date: Fri, 17 Jun 2016 23:38:30 +0800 Subject: [PATCH 2/2] Changelog and NEWS.Rd entry for #493 --- ChangeLog | 4 ++++ inst/NEWS.Rd | 1 + 2 files changed, 5 insertions(+) diff --git a/ChangeLog b/ChangeLog index ef5b66eba..efa3e64b3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2016-06-20 Qin Wenfeng + + * inst/include/Rcpp/exceptions.h: add RCPP_USING_UTF8_ERROR_STRING macro to use UTF-8 encoding exception string in R + 2016-06-14 Artem Klevtsov * inst/include/Rcpp/sugar/functions/na_omit.h: Improve na_omit for diff --git a/inst/NEWS.Rd b/inst/NEWS.Rd index 08b0d84f6..0b296fece 100644 --- a/inst/NEWS.Rd +++ b/inst/NEWS.Rd @@ -14,6 +14,7 @@ \ghit{486}). \item String replacement was corrected (Qiang in \ghpr{479} following mailing list bug report by Masaki Tsuda) + \item Add RCPP_USING_UTF8_ERROR_STRING macro to use UTF-8 encoding exception string in R (Qin Wenfeng in \ghpr{493}) } \item Changes in Rcpp Sugar: \itemize{