Skip to content

Commit

Permalink
Merge commit 'e088a1c892e97436fdd3263c6cfc6df61e47a365'
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich committed Nov 2, 2023
2 parents 6f0e353 + e088a1c commit 0352dfa
Show file tree
Hide file tree
Showing 20 changed files with 635 additions and 398 deletions.
27 changes: 0 additions & 27 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ S3method(xml_add_parent,xml_nodeset)
S3method(xml_add_sibling,xml_missing)
S3method(xml_add_sibling,xml_node)
S3method(xml_add_sibling,xml_nodeset)
S3method(xml_attr,xml_missing)
S3method(xml_attr,xml_node)
S3method(xml_attr,xml_nodeset)
S3method(xml_attrs,xml_missing)
S3method(xml_attrs,xml_node)
S3method(xml_attrs,xml_nodeset)
S3method(xml_double,xml_missing)
S3method(xml_double,xml_node)
S3method(xml_double,xml_nodeset)
S3method(xml_find_all,xml_missing)
S3method(xml_find_all,xml_node)
S3method(xml_find_all,xml_nodeset)
Expand All @@ -95,25 +86,13 @@ S3method(xml_find_lgl,xml_nodeset)
S3method(xml_find_num,xml_missing)
S3method(xml_find_num,xml_node)
S3method(xml_find_num,xml_nodeset)
S3method(xml_integer,xml_missing)
S3method(xml_integer,xml_node)
S3method(xml_integer,xml_nodeset)
S3method(xml_length,xml_missing)
S3method(xml_length,xml_node)
S3method(xml_length,xml_nodeset)
S3method(xml_name,xml_missing)
S3method(xml_name,xml_node)
S3method(xml_name,xml_nodeset)
S3method(xml_ns,xml_document)
S3method(xml_ns,xml_missing)
S3method(xml_ns,xml_node)
S3method(xml_ns,xml_nodeset)
S3method(xml_parent,xml_missing)
S3method(xml_parent,xml_node)
S3method(xml_parent,xml_nodeset)
S3method(xml_path,xml_missing)
S3method(xml_path,xml_node)
S3method(xml_path,xml_nodeset)
S3method(xml_remove,xml_missing)
S3method(xml_remove,xml_node)
S3method(xml_remove,xml_nodeset)
Expand All @@ -132,12 +111,6 @@ S3method(xml_set_attrs,xml_nodeset)
S3method(xml_set_name,xml_missing)
S3method(xml_set_name,xml_node)
S3method(xml_set_name,xml_nodeset)
S3method(xml_text,xml_missing)
S3method(xml_text,xml_node)
S3method(xml_text,xml_nodeset)
S3method(xml_type,xml_missing)
S3method(xml_type,xml_node)
S3method(xml_type,xml_nodeset)
S3method(xml_url,xml_missing)
S3method(xml_url,xml_node)
S3method(xml_url,xml_nodeset)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# xml2 (development version)

* Remove unused dependencies on glue, withr and lifecycle (@mgirlich).
* `print()` is faster for very long `xml_nodeset` inputs (#366, @michaelchirico).

# xml2 1.3.5

Expand Down
28 changes: 14 additions & 14 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@ ns_lookup <- function(doc_sxp, node_sxp, prefix_sxp) {
.Call(`_xml2_ns_lookup`, doc_sxp, node_sxp, prefix_sxp)
}

node_name <- function(node_sxp, nsMap) {
.Call(`_xml2_node_name`, node_sxp, nsMap)
node_name <- function(x, nsMap) {
.Call(`_xml2_node_name`, x, nsMap)
}

node_set_name <- function(node_sxp, value) {
.Call(`_xml2_node_set_name`, node_sxp, value)
}

node_text <- function(node_sxp) {
.Call(`_xml2_node_text`, node_sxp)
node_text <- function(x) {
.Call(`_xml2_node_text`, x)
}

node_attr <- function(node_sxp, name_sxp, missing_sxp, nsMap_sxp) {
.Call(`_xml2_node_attr`, node_sxp, name_sxp, missing_sxp, nsMap_sxp)
node_attr <- function(x, name_sxp, missing_sxp, nsMap_sxp) {
.Call(`_xml2_node_attr`, x, name_sxp, missing_sxp, nsMap_sxp)
}

node_attrs <- function(node_sxp, nsMap_sxp) {
.Call(`_xml2_node_attrs`, node_sxp, nsMap_sxp)
node_attrs <- function(x, nsMap_sxp) {
.Call(`_xml2_node_attrs`, x, nsMap_sxp)
}

node_set_attr <- function(node_sxp, name_sxp, value, nsMap) {
Expand All @@ -96,8 +96,8 @@ node_children <- function(node_sxp, only_node_sxp) {
.Call(`_xml2_node_children`, node_sxp, only_node_sxp)
}

node_length <- function(node_sxp, only_node_sxp) {
.Call(`_xml2_node_length`, node_sxp, only_node_sxp)
node_length <- function(x, only_node_sxp) {
.Call(`_xml2_node_length`, x, only_node_sxp)
}

node_has_children <- function(node_sxp, only_node_sxp) {
Expand All @@ -116,16 +116,16 @@ node_parent <- function(node_sxp) {
.Call(`_xml2_node_parent`, node_sxp)
}

node_path <- function(node_sxp) {
.Call(`_xml2_node_path`, node_sxp)
node_path <- function(x) {
.Call(`_xml2_node_path`, x)
}

nodes_duplicated <- function(nodes) {
.Call(`_xml2_nodes_duplicated`, nodes)
}

node_type <- function(node_sxp) {
.Call(`_xml2_node_type`, node_sxp)
node_type <- function(x) {
.Call(`_xml2_node_type`, x)
}

node_copy <- function(node_sxp) {
Expand Down
43 changes: 2 additions & 41 deletions R/xml_attr.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,7 @@
#' xml_attrs(doc) <- c("b:id" = "one", "f:id" = "two", "id" = "three")
#' xml_set_attrs(doc, c("b:id" = "one", "f:id" = "two", "id" = "three"))
xml_attr <- function(x, attr, ns = character(), default = NA_character_) {
UseMethod("xml_attr")
}

#' @export
xml_attr.xml_missing <- function(x, attr, ns = character(), default = NA_character_) {
default
}

#' @export
xml_attr.xml_node <- function(x, attr, ns = character(),
default = NA_character_) {
node_attr(x$node, attr, as.character(default), ns)
}

#' @export
xml_attr.xml_nodeset <- function(x, attr, ns = character(),
default = NA_character_) {
vapply(
x,
xml_attr,
attr = attr,
default = default,
ns = ns,
FUN.VALUE = character(1)
)
node_attr(x, attr, as.character(default), ns)
}

#' @export
Expand All @@ -96,22 +72,7 @@ xml_has_attr <- function(x, attr, ns = character()) {
#' @export
#' @rdname xml_attr
xml_attrs <- function(x, ns = character()) {
UseMethod("xml_attrs")
}

#' @export
xml_attrs.xml_missing <- function(x, ns = character()) {
NA_character_
}

#' @export
xml_attrs.xml_node <- function(x, ns = character()) {
node_attrs(x$node, nsMap_sxp = ns)
}

#' @export
xml_attrs.xml_nodeset <- function(x, ns = character()) {
lapply(x, xml_attrs, ns = ns)
node_attrs(x, nsMap_sxp = ns)
}

#' @param value character vector of new value.
Expand Down
21 changes: 1 addition & 20 deletions R/xml_children.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,7 @@ xml_parent.xml_nodeset <- function(x) {
#' @export
#' @rdname xml_children
xml_length <- function(x, only_elements = TRUE) {
UseMethod("xml_length")
}

#' @export
xml_length.xml_missing <- function(x, only_elements = TRUE) {
0L
}

#' @export
xml_length.xml_node <- function(x, only_elements = TRUE) {
node_length(x$node, only_elements)
}

#' @export
xml_length.xml_nodeset <- function(x, only_elements = TRUE) {
if (length(x) == 0) {
return(0L)
}

vapply(x, xml_length, only_elements = only_elements, FUN.VALUE = integer(1))
node_length(x, only_elements)
}

#' @export
Expand Down
17 changes: 1 addition & 16 deletions R/xml_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,7 @@
#' z <- xml_children(y)
#' xml_name(xml_children(y))
xml_name <- function(x, ns = character()) {
UseMethod("xml_name")
}

#' @export
xml_name.xml_missing <- function(x, ns = character()) {
NA_character_
}

#' @export
xml_name.xml_nodeset <- function(x, ns = character()) {
vapply(x, xml_name, ns = ns, FUN.VALUE = character(1))
}

#' @export
xml_name.xml_node <- function(x, ns = character()) {
node_name(x$node, ns)
node_name(x, ns)
}

#' Modify the (tag) name of an element
Expand Down
29 changes: 21 additions & 8 deletions R/xml_nodeset.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ as.character.xml_nodeset <- function(x, ...) {
xml_nodeset(NextMethod())
}

#' Wrapper for encodeString() that takes width into consideration
#'
#' encodeString() is relatively expensive to run (see #366), so
#' avoid doing so to very wide inputs by first trimming inputs
#' to approximately the correct width, then encoding. A second
#' round of truncation occurs after encoding to account for
#' any newly-inserted characters bumping an input too wide.
#' @noRd
encode_with_width <- function(x, width) {
truncate_raw <- nchar(x) > width
x[truncate_raw] <- substr(x[truncate_raw], 1L, width - 3L)
x <- encodeString(x)
truncate_encoded <- truncate_raw | nchar(x) > width
x[truncate_encoded] <- paste(substr(x[truncate_encoded], 1L, width - 3L), "...")
x
}

show_nodes <- function(x, width = getOption("width"), max_n = 20) {
stopifnot(inherits(x, "xml_nodeset"))

Expand All @@ -46,20 +63,16 @@ show_nodes <- function(x, width = getOption("width"), max_n = 20) {
return()
}

if (n > max_n) {
trunc <- n > max_n
if (trunc) {
n <- max_n
x <- x[seq_len(n)]
trunc <- TRUE
} else {
trunc <- FALSE
}

label <- format(paste0("[", seq_len(n), "]"), justify = "right")
contents <- encodeString(vapply(x, as.character, FUN.VALUE = character(1)))
contents <- vapply(x, as.character, FUN.VALUE = character(1L))

desc <- paste0(label, " ", contents)
needs_trunc <- nchar(desc) > width
desc[needs_trunc] <- paste(substr(desc[needs_trunc], 1, width - 3), "...")
desc <- encode_with_width(paste(label, contents), width)

cat(desc, sep = "\n")
if (trunc) {
Expand Down
17 changes: 1 addition & 16 deletions R/xml_path.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,5 @@
#' x <- read_xml("<foo><bar><baz /></bar><baz /></foo>")
#' xml_path(xml_find_all(x, ".//baz"))
xml_path <- function(x) {
UseMethod("xml_path")
}

#' @export
xml_path.xml_missing <- function(x) {
NA_character_
}

#' @export
xml_path.xml_node <- function(x) {
node_path(x$node)
}

#' @export
xml_path.xml_nodeset <- function(x) {
vapply(x, xml_path, FUN.VALUE = character(1))
node_path(x)
}
56 changes: 5 additions & 51 deletions R/xml_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,16 @@
#' xml_integer(xml_find_all(x, "//@x"))
#' @export
xml_text <- function(x, trim = FALSE) {
UseMethod("xml_text")
}

#' @export
xml_text.xml_missing <- function(x, trim = FALSE) {
NA_character_
}

#' @export
xml_text.xml_node <- function(x, trim = FALSE) {
res <- node_text(x$node)
res <- node_text(x)
if (isTRUE(trim)) {
res <- sub("^[[:space:]\u00a0]+", "", res)
res <- sub("[[:space:]\u00a0]+$", "", res)
res <- trim_text(res)
}
res
}

#' @export
xml_text.xml_nodeset <- function(x, trim = FALSE) {
vapply(x, xml_text, trim = trim, FUN.VALUE = character(1))
trim_text <- function(x) {
x <- sub("^[[:space:]\u00a0]+", "", x)
sub("[[:space:]\u00a0]+$", "", x)
}

#' @rdname xml_text
Expand Down Expand Up @@ -94,46 +83,11 @@ xml_text.xml_nodeset <- function(x, trim = FALSE) {
#' @rdname xml_text
#' @export
xml_double <- function(x) {
UseMethod("xml_double")
}

#' @export
xml_double.xml_missing <- function(x) {
NA_real_
}

#' @export
xml_double.xml_node <- function(x) {
as.numeric(xml_text(x))
}

#' @export
xml_double.xml_nodeset <- function(x) {
vapply(x, xml_double, numeric(1))
}

#' @export
xml_integer <- function(x) {
UseMethod("xml_integer")
}

#' @export
xml_integer.xml_missing <- function(x) {
NA_integer_
}

#' @rdname xml_text
#' @export
xml_integer <- function(x) {
UseMethod("xml_integer")
}

#' @export
xml_integer.xml_node <- function(x) {
as.integer(xml_text(x))
}

#' @export
xml_integer.xml_nodeset <- function(x) {
vapply(x, xml_integer, integer(1))
}
Loading

0 comments on commit 0352dfa

Please sign in to comment.