Navigation Menu

Skip to content

Commit

Permalink
cleaner code, lower costs, and higher performance
Browse files Browse the repository at this point in the history
  • Loading branch information
renkun-ken committed Jul 24, 2014
1 parent 8d9b141 commit bf67573
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions R/main.R
@@ -1,24 +1,16 @@
css_to_xpath <- function(selector, prefix = "descendant-or-self::", translator = "generic") {
ns <- length(selector)
np <- length(prefix)
nt <- length(translator)
n <- max(ns, np, nt)
selector <- rep(selector, length.out = n)
prefix <- rep(prefix, length.out = n)
translator <- rep(translator, length.out = n)
results <- character(n)
for (i in 1:n) {
tran <-
if (translator[i] == "html") {
HTMLTranslator$new()
} else if (translator[i] == "xhtml") {
HTMLTranslator$new(xhtml = TRUE)
} else {
GenericTranslator$new()
}
results[i] <- tran$css_to_xpath(selector[i], prefix = prefix[i])
}
results
n <- max(vapply(list(selector,prefix,translator),length,integer(1L)))

This comment has been minimized.

Copy link
@renkun-ken

renkun-ken Jul 25, 2014

Author Owner

It seems that this line is unnecessary too, neither is 1L:n. Map will handle this automatically.

results <- Map(function(i,selector,prefix,translator) {
tran <- if (translator == "html") {
HTMLTranslator$new()
} else if (translator == "xhtml") {
HTMLTranslator$new(xhtml = TRUE)
} else {
GenericTranslator$new()
}
tran$css_to_xpath(selector, prefix = prefix)
}, 1L:n, selector, prefix, translator)
as.character(results)
}

querySelector <- function(doc, selector, ns = NULL, ...) {
Expand Down

0 comments on commit bf67573

Please sign in to comment.