Skip to content

Commit 67f3fb1

Browse files
authored
Merge pull request #11 from pachadotdev/named_arg_utf8
implement r-lib#435
2 parents 49953b9 + b041c48 commit 67f3fb1

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

inst/include/cpp11/r_vector.hpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -888,15 +888,23 @@ inline r_vector<T>::r_vector(std::initializer_list<named_arg> il)
888888
// SAFETY: We've validated type and length ahead of this.
889889
const underlying_type elt = get_elt(value, 0);
890890

891-
// TODO: The equivalent ctor from `initializer_list<r_string>` has a specialization
892-
// for `<r_string>` to translate `elt` to UTF-8 before assigning. Should we have
893-
// that here too? `named_arg` doesn't do any checking here.
894-
if (data_p_ != nullptr) {
895-
data_p_[i] = elt;
891+
if constexpr (std::is_same<T, cpp11::r_string>::value) {
892+
// Translate to UTF-8 before assigning for string types
893+
SEXP translated_elt = Rf_mkCharCE(Rf_translateCharUTF8(elt), CE_UTF8);
894+
895+
if (data_p_ != nullptr) {
896+
data_p_[i] = translated_elt;
897+
} else {
898+
// Handles STRSXP case. VECSXP case has its own specialization.
899+
// We don't expect any ALTREP cases since we just freshly allocated `data_`.
900+
set_elt(data_, i, translated_elt);
901+
}
896902
} else {
897-
// Handles STRSXP case. VECSXP case has its own specialization.
898-
// We don't expect any ALTREP cases since we just freshly allocated `data_`.
899-
set_elt(data_, i, elt);
903+
if (data_p_ != nullptr) {
904+
data_p_[i] = elt;
905+
} else {
906+
set_elt(data_, i, elt);
907+
}
900908
}
901909

902910
SEXP name = Rf_mkCharCE(it->name(), CE_UTF8);

0 commit comments

Comments
 (0)