Skip to content

Commit

Permalink
Directly return node if num_results = 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich committed Aug 29, 2023
1 parent 9a924ed commit 6768e16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 1 addition & 6 deletions R/xml_find.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,7 @@ xml_find_first.xml_missing <- function(x, xpath, ns = xml_ns(x)) {

#' @export
xml_find_first.xml_node <- function(x, xpath, ns = xml_ns(x)) {
res <- .Call(xpath_search, x$node, x$doc, xpath, ns, 1)
if (length(res) == 1) {
res[[1]]
} else {
res
}
.Call(xpath_search, x$node, x$doc, xpath, ns, 1)
}

#' @export
Expand Down
19 changes: 16 additions & 3 deletions src/xml2_xpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,27 @@ class XmlSeeker {
if (nodes == NULL || nodes->nodeNr == 0) {
return new_xml_missing();
}
int n = std::min(result_->nodesetval->nodeNr, num_results);

SEXP out = PROTECT(Rf_allocVector(VECSXP, n));

SEXP names = PROTECT(Rf_allocVector(STRSXP, 2));
SET_STRING_ELT(names, 0, Rf_mkChar("node"));
SET_STRING_ELT(names, 1, Rf_mkChar("doc"));

if (num_results == 1) {
SEXP out = PROTECT(Rf_allocVector(VECSXP, 2));

SET_VECTOR_ELT(out, 0, XPtrNode(nodes->nodeTab[0]));
SET_VECTOR_ELT(out, 1, doc_);

Rf_setAttrib(out, R_NamesSymbol, names);
Rf_setAttrib(out, R_ClassSymbol, Rf_mkString("xml_node"));

UNPROTECT(2);
return out;
}

int n = std::min(nodes->nodeNr, num_results);
SEXP out = PROTECT(Rf_allocVector(VECSXP, n));

for (int i = 0; i < n; i++) {
SEXP ret = PROTECT(Rf_allocVector(VECSXP, 2));

Expand Down

0 comments on commit 6768e16

Please sign in to comment.