From bb9cd606515b4ae7eefa29d8362ced865a00e0fb Mon Sep 17 00:00:00 2001 From: Dewey Dunnington Date: Sat, 16 Dec 2023 13:58:16 -0400 Subject: [PATCH 1/3] calc nrow --- inst/include/cpp11/data_frame.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/include/cpp11/data_frame.hpp b/inst/include/cpp11/data_frame.hpp index f1caad51..59e30985 100644 --- a/inst/include/cpp11/data_frame.hpp +++ b/inst/include/cpp11/data_frame.hpp @@ -36,7 +36,7 @@ class data_frame : public list { return R_NilValue; } - static int calc_nrow(SEXP x) { + static R_xlen_t calc_nrow(SEXP x) { auto nms = get_attrib0(x, R_RowNamesSymbol); bool has_short_rownames = (Rf_isInteger(nms) && Rf_xlength(nms) == 2 && INTEGER(nms)[0] == NA_INTEGER); From e5a4629918f9ea09d4062396079adab51e550fcc Mon Sep 17 00:00:00 2001 From: Dewey Dunnington Date: Mon, 18 Dec 2023 15:29:12 -0400 Subject: [PATCH 2/3] first pass --- inst/include/cpp11/as.hpp | 15 +++++++-------- inst/include/cpp11/r_string.hpp | 2 +- inst/include/cpp11/sexp.hpp | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/inst/include/cpp11/as.hpp b/inst/include/cpp11/as.hpp index 682f12b5..d9a94692 100644 --- a/inst/include/cpp11/as.hpp +++ b/inst/include/cpp11/as.hpp @@ -92,22 +92,21 @@ template enable_if_integral as_cpp(SEXP from) { if (Rf_isInteger(from)) { if (Rf_xlength(from) == 1) { - return INTEGER_ELT(from, 0); + return static_cast(INTEGER_ELT(from, 0)); } } else if (Rf_isReal(from)) { if (Rf_xlength(from) == 1) { - if (ISNA(REAL_ELT(from, 0))) { - return NA_INTEGER; - } double value = REAL_ELT(from, 0); - if (is_convertible_without_loss_to_integer(value)) { - return value; + if (ISNA(value) && sizeof(T) >= sizeof(int)) { + return static_cast(NA_INTEGER); + } else if (!ISNA(value) && is_convertible_without_loss_to_integer(value)) { + return static_cast(value); } } } else if (Rf_isLogical(from)) { if (Rf_xlength(from) == 1) { - if (LOGICAL_ELT(from, 0) == NA_LOGICAL) { - return NA_INTEGER; + if (LOGICAL_ELT(from, 0) == NA_LOGICAL && sizeof(T) >= sizeof(int)) { + return static_cast(NA_INTEGER); } } } diff --git a/inst/include/cpp11/r_string.hpp b/inst/include/cpp11/r_string.hpp index 692b66ea..edef4431 100644 --- a/inst/include/cpp11/r_string.hpp +++ b/inst/include/cpp11/r_string.hpp @@ -17,7 +17,7 @@ class r_string { r_string(SEXP data) : data_(data) {} r_string(const char* data) : data_(safe[Rf_mkCharCE](data, CE_UTF8)) {} r_string(const std::string& data) - : data_(safe[Rf_mkCharLenCE](data.c_str(), data.size(), CE_UTF8)) {} + : data_(safe[Rf_mkCharLenCE](data.c_str(), static_cast(data.size()), CE_UTF8)) {} operator SEXP() const { return data_; } operator sexp() const { return data_; } diff --git a/inst/include/cpp11/sexp.hpp b/inst/include/cpp11/sexp.hpp index 9f6f5e18..eef0cf49 100644 --- a/inst/include/cpp11/sexp.hpp +++ b/inst/include/cpp11/sexp.hpp @@ -76,7 +76,7 @@ class sexp { operator SEXP() const { return data_; } operator double() const { return REAL_ELT(data_, 0); } - operator size_t() const { return REAL_ELT(data_, 0); } + operator size_t() const { return static_cast(REAL_ELT(data_, 0)); } operator bool() const { return LOGICAL_ELT(data_, 0); } SEXP data() const { return data_; } }; From 467cf1e4e95c8dfae5baf6eaf97a69ed10ced89c Mon Sep 17 00:00:00 2001 From: Dewey Dunnington Date: Mon, 18 Dec 2023 15:50:24 -0400 Subject: [PATCH 3/3] format --- inst/include/cpp11/r_string.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inst/include/cpp11/r_string.hpp b/inst/include/cpp11/r_string.hpp index edef4431..90f2d51d 100644 --- a/inst/include/cpp11/r_string.hpp +++ b/inst/include/cpp11/r_string.hpp @@ -17,7 +17,8 @@ class r_string { r_string(SEXP data) : data_(data) {} r_string(const char* data) : data_(safe[Rf_mkCharCE](data, CE_UTF8)) {} r_string(const std::string& data) - : data_(safe[Rf_mkCharLenCE](data.c_str(), static_cast(data.size()), CE_UTF8)) {} + : data_( + safe[Rf_mkCharLenCE](data.c_str(), static_cast(data.size()), CE_UTF8)) {} operator SEXP() const { return data_; } operator sexp() const { return data_; }