Skip to content

Commit 9d8c3a2

Browse files
author
thirdwing
committed
minor update
1 parent d88820a commit 9d8c3a2

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

inst/include/Rcpp/String.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ) ) ;

inst/unitTests/cpp/String.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ String test_String_ctor_encoding(String x) {
7979
y.set_encoding("UTF-8");
8080
return y;
8181
}
82+
83+
84+
// [[Rcpp::export]]
85+
String test_String_ctor_encoding2() {
86+
String y("å");
87+
y.set_encoding("UTF-8");
88+
return y;
89+
}

inst/unitTests/runit.String.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ if (.runThisTest) {
6363
checkEquals(test_String_encoding(b), "UTF-8")
6464
checkEquals(Encoding(test_String_set_encoding(a)), "UTF-8")
6565
checkEquals(Encoding(test_String_ctor_encoding(a)), "UTF-8")
66+
checkEquals(Encoding(test_String_ctor_encoding2()), "UTF-8")
6667
}
6768

6869
}

0 commit comments

Comments
 (0)