@@ -54,45 +54,54 @@ namespace Rcpp {
54
54
55
55
/* * default constructor */
56
56
String ( ): data( Rf_mkChar(" " ) ), buffer(), valid(true ), buffer_ready(true ), enc(CE_NATIVE) {
57
+ Rcpp_PreserveObject ( data );
57
58
RCPP_STRING_DEBUG ( " String()" ) ;
58
59
}
59
60
60
61
/* * copy constructor */
61
62
String ( const String& other) : data( other.get_sexp()), valid(true ), buffer_ready(false ), enc(Rf_getCharCE(other.get_sexp())) {
63
+ Rcpp_PreserveObject ( data );
62
64
RCPP_STRING_DEBUG ( " String(const String&)" ) ;
63
65
}
64
66
65
67
String ( const String& other, const std::string& enc) : data( other.get_sexp()), valid(true ), buffer_ready(false ) {
68
+ Rcpp_PreserveObject ( data );
66
69
set_encoding (enc);
67
70
RCPP_STRING_DEBUG ( " String(const String&)" ) ;
68
71
}
69
72
70
73
/* * construct a string from a single CHARSXP SEXP */
71
74
String (SEXP charsxp) : data(charsxp), valid(true ), buffer_ready(false ), enc(Rf_getCharCE(charsxp)) {
75
+ Rcpp_PreserveObject ( data );
72
76
RCPP_STRING_DEBUG ( " String(SEXP)" ) ;
73
77
}
74
78
75
79
String (SEXP charsxp, const std::string& enc) : data(charsxp), valid(true ), buffer_ready(false ) {
80
+ Rcpp_PreserveObject ( data );
76
81
set_encoding (enc);
77
82
RCPP_STRING_DEBUG ( " String(SEXP)" ) ;
78
83
}
79
84
80
85
/* * from string proxy */
81
86
String ( const StringProxy& proxy ): data( proxy.get() ), valid(true ), buffer_ready(false ), enc(Rf_getCharCE(proxy.get())){
87
+ Rcpp_PreserveObject ( data );
82
88
RCPP_STRING_DEBUG ( " String( const StringProxy&)" ) ;
83
89
}
84
90
85
91
String ( const StringProxy& proxy, const std::string& enc ): data( proxy.get() ), valid(true ), buffer_ready(false ) {
92
+ Rcpp_PreserveObject ( data );
86
93
set_encoding (enc);
87
94
RCPP_STRING_DEBUG ( " String( const StringProxy&)" ) ;
88
95
}
89
96
90
97
/* * from string proxy */
91
98
String ( const const_StringProxy& proxy ): data( proxy.get() ), valid(true ), buffer_ready(false ), enc(Rf_getCharCE(proxy.get())){
99
+ Rcpp_PreserveObject ( data );
92
100
RCPP_STRING_DEBUG ( " String( const const_StringProxy&)" ) ;
93
101
}
94
102
95
103
String ( const const_StringProxy& proxy, const std::string& enc ): data( proxy.get() ), valid(true ), buffer_ready(false ) {
104
+ Rcpp_PreserveObject ( data );
96
105
set_encoding (enc);
97
106
RCPP_STRING_DEBUG ( " String( const const_StringProxy&)" ) ;
98
107
}
@@ -103,6 +112,7 @@ namespace Rcpp {
103
112
}
104
113
105
114
String ( const std::wstring& s) : data(internal::make_charsexp(s)), valid(true ), buffer_ready(false ), enc(CE_NATIVE) {
115
+ Rcpp_PreserveObject ( data );
106
116
RCPP_STRING_DEBUG ( " String(const std::wstring& )" ) ;
107
117
}
108
118
@@ -112,32 +122,38 @@ namespace Rcpp {
112
122
}
113
123
114
124
String ( const wchar_t * s) : data(internal::make_charsexp(s)), valid(true ), buffer_ready(false ), enc(CE_NATIVE) {
125
+ Rcpp_PreserveObject ( data );
115
126
RCPP_STRING_DEBUG ( " String(const wchar_t* s)" ) ;
116
127
}
117
128
118
129
/* * constructors from R primitives */
119
- String ( int x ) : data( internal::r_coerce<INTSXP,STRSXP>(x) ), valid(true ), buffer_ready(false ), enc(CE_NATIVE){}
120
- String ( double x ) : data( internal::r_coerce<REALSXP,STRSXP>(x) ), valid(true ), buffer_ready(false ), enc(CE_NATIVE){}
121
- String ( bool x ) : data( internal::r_coerce<LGLSXP,STRSXP>(x) ), valid( true ) , buffer_ready(false ), enc(CE_NATIVE){}
122
- String ( Rcomplex x ) : data( internal::r_coerce<CPLXSXP,STRSXP>(x) ), valid( true ), buffer_ready(false ), enc(CE_NATIVE){}
123
- String ( Rbyte x ) : data( internal::r_coerce<RAWSXP,STRSXP>(x) ), valid(true ), buffer_ready(false ), enc(CE_NATIVE){}
124
-
125
-
126
- inline String& operator =( int x ){ data = internal::r_coerce<INTSXP ,STRSXP>( x ) ; valid = true ; buffer_ready = false ; return *this ; }
127
- inline String& operator =( double x ){ data = internal::r_coerce<REALSXP,STRSXP>( x ) ; valid = true ; buffer_ready = false ; return *this ; }
128
- inline String& operator =( Rbyte x ){ data = internal::r_coerce<RAWSXP ,STRSXP>( x ) ; valid = true ; buffer_ready = false ; return *this ; }
129
- inline String& operator =( bool x ){ data = internal::r_coerce<LGLSXP ,STRSXP>( x ) ; valid = true ; buffer_ready = false ; return *this ; }
130
- inline String& operator =( Rcomplex x){ data = internal::r_coerce<CPLXSXP,STRSXP>( x ) ; valid = true ; buffer_ready = false ; return *this ; }
131
- inline String& operator =( SEXP x){ data = x ; valid = true ; buffer_ready = false ; return *this ; }
132
- inline String& operator =( const StringProxy& proxy){ data = proxy.get () ; valid = true ; buffer_ready=false ; return *this ; }
133
- inline String& operator =( const String& other ){ data = other.get_sexp () ; valid = true ; buffer_ready = false ; return *this ; }
130
+ String ( int x ) : data( internal::r_coerce<INTSXP,STRSXP>(x) ), valid(true ), buffer_ready(false ), enc(CE_NATIVE){Rcpp_PreserveObject ( data );}
131
+ String ( double x ) : data( internal::r_coerce<REALSXP,STRSXP>(x) ), valid(true ), buffer_ready(false ), enc(CE_NATIVE){Rcpp_PreserveObject ( data );}
132
+ String ( bool x ) : data( internal::r_coerce<LGLSXP,STRSXP>(x) ), valid( true ) , buffer_ready(false ), enc(CE_NATIVE){Rcpp_PreserveObject ( data );}
133
+ String ( Rcomplex x ) : data( internal::r_coerce<CPLXSXP,STRSXP>(x) ), valid( true ), buffer_ready(false ), enc(CE_NATIVE){Rcpp_PreserveObject ( data );}
134
+ String ( Rbyte x ) : data( internal::r_coerce<RAWSXP,STRSXP>(x) ), valid(true ), buffer_ready(false ), enc(CE_NATIVE){Rcpp_PreserveObject ( data );}
135
+
136
+ ~String () {
137
+ Rcpp_ReleaseObject ( data );
138
+ data = R_NilValue ;
139
+ }
140
+
141
+
142
+ inline String& operator =( int x ){ data = Rcpp_ReplaceObject ( data, internal::r_coerce<INTSXP ,STRSXP>( x )) ; valid = true ; buffer_ready = false ; return *this ; }
143
+ inline String& operator =( double x ){ data = Rcpp_ReplaceObject ( data, internal::r_coerce<REALSXP,STRSXP>( x )) ; valid = true ; buffer_ready = false ; return *this ; }
144
+ inline String& operator =( Rbyte x ){ data = Rcpp_ReplaceObject ( data, internal::r_coerce<RAWSXP ,STRSXP>( x )) ; valid = true ; buffer_ready = false ; return *this ; }
145
+ inline String& operator =( bool x ){ data = Rcpp_ReplaceObject ( data, internal::r_coerce<LGLSXP ,STRSXP>( x )) ; valid = true ; buffer_ready = false ; return *this ; }
146
+ inline String& operator =( Rcomplex x){ data = Rcpp_ReplaceObject ( data, internal::r_coerce<CPLXSXP,STRSXP>( x )) ; valid = true ; buffer_ready = false ; return *this ; }
147
+ inline String& operator =( SEXP x){ data = Rcpp_ReplaceObject ( data, x ) ; valid = true ; buffer_ready = false ; return *this ; }
148
+ inline String& operator =( const StringProxy& proxy){ data = Rcpp_ReplaceObject ( data, proxy.get () ) ; valid = true ; buffer_ready=false ; return *this ; }
149
+ inline String& operator =( const String& other ){ data = Rcpp_ReplaceObject ( data, other.get_sexp () ) ; valid = true ; buffer_ready = false ; return *this ; }
134
150
inline String& operator =( const std::string& s){ buffer = s ; valid = false ; buffer_ready = true ; return *this ; }
135
151
inline String& operator =( const char * s){ buffer = s ; valid = false ; buffer_ready = true ; return *this ; }
136
152
137
153
private:
138
154
template <typename T>
139
155
inline String& assign_wide_string ( const T& s){
140
- data = internal::make_charsexp ( s ) ;
156
+ data = Rcpp_ReplaceObject ( data, internal::make_charsexp ( s ) ) ;
141
157
valid = true ;
142
158
buffer_ready = false ;
143
159
return *this ;
@@ -169,7 +185,7 @@ namespace Rcpp {
169
185
const char * buf = CHAR ( data );
170
186
std::wstring tmp ( buf, buf + strlen (buf ) ) ;
171
187
tmp += s ;
172
- data = internal::make_charsexp ( tmp ) ;
188
+ data = Rcpp_ReplaceObject ( data, internal::make_charsexp ( tmp ) ) ;
173
189
valid = true ;
174
190
buffer_ready = false ;
175
191
return *this ;
@@ -183,22 +199,22 @@ namespace Rcpp {
183
199
inline String& operator +=( const String& other ){
184
200
RCPP_STRING_DEBUG ( " String::operator+=( const char*)" ) ;
185
201
if ( is_na () ) return *this ;
186
- if ( other.is_na () ){ data = NA_STRING ; valid = true ; buffer_ready = false ; return *this ; }
202
+ if ( other.is_na () ){ data = Rcpp_ReplaceObject ( data, NA_STRING) ; valid = true ; buffer_ready = false ; return *this ; }
187
203
setBuffer () ; buffer += other ; valid = false ;
188
204
return *this ;
189
205
}
190
206
inline String& operator +=( const StringProxy& proxy){
191
207
RCPP_STRING_DEBUG ( " String::operator+=( const StringProxy& )" ) ;
192
208
if ( is_na () ) return *this ;
193
209
SEXP proxy_sexp = proxy ;
194
- if ( proxy_sexp == NA_STRING ) { data = NA_STRING ; valid = true ; buffer_ready = false ; return *this ;}
210
+ if ( proxy_sexp == NA_STRING ) { data = Rcpp_ReplaceObject ( data, NA_STRING) ; valid = true ; buffer_ready = false ; return *this ;}
195
211
setBuffer () ; buffer += CHAR (proxy_sexp) ; valid = false ;
196
212
return *this ;
197
213
}
198
214
inline String& operator +=( SEXP x){
199
215
RCPP_STRING_DEBUG ( " String::operator+=( SEXP )" ) ;
200
216
if ( is_na () ) return *this ;
201
- if ( x == NA_STRING ) { data = NA_STRING ; valid = true ; buffer_ready = false ; return *this ;}
217
+ if ( x == NA_STRING ) { data = Rcpp_ReplaceObject ( data, NA_STRING ) ; valid = true ; buffer_ready = false ; return *this ;}
202
218
setBuffer () ; buffer += CHAR (x) ; valid = false ;
203
219
return *this ;
204
220
}
@@ -324,7 +340,8 @@ namespace Rcpp {
324
340
325
341
326
342
inline void set_na (){
327
- data = NA_STRING ; valid = true ; buffer_ready = false ;
343
+ data = Rcpp_ReplaceObject (data, NA_STRING) ;
344
+ valid = true ; buffer_ready = false ;
328
345
}
329
346
330
347
@@ -368,9 +385,10 @@ namespace Rcpp {
368
385
enc = encoding;
369
386
370
387
if (valid) {
371
- data = Rf_mkCharCE (Rf_translateCharUTF8 (data), encoding);
388
+ data = Rcpp_ReplaceObject (data, Rf_mkCharCE (Rf_translateCharUTF8 (data), encoding) );
372
389
} else {
373
390
data = Rf_mkCharCE (buffer.c_str (), encoding) ;
391
+ Rcpp_PreserveObject ( data );
374
392
valid = true ;
375
393
}
376
394
}
@@ -430,6 +448,7 @@ namespace Rcpp {
430
448
RCPP_STRING_DEBUG ( " setData" ) ;
431
449
if (!valid) {
432
450
data = Rf_mkCharCE (buffer.c_str (), enc) ;
451
+ Rcpp_PreserveObject ( data );
433
452
valid = true ;
434
453
}
435
454
}
@@ -476,6 +495,7 @@ namespace Rcpp {
476
495
RCPP_STRING_DEBUG ( " wrap<String>()" ) ;
477
496
Shield<SEXP> res ( Rf_allocVector ( STRSXP, 1 ) ) ;
478
497
SEXP data = object.get_sexp ();
498
+ Rcpp_PreserveObject ( data );
479
499
SET_STRING_ELT ( res, 0 , data ) ;
480
500
return res ;
481
501
}
0 commit comments