From e0d50b3e3206ce9a8a57ff7d3209bd30942994bc Mon Sep 17 00:00:00 2001 From: Michel Lang Date: Sat, 20 Jul 2024 17:01:38 +0200 Subject: [PATCH] fix: remove deprecated API call to C function R_nchar --- src/checks.c | 6 +++--- src/find_nchar.c | 8 ++------ src/find_nchar.h | 1 - tests/testthat/test_checkString.R | 5 +++++ 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/checks.c b/src/checks.c index b1166817..52808c21 100644 --- a/src/checks.c +++ b/src/checks.c @@ -419,7 +419,7 @@ static Rboolean check_string_nchar(SEXP x, SEXP n_chars, SEXP min_chars, SEXP ma R_xlen_t pos = find_nchar(x, n); if (pos > 0) { return message("All elements must have exactly %i characters, but element %i has %i chararacters", - n, pos, get_nchars(x, pos - 1)); + n, pos, length(STRING_ELT(x, pos - 1))); } } @@ -428,7 +428,7 @@ static Rboolean check_string_nchar(SEXP x, SEXP n_chars, SEXP min_chars, SEXP ma R_xlen_t pos = find_min_nchar(x, n); if (pos > 0) { return message("All elements must have at least %i characters, but element %i has %i characters", - n, pos, get_nchars(x, pos - 1)); + n, pos, length(STRING_ELT(x, pos - 1))); } } @@ -437,7 +437,7 @@ static Rboolean check_string_nchar(SEXP x, SEXP n_chars, SEXP min_chars, SEXP ma R_xlen_t pos = find_max_nchar(x, n); if (pos > 0) { return message("All elements must have at most %i characters, but element %i has %i characters", - n, pos, get_nchars(x, pos - 1)); + n, pos, length(STRING_ELT(x, pos - 1))); } } diff --git a/src/find_nchar.c b/src/find_nchar.c index 55173a55..c5b2e434 100644 --- a/src/find_nchar.c +++ b/src/find_nchar.c @@ -5,10 +5,6 @@ static inline Rboolean ii_eq(const R_xlen_t x, const R_xlen_t y) { return x == y static inline Rboolean ii_le(const R_xlen_t x, const R_xlen_t y) { return x <= y; } static inline Rboolean ii_ge(const R_xlen_t x, const R_xlen_t y) { return x >= y; } -R_xlen_t get_nchars(SEXP x, R_xlen_t i) { - return R_nchar(STRING_ELT(x, i), Chars, TRUE, TRUE, "character vector"); -} - static R_xlen_t check_nchar(SEXP x, R_xlen_t n, cm_ll_cmp cmp) { if (!isString(x)) { SEXP xs = PROTECT(coerceVector(x, STRSXP)); @@ -19,8 +15,8 @@ static R_xlen_t check_nchar(SEXP x, R_xlen_t n, cm_ll_cmp cmp) { const R_xlen_t nx = xlength(x); for (R_xlen_t i = 0; i < nx; i++) { - R_xlen_t nchars = get_nchars(x, i); - if (nchars != NA_INTEGER && !(*cmp)(nchars, n)) { + SEXP tmp = STRING_ELT(x, i); + if (tmp != NA_STRING && !(*cmp)(length(tmp), n)) { return i + 1; } } diff --git a/src/find_nchar.h b/src/find_nchar.h index cc9af180..85e079ee 100644 --- a/src/find_nchar.h +++ b/src/find_nchar.h @@ -4,7 +4,6 @@ #include #include -R_xlen_t get_nchars(SEXP, R_xlen_t); R_xlen_t find_nchar(SEXP, R_xlen_t); R_xlen_t find_min_nchar(SEXP, R_xlen_t); R_xlen_t find_max_nchar(SEXP, R_xlen_t); diff --git a/tests/testthat/test_checkString.R b/tests/testthat/test_checkString.R index 7bf18565..e94751b3 100644 --- a/tests/testthat/test_checkString.R +++ b/tests/testthat/test_checkString.R @@ -26,4 +26,9 @@ test_that("checkString", { expect_true(testString(NA_character_, min.chars = 1, na.ok = TRUE)) expect_true(testString(NA_real_, min.chars = 1, na.ok = TRUE)) expect_error(assertString(1)) + + expect_true(testString("a", n.chars = 1)) + expect_false(testString("", n.chars = 1)) + expect_true(testString("a", max.chars = 1)) + expect_false(testString("ab", max.chars = 1)) })