Skip to content

Commit 81c06aa

Browse files
author
qinwf
committed
add RCPP_USING_UTF8_ERROR_STRING for UTF-8 error string
1 parent b977453 commit 81c06aa

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

inst/include/Rcpp/exceptions.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ inline SEXP get_last_call(){
137137

138138
inline SEXP get_exception_classes( const std::string& ex_class) {
139139
Rcpp::Shield<SEXP> res( Rf_allocVector( STRSXP, 4 ) );
140+
141+
#ifndef RCPP_USING_UTF8_ERROR_STRING
140142
SET_STRING_ELT( res, 0, Rf_mkChar( ex_class.c_str() ) ) ;
143+
#else
144+
SET_STRING_ELT( res, 0, Rf_mkCharLenCE( ex_class.c_str(), ex_class.size(), CE_UTF8 ) );
145+
#endif
141146
SET_STRING_ELT( res, 1, Rf_mkChar( "C++Error" ) ) ;
142147
SET_STRING_ELT( res, 2, Rf_mkChar( "error" ) ) ;
143148
SET_STRING_ELT( res, 3, Rf_mkChar( "condition" ) ) ;
@@ -146,8 +151,13 @@ inline SEXP get_exception_classes( const std::string& ex_class) {
146151

147152
inline SEXP make_condition(const std::string& ex_msg, SEXP call, SEXP cppstack, SEXP classes){
148153
Rcpp::Shield<SEXP> res( Rf_allocVector( VECSXP, 3 ) ) ;
149-
150-
SET_VECTOR_ELT( res, 0, Rf_mkString( ex_msg.c_str() ) ) ;
154+
#ifndef RCPP_USING_UTF8_ERROR_STRING
155+
SET_VECTOR_ELT( res, 0, Rf_mkString( ex_msg.c_str() ) ) ;
156+
#else
157+
Rcpp::Shield<SEXP> ex_msg_rstring( Rf_allocVector( STRSXP, 1 ) ) ;
158+
SET_STRING_ELT( ex_msg_rstring, 0, Rf_mkCharLenCE( ex_msg.c_str(), ex_msg.size(), CE_UTF8 ) );
159+
SET_VECTOR_ELT( res, 0, ex_msg_rstring ) ;
160+
#endif
151161
SET_VECTOR_ELT( res, 1, call ) ;
152162
SET_VECTOR_ELT( res, 2, cppstack ) ;
153163

@@ -174,10 +184,17 @@ inline SEXP exception_to_r_condition( const std::exception& ex){
174184

175185
inline SEXP string_to_try_error( const std::string& str){
176186
using namespace Rcpp;
177-
178-
Rcpp::Shield<SEXP> simpleErrorExpr( Rf_lang2(::Rf_install("simpleError"), Rf_mkString(str.c_str())) );
187+
188+
#ifndef RCPP_USING_UTF8_ERROR_STRING
189+
Rcpp::Shield<SEXP> simpleErrorExpr( Rf_lang2(::Rf_install("simpleError"), Rf_mkString(str.c_str())) );
190+
Rcpp::Shield<SEXP> tryError( Rf_mkString( str.c_str() ) );
191+
#else
192+
Rcpp::Shield<SEXP> tryError( Rf_allocVector( STRSXP, 1 ) ) ;
193+
SET_STRING_ELT( tryError, 0, Rf_mkCharLenCE( str.c_str(), str.size(), CE_UTF8 ) );
194+
Rcpp::Shield<SEXP> simpleErrorExpr( Rf_lang2(::Rf_install("simpleError"), tryError ));
195+
#endif
196+
179197
Rcpp::Shield<SEXP> simpleError( Rf_eval(simpleErrorExpr, R_GlobalEnv) );
180-
Rcpp::Shield<SEXP> tryError( Rf_mkString( str.c_str() ) );
181198
Rf_setAttrib( tryError, R_ClassSymbol, Rf_mkString("try-error") ) ;
182199
Rf_setAttrib( tryError, Rf_install( "condition") , simpleError ) ;
183200

0 commit comments

Comments
 (0)