Skip to content

Commit e09d20c

Browse files
committed
Use cpp11 in xml2_utils.h
1 parent 50489ac commit e09d20c

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/xml2_doc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ cpp11::strings doc_url(cpp11::sexp doc_sxp) {
266266
}
267267

268268
[[cpp11::register]]
269-
cpp11::sexp doc_new(cpp11::sexp version_sxp, cpp11::strings encoding_sxp) {
269+
cpp11::sexp doc_new(cpp11::strings version_sxp, cpp11::strings encoding_sxp) {
270270
const char* encoding = cpp11::as_cpp<const char*>(encoding_sxp);
271271

272272
XPtrDoc x(xmlNewDoc(asXmlChar(version_sxp)));

src/xml2_utils.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ inline const xmlChar* asXmlChar(std::string const& x) {
1515
return (const xmlChar*) x.c_str();
1616
}
1717

18-
inline const xmlChar* asXmlChar(SEXP x, int n = 0) {
19-
return (const xmlChar*) CHAR(STRING_ELT(x, n));
18+
inline const xmlChar* asXmlChar(cpp11::strings x) {
19+
return (const xmlChar*) cpp11::as_cpp<const char*>(x);
2020
}
2121

2222
#define BEGIN_CPP try {
@@ -64,11 +64,11 @@ class Xml2String {
6464
return std::string((char*) string_);
6565
}
6666

67-
SEXP asRString(SEXP missing = NA_STRING) {
67+
cpp11::r_string asRString(cpp11::r_string missing = NA_STRING) {
6868
if (string_ == NULL)
6969
return missing;
7070

71-
return Rf_mkCharCE((char*) string_, CE_UTF8);
71+
return cpp11::r_string((char*) string_);
7272
};
7373
};
7474

@@ -87,10 +87,10 @@ class NsMap {
8787
}
8888

8989
// Initialise from an existing STRSXP
90-
NsMap(SEXP x) {
91-
SEXP names = Rf_getAttrib(x, R_NamesSymbol);
92-
for (R_len_t i = 0; i < Rf_xlength(x); ++i) {
93-
add(std::string(CHAR(STRING_ELT(names, i))), std::string(CHAR(STRING_ELT(x, i))));
90+
NsMap(cpp11::strings x) {
91+
cpp11::strings names = x.names();
92+
for (R_len_t i = 0; i < x.size(); ++i) {
93+
add(cpp11::r_string(names[i]), cpp11::r_string(x[i]));
9494
}
9595
}
9696

@@ -128,20 +128,20 @@ class NsMap {
128128
return true;
129129
}
130130

131-
SEXP out() {
132-
SEXP out = PROTECT(Rf_allocVector(STRSXP, prefix2url.size()));
133-
SEXP names = PROTECT(Rf_allocVector(STRSXP, prefix2url.size()));
131+
cpp11::sexp out() {
132+
int n = prefix2url.size();
133+
cpp11::writable::strings out(n);
134+
cpp11::writable::strings names(n);
134135

135136
size_t i = 0;
136137
for (prefix2url_t::const_iterator it = prefix2url.begin(); it != prefix2url.end(); ++it) {
137-
SET_STRING_ELT(out, i, Rf_mkChar(it->second.c_str()));
138-
SET_STRING_ELT(names, i, Rf_mkChar(it->first.c_str()));
138+
out[i] = it->second.c_str();
139+
names[i] = it->first.c_str();
139140
++i;
140141
}
141142

142-
Rf_setAttrib(out, R_NamesSymbol, names);
143+
out.names() = names;
143144

144-
UNPROTECT(2);
145145
return out;
146146
}
147147
};

0 commit comments

Comments
 (0)