Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cpp11 #405

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5b11a52
`use_cpp11()`
mgirlich Sep 4, 2023
1fd06ac
Use cpp11 register mechanism
mgirlich Sep 4, 2023
48ab979
Migrate `xml2_url.cpp`
mgirlich Sep 4, 2023
992cd01
Replace `extern "C"` by `cpp11::sexp`
mgirlich Sep 4, 2023
44eddbe
Remove unused `R_ReadConnection()`
mgirlich Sep 4, 2023
66f4c60
Remove unnecessary `R_GetConnection()`
mgirlich Sep 4, 2023
cd54aed
Migrate `xml2_output.cpp`
mgirlich Sep 4, 2023
e351747
Convert `xml2_doc.cpp`
mgirlich Sep 4, 2023
60ae697
Convert `xml2_schema.cpp`
mgirlich Sep 4, 2023
ae3a6ec
Convert `xml2_path.cpp`
mgirlich Sep 4, 2023
33d6c7e
Fix compilation issues
mgirlich Sep 4, 2023
8029c30
Minor things
mgirlich Sep 4, 2023
33561d8
As `as_cpp<int>()`
mgirlich Nov 2, 2023
10f3f75
Use `R_xlen_t` for `size()` method
mgirlich Nov 2, 2023
d43f4bf
Use cpp11 raws with memcpy
mgirlich Nov 2, 2023
ea377a9
Check for empty vectors as well
mgirlich Nov 2, 2023
51cbbaf
Merge commit '005b78659c2271a03ed016f13df0d32f8b1e66e5'
mgirlich Nov 2, 2023
31e7555
Fix ambiguous initializer
mgirlich Nov 2, 2023
b7ff753
More cpp11 i `xml2_xpath.cpp`
mgirlich Nov 2, 2023
50489ac
Minor changes
mgirlich Nov 2, 2023
6f0e353
Use cpp11 in `xml2_utils.h`
mgirlich Nov 2, 2023
0352dfa
Merge commit 'e088a1c892e97436fdd3263c6cfc6df61e47a365'
mgirlich Nov 2, 2023
2539195
Use cpp11 in `xml2_node.cpp`
mgirlich Nov 3, 2023
b65e011
Fix compilation issues
mgirlich Nov 3, 2023
f73bb41
Use cpp11 in namespace and connection
mgirlich Nov 3, 2023
2e85921
Remove cpp wrapper macro
mgirlich Nov 3, 2023
01c41e7
Some more cleanup
mgirlich Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SystemRequirements: libxml2: libxml2-dev (deb), libxml2-devel (rpm)
Collate:
'S4.R'
'as_list.R'
'cpp11.R'
'xml_parse.R'
'as_xml_document.R'
'classes.R'
Expand Down Expand Up @@ -74,3 +75,5 @@ Collate:
'xml_write.R'
'zzz.R'
Config/testthat/edition: 3
LinkingTo:
cpp11
2 changes: 1 addition & 1 deletion R/as_xml_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ as_xml_document.list <- function(x, ...) {

add_node <- function(x, parent, tag = NULL) {
if (is.atomic(x)) {
return(.Call(node_new_text, parent$node, as.character(x)))
return(node_new_text(parent$node, as.character(x)))
}
if (!is.null(tag)) {
parent <- xml_add_child(parent, tag)
Expand Down
253 changes: 253 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
# Generated by cpp11: do not edit by hand

read_connection_ <- function(con_sxp, read_size_sxp) {
.Call(`_xml2_read_connection_`, con_sxp, read_size_sxp)
}

xml_parse_options_ <- function() {
.Call(`_xml2_xml_parse_options_`)
}

doc_parse_file <- function(path_sxp, encoding_sxp, as_html_sxp, options_sxp) {
.Call(`_xml2_doc_parse_file`, path_sxp, encoding_sxp, as_html_sxp, options_sxp)
}

doc_parse_raw <- function(x, encoding_sxp, base_url_sxp, as_html_sxp, options_sxp) {
.Call(`_xml2_doc_parse_raw`, x, encoding_sxp, base_url_sxp, as_html_sxp, options_sxp)
}

doc_root <- function(x) {
.Call(`_xml2_doc_root`, x)
}

doc_has_root <- function(x_sxp) {
.Call(`_xml2_doc_has_root`, x_sxp)
}

doc_url <- function(doc_sxp) {
.Call(`_xml2_doc_url`, doc_sxp)
}

doc_new <- function(version_sxp, encoding_sxp) {
.Call(`_xml2_doc_new`, version_sxp, encoding_sxp)
}

doc_set_root <- function(doc_sxp, root_sxp) {
.Call(`_xml2_doc_set_root`, doc_sxp, root_sxp)
}

doc_is_html <- function(doc_sxp) {
.Call(`_xml2_doc_is_html`, doc_sxp)
}

init_libxml2 <- function() {
.Call(`_xml2_init_libxml2`)
}

libxml2_version_ <- function() {
.Call(`_xml2_libxml2_version_`)
}

unique_ns <- function(ns) {
.Call(`_xml2_unique_ns`, ns)
}

doc_namespaces <- function(doc_sxp) {
.Call(`_xml2_doc_namespaces`, doc_sxp)
}

ns_lookup_uri <- function(doc_sxp, node_sxp, uri_sxp) {
.Call(`_xml2_ns_lookup_uri`, doc_sxp, node_sxp, uri_sxp)
}

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_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_attr <- function(node_sxp, name_sxp, missing_sxp, nsMap_sxp) {
.Call(`_xml2_node_attr`, node_sxp, name_sxp, missing_sxp, nsMap_sxp)
}

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

node_set_attr <- function(node_sxp, name_sxp, value, nsMap) {
.Call(`_xml2_node_set_attr`, node_sxp, name_sxp, value, nsMap)
}

node_remove_attr <- function(node_sxp, name_sxp, nsMap) {
.Call(`_xml2_node_remove_attr`, node_sxp, name_sxp, nsMap)
}

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_has_children <- function(node_sxp, only_node_sxp) {
.Call(`_xml2_node_has_children`, node_sxp, only_node_sxp)
}

node_parents <- function(node_sxp) {
.Call(`_xml2_node_parents`, node_sxp)
}

node_siblings <- function(node_sxp, only_node_sxp) {
.Call(`_xml2_node_siblings`, node_sxp, only_node_sxp)
}

node_parent <- function(node_sxp) {
.Call(`_xml2_node_parent`, node_sxp)
}

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

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

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

node_copy <- function(node_sxp) {
.Call(`_xml2_node_copy`, node_sxp)
}

node_set_content <- function(node_sxp, content) {
.Call(`_xml2_node_set_content`, node_sxp, content)
}

node_append_content <- function(node_sxp, content) {
.Call(`_xml2_node_append_content`, node_sxp, content)
}

node_new_text <- function(node_sxp, content) {
.Call(`_xml2_node_new_text`, node_sxp, content)
}

node_append_child <- function(parent_sxp, cur_sxp) {
.Call(`_xml2_node_append_child`, parent_sxp, cur_sxp)
}

node_prepend_child <- function(parent_sxp, cur_sxp) {
.Call(`_xml2_node_prepend_child`, parent_sxp, cur_sxp)
}

node_prepend_sibling <- function(cur_sxp, elem_sxp) {
.Call(`_xml2_node_prepend_sibling`, cur_sxp, elem_sxp)
}

node_append_sibling <- function(cur_sxp, elem_sxp) {
.Call(`_xml2_node_append_sibling`, cur_sxp, elem_sxp)
}

node_replace <- function(old_sxp, cur_sxp) {
.Call(`_xml2_node_replace`, old_sxp, cur_sxp)
}

node_remove <- function(node_sxp, free_sxp) {
.Call(`_xml2_node_remove`, node_sxp, free_sxp)
}

node_new <- function(name) {
.Call(`_xml2_node_new`, name)
}

node_cdata_new <- function(doc_sxp, content_sxp) {
.Call(`_xml2_node_cdata_new`, doc_sxp, content_sxp)
}

node_comment_new <- function(content) {
.Call(`_xml2_node_comment_new`, content)
}

node_new_ns <- function(name, ns_sxp) {
.Call(`_xml2_node_new_ns`, name, ns_sxp)
}

node_set_namespace_uri <- function(doc_sxp, node_sxp, uri) {
.Call(`_xml2_node_set_namespace_uri`, doc_sxp, node_sxp, uri)
}

node_set_namespace_prefix <- function(doc_sxp, node_sxp, prefix_sxp) {
.Call(`_xml2_node_set_namespace_prefix`, doc_sxp, node_sxp, prefix_sxp)
}

node_new_dtd <- function(doc_sxp, name_sxp, eid_sxp, sid_sxp) {
.Call(`_xml2_node_new_dtd`, doc_sxp, name_sxp, eid_sxp, sid_sxp)
}

xml_save_options_ <- function() {
.Call(`_xml2_xml_save_options_`)
}

doc_write_file <- function(doc_sxp, path_sxp, encoding_sxp, options_sxp) {
.Call(`_xml2_doc_write_file`, doc_sxp, path_sxp, encoding_sxp, options_sxp)
}

doc_write_connection <- function(doc_sxp, connection, encoding_sxp, options_sxp) {
.Call(`_xml2_doc_write_connection`, doc_sxp, connection, encoding_sxp, options_sxp)
}

doc_write_character <- function(doc_sxp, encoding_sxp, options_sxp) {
.Call(`_xml2_doc_write_character`, doc_sxp, encoding_sxp, options_sxp)
}

node_write_file <- function(node_sxp, path_sxp, encoding_sxp, options_sxp) {
.Call(`_xml2_node_write_file`, node_sxp, path_sxp, encoding_sxp, options_sxp)
}

node_write_connection <- function(node_sxp, connection, encoding_sxp, options_sxp) {
.Call(`_xml2_node_write_connection`, node_sxp, connection, encoding_sxp, options_sxp)
}

node_write_character <- function(node_sxp, encoding_sxp, options_sxp) {
.Call(`_xml2_node_write_character`, node_sxp, encoding_sxp, options_sxp)
}

doc_validate <- function(doc_sxp, schema_sxp) {
.Call(`_xml2_doc_validate`, doc_sxp, schema_sxp)
}

url_absolute_ <- function(x_sxp, base_sxp) {
.Call(`_xml2_url_absolute_`, x_sxp, base_sxp)
}

url_relative_ <- function(x_sxp, base_sxp) {
.Call(`_xml2_url_relative_`, x_sxp, base_sxp)
}

url_parse_ <- function(x_sxp) {
.Call(`_xml2_url_parse_`, x_sxp)
}

url_escape_ <- function(x_sxp, reserved_sxp) {
.Call(`_xml2_url_escape_`, x_sxp, reserved_sxp)
}

url_unescape_ <- function(x_sxp) {
.Call(`_xml2_url_unescape_`, x_sxp)
}

xpath_search <- function(node_sxp, doc_sxp, xpath_sxp, nsMap_sxp, num_results_sxp) {
.Call(`_xml2_xpath_search`, node_sxp, doc_sxp, xpath_sxp, nsMap_sxp, num_results_sxp)
}
8 changes: 4 additions & 4 deletions R/init.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.onLoad <- function(lib, pkg) {
.Call(init_libxml2)
init_libxml2()
}

libxml2_version <- function() {
as.numeric_version(.Call(libxml2_version_))
as.numeric_version(libxml2_version_())
}

xml_parse_options <- function() {
.Call(xml_parse_options_)
xml_parse_options_()
}

xml_save_options <- function() {
.Call(xml_save_options_)
xml_save_options_()
}
1 change: 1 addition & 0 deletions R/xml2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#' @import rlang
#' @importFrom glue glue
#' @importFrom lifecycle deprecated
#' @useDynLib xml2, .registration = TRUE
## usethis namespace: end
NULL
8 changes: 4 additions & 4 deletions R/xml_attr.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ xml_attr.xml_missing <- function(x, attr, ns = character(), default = NA_charact
#' @export
xml_attr.xml_node <- function(x, attr, ns = character(),
default = NA_character_) {
.Call(node_attr, x$node, attr, as.character(default), ns)
node_attr(x$node, attr, as.character(default), ns)
}

#' @export
Expand Down Expand Up @@ -106,7 +106,7 @@ xml_attrs.xml_missing <- function(x, ns = character()) {

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

#' @export
Expand All @@ -124,10 +124,10 @@ xml_attrs.xml_nodeset <- function(x, ns = character()) {
#' @export
`xml_attr<-.xml_node` <- function(x, attr, ns = character(), value) {
if (is.null(value)) {
.Call(node_remove_attr, x$node, attr, ns)
node_remove_attr(x$node, attr, ns)
} else {
value <- as.character(value)
.Call(node_set_attr, x$node, attr, value, ns)
node_set_attr(x$node, attr, value, ns)
}
x
}
Expand Down
16 changes: 8 additions & 8 deletions R/xml_children.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#' xml_child(x, 2)
#' xml_child(x, "baz")
xml_children <- function(x) {
nodeset_apply(x, function(x) .Call(node_children, x, TRUE))
nodeset_apply(x, function(x) node_children(x, TRUE))
}

#' @export
Expand All @@ -60,19 +60,19 @@ xml_child <- function(x, search = 1, ns = xml_ns(x)) {
#' @export
#' @rdname xml_children
xml_contents <- function(x) {
nodeset_apply(x, function(x) .Call(node_children, x, FALSE))
nodeset_apply(x, function(x) node_children(x, FALSE))
}

#' @export
#' @rdname xml_children
xml_parents <- function(x) {
nodeset_apply(x, function(x) .Call(node_parents, x))
nodeset_apply(x, function(x) node_parents(x))
}

#' @export
#' @rdname xml_children
xml_siblings <- function(x) {
nodeset_apply(x, function(x) .Call(node_siblings, x, TRUE))
nodeset_apply(x, function(x) node_siblings(x, TRUE))
}

#' @export
Expand All @@ -88,12 +88,12 @@ xml_parent.xml_missing <- function(x) {

#' @export
xml_parent.xml_node <- function(x) {
xml_node(.Call(node_parent, x$node), x$doc)
xml_node(node_parent(x$node), x$doc)
}

#' @export
xml_parent.xml_nodeset <- function(x) {
nodeset_apply(x, function(x) .Call(node_parent, x))
nodeset_apply(x, function(x) node_parent(x))
}


Expand All @@ -110,7 +110,7 @@ xml_length.xml_missing <- function(x, only_elements = TRUE) {

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

#' @export
Expand All @@ -134,7 +134,7 @@ xml_root <- function(x) {
return(xml_root(x[[1]]))
}
}
if (!.Call(doc_has_root, x$doc)) {
if (!doc_has_root(x$doc)) {
xml_missing()
} else {
xml_document(x$doc)
Expand Down
Loading
Loading