Rcpp 中 `STRING_ELT` 的含义

meaning of `STRING_ELT` in Rcpp

我搜索了 RCPP 的源代码,但找不到 STRING_ELT 的定义,有人可以指出一个参考,在那里我可以找到 RCPP 中宏之类的所有定义吗?

这是 R 内部的一部分,可通过以下方式访问:

#include <R.h>
#include <Rinternals.h>

参见5.9.7 Handling character data of Writing R Extensions

R character vectors are stored as STRSXPs, a vector type like VECSXP where every element is of type CHARSXP. The CHARSXP elements of STRSXPs are accessed using STRING_ELT and SET_STRING_ELT.

CHARSXPs are read-only objects and must never be modified. In particular, the C-style string contained in a CHARSXP should be treated as read-only and for this reason the CHAR function used to access the character data of a CHARSXP returns (const char *) (this also allows compilers to issue warnings about improper use). Since CHARSXPs are immutable, the same CHARSXP can be shared by any STRSXP needing an element representing the same string. R maintains a global cache of CHARSXPs so that there is only ever one CHARSXP representing a given string in memory.

You can obtain a CHARSXP by calling mkChar and providing a nul-terminated C-style string. This function will return a pre-existing CHARSXP if one with a matching string already exists, otherwise it will create a new one and add it to the cache before returning it to you. The variant mkCharLen can be used to create a CHARSXP from part of a buffer and will ensure null-termination.

Note that R character strings are restricted to 2^31 - 1 bytes, and hence so should the input to mkChar be (C allows longer strings on 64-bit platforms).