@@ -62,7 +62,7 @@ namespace Rcpp {
6262 RCPP_STRING_DEBUG ( " String(const String&)" ) ;
6363 }
6464
65- String ( const String& other, const char * enc) : data( other.get_sexp()), valid(true ), buffer_ready(false ) {
65+ String ( const String& other, const std::string& enc) : data( other.get_sexp()), valid(true ), buffer_ready(false ) {
6666 set_encoding (enc);
6767 RCPP_STRING_DEBUG ( " String(const String&)" ) ;
6868 }
@@ -72,7 +72,7 @@ namespace Rcpp {
7272 RCPP_STRING_DEBUG ( " String(SEXP)" ) ;
7373 }
7474
75- String (SEXP charsxp, const char * enc) : data(charsxp), valid(true ), buffer_ready(false ) {
75+ String (SEXP charsxp, const std::string& enc) : data(charsxp), valid(true ), buffer_ready(false ) {
7676 set_encoding (enc);
7777 RCPP_STRING_DEBUG ( " String(SEXP)" ) ;
7878 }
@@ -82,7 +82,7 @@ namespace Rcpp {
8282 RCPP_STRING_DEBUG ( " String( const StringProxy&)" ) ;
8383 }
8484
85- String ( const StringProxy& proxy, const char * enc ): data( proxy.get() ), valid(true ), buffer_ready(false ) {
85+ String ( const StringProxy& proxy, const std::string& enc ): data( proxy.get() ), valid(true ), buffer_ready(false ) {
8686 set_encoding (enc);
8787 RCPP_STRING_DEBUG ( " String( const StringProxy&)" ) ;
8888 }
@@ -92,7 +92,7 @@ namespace Rcpp {
9292 RCPP_STRING_DEBUG ( " String( const const_StringProxy&)" ) ;
9393 }
9494
95- String ( const const_StringProxy& proxy, const char * enc ): data( proxy.get() ), valid(true ), buffer_ready(false ) {
95+ String ( const const_StringProxy& proxy, const std::string& enc ): data( proxy.get() ), valid(true ), buffer_ready(false ) {
9696 set_encoding (enc);
9797 RCPP_STRING_DEBUG ( " String( const const_StringProxy&)" ) ;
9898 }
@@ -177,8 +177,8 @@ namespace Rcpp {
177177
178178 public:
179179
180- inline String& operator +=( const std::wstring& s){ return append_wide_string ( s ); }
181- inline String& operator +=( const wchar_t * s){ return append_wide_string ( s ); }
180+ inline String& operator +=( const std::wstring& s){ return append_wide_string ( s ); }
181+ inline String& operator +=( const wchar_t * s){ return append_wide_string ( s ); }
182182
183183 inline String& operator +=( const String& other ){
184184 RCPP_STRING_DEBUG ( " String::operator+=( const char*)" ) ;
@@ -366,21 +366,25 @@ namespace Rcpp {
366366
367367 inline void set_encoding ( cetype_t encoding ) {
368368 enc = encoding;
369- if (data != NULL )
370- data = Rf_mkCharCE (Rf_translateCharUTF8 (data), enc);
369+
370+ if (valid) {
371+ data = Rf_mkCharCE (Rf_translateCharUTF8 (data), encoding);
372+ } else {
373+ data = Rf_mkCharCE (buffer.c_str (), encoding) ;
374+ valid = true ;
375+ }
371376 }
372377
373378 inline void set_encoding (const std::string & encoding) {
374379 if ( encoding == " bytes" ) {
375- enc = CE_BYTES;
380+ set_encoding ( CE_BYTES ) ;
376381 } else if ( encoding == " latin1" ) {
377- enc = CE_LATIN1;
382+ set_encoding ( CE_LATIN1 ) ;
378383 } else if ( encoding == " UTF-8" ) {
379- enc = CE_UTF8;
384+ set_encoding ( CE_UTF8 ) ;
380385 } else {
381- enc = CE_ANY;
386+ set_encoding ( CE_ANY ) ;
382387 }
383- set_encoding (enc);
384388 }
385389
386390 bool operator <( const Rcpp::String& other ) const {
@@ -455,8 +459,8 @@ namespace Rcpp {
455459 return s.get_sexp () ;
456460 }
457461
458- template <int RTYPE>
459- template <typename T>
462+ template <int RTYPE>
463+ template <typename T>
460464 string_proxy<RTYPE>& string_proxy<RTYPE>::operator +=(const T& rhs) {
461465 String tmp = get () ;
462466 tmp += rhs ;
@@ -467,7 +471,7 @@ namespace Rcpp {
467471 }
468472
469473
470- template <>
474+ template <>
471475 inline SEXP wrap<Rcpp::String>( const Rcpp::String& object) {
472476 RCPP_STRING_DEBUG ( " wrap<String>()" ) ;
473477 Shield<SEXP> res ( Rf_allocVector ( STRSXP, 1 ) ) ;
0 commit comments