Skip to content

Commit f9fbd93

Browse files
author
qkou
committed
initialized underlying SEXP when construct String from char*/std::string
1 parent ac11e78 commit f9fbd93

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

inst/include/Rcpp/String.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ namespace Rcpp {
108108

109109
/** from a std::string */
110110
String( const std::string& s) : buffer(s), valid(false), buffer_ready(true), enc(CE_NATIVE) {
111+
data = R_NilValue ;
111112
RCPP_STRING_DEBUG( "String(const std::string& )" ) ;
112113
}
113114

@@ -118,6 +119,7 @@ namespace Rcpp {
118119

119120
/** from a const char* */
120121
String( const char* s) : buffer(s), valid(false), buffer_ready(true), enc(CE_NATIVE){
122+
data = R_NilValue ;
121123
RCPP_STRING_DEBUG( "String(const char*)" ) ;
122124
}
123125

inst/unitTests/cpp/String.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ CharacterVector test_sapply_string( CharacterVector text, CharacterVector old ,
4646
return res ;
4747
}
4848

49+
// [[Rcpp::export]]
50+
String test_ctor(String x) {
51+
String test = "test";
52+
test = x;
53+
return test;
54+
}
55+
4956
// [[Rcpp::export]]
5057
List test_compare_Strings( String aa, String bb ){
5158
return List::create(

inst/unitTests/runit.String.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2020

2121
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
22-
2322
if (.runThisTest) {
2423

2524
.setUp <- Rcpp:::unitTestSetup("String.cpp")
@@ -50,6 +49,11 @@ if (.runThisTest) {
5049
checkEquals( res, target )
5150
}
5251

52+
test.String.ctor <- function() {
53+
res <- test_ctor("abc")
54+
checkIdentical(res, "abc")
55+
}
56+
5357
test.push.front <- function() {
5458
res <- test_push_front("def")
5559
checkIdentical(res, "abcdef")

0 commit comments

Comments
 (0)