| 131 | } // namespace unsafe |
| 132 | |
| 133 | inline SEXP utf8_strings(SEXP x) { |
| 134 | return cpp11::unwind_protect([&] { |
| 135 | // ensure that x is not actually altrep first this also ensures that |
| 136 | // x is not altrep even after it is materialized |
| 137 | bool was_altrep = ALTREP(x); |
| 138 | if (was_altrep) { |
| 139 | x = PROTECT(Rf_duplicate(x)); |
| 140 | } |
| 141 | R_xlen_t n = XLENGTH(x); |
| 142 | |
| 143 | // if `x` is an altrep of some sort, this will |
| 144 | // materialize upfront. That's usually better because |
| 145 | // the loop touches all strings |
| 146 | const SEXP* p_x = STRING_PTR_RO(x); |
| 147 | |
| 148 | for (R_xlen_t i = 0; i < n; i++, ++p_x) { |
| 149 | SEXP s = *p_x; |
| 150 | if (s != NA_STRING) { |
| 151 | SET_STRING_ELT(x, i, Rf_mkCharCE(Rf_translateCharUTF8(s), CE_UTF8)); |
| 152 | } |
| 153 | } |
| 154 | if (was_altrep) { |
| 155 | UNPROTECT(1); |
| 156 | } |
| 157 | return x; |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | struct symbols { |
| 162 | static SEXP units; |