Skip to content

Commit 5fbc0ec

Browse files
committed
fix for rchk
1 parent d0663e6 commit 5fbc0ec

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/which_first.c

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#include "which_first.h"
22
#include "backports.h"
33

4-
static inline SEXP named_return(R_xlen_t ind, SEXP names) {
5-
if (isNull(names))
4+
static inline SEXP named_return(R_xlen_t ind, SEXP x, SEXP use_names) {
5+
if (!LOGICAL_RO(use_names)[0]) {
66
return ScalarInteger(ind + 1);
7+
}
8+
9+
SEXP names = PROTECT(getAttrib(x, R_NamesSymbol));
10+
if (isNull(names)) {
11+
UNPROTECT(1);
12+
return ScalarInteger(ind + 1);
13+
}
714

8-
SEXP res;
9-
PROTECT(res = ScalarInteger(ind + 1));
15+
SEXP res = PROTECT(ScalarInteger(ind + 1));
1016
setAttrib(res, R_NamesSymbol, ScalarString(STRING_ELT(names, ind)));
11-
UNPROTECT(1);
17+
UNPROTECT(2);
1218
return res;
1319
}
1420

@@ -22,12 +28,7 @@ SEXP attribute_hidden c_which_first(SEXP x, SEXP use_names) {
2228

2329
for (R_xlen_t i = 0; i < n; i++) {
2430
if (xp[i] != NA_LOGICAL && xp[i]) {
25-
if (LOGICAL_RO(use_names)[0]) {
26-
SEXP nn = getAttrib(x, R_NamesSymbol);
27-
return named_return(i, nn);
28-
} else {
29-
return ScalarInteger(i+1);
30-
}
31+
return named_return(i, x, use_names);
3132
}
3233
}
3334
return allocVector(INTSXP, 0);
@@ -42,12 +43,7 @@ SEXP attribute_hidden c_which_last(SEXP x, SEXP use_names) {
4243

4344
for (R_xlen_t i = xlength(x) - 1; i >= 0; i--) {
4445
if (xp[i] != NA_LOGICAL && xp[i]) {
45-
if (LOGICAL_RO(use_names)[0]) {
46-
SEXP nn = getAttrib(x, R_NamesSymbol);
47-
return named_return(i,nn);
48-
} else {
49-
return ScalarInteger(i+1);
50-
}
46+
return named_return(i, x, use_names);
5147
}
5248
}
5349
return allocVector(INTSXP, 0);

0 commit comments

Comments
 (0)