Skip to content

Commit

Permalink
fix: remove deprecated API call to C function R_nchar
Browse files Browse the repository at this point in the history
  • Loading branch information
mllg committed Jul 20, 2024
1 parent 2a87951 commit e0d50b3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/checks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}

Expand All @@ -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)));
}
}

Expand All @@ -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)));
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/find_nchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/find_nchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <R.h>
#include <Rinternals.h>

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);
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test_checkString.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

0 comments on commit e0d50b3

Please sign in to comment.