1
1
#include "which_first.h"
2
2
#include "backports.h"
3
3
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 ]) {
6
6
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
+ }
7
14
8
- SEXP res ;
9
- PROTECT (res = ScalarInteger (ind + 1 ));
15
+ SEXP res = PROTECT (ScalarInteger (ind + 1 ));
10
16
setAttrib (res , R_NamesSymbol , ScalarString (STRING_ELT (names , ind )));
11
- UNPROTECT (1 );
17
+ UNPROTECT (2 );
12
18
return res ;
13
19
}
14
20
@@ -22,12 +28,7 @@ SEXP attribute_hidden c_which_first(SEXP x, SEXP use_names) {
22
28
23
29
for (R_xlen_t i = 0 ; i < n ; i ++ ) {
24
30
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 );
31
32
}
32
33
}
33
34
return allocVector (INTSXP , 0 );
@@ -42,12 +43,7 @@ SEXP attribute_hidden c_which_last(SEXP x, SEXP use_names) {
42
43
43
44
for (R_xlen_t i = xlength (x ) - 1 ; i >= 0 ; i -- ) {
44
45
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 );
51
47
}
52
48
}
53
49
return allocVector (INTSXP , 0 );
0 commit comments