diff options
| author | steven-vd <steven@vandorp.lu> | 2026-08-16 15:00:32 +0200 |
|---|---|---|
| committer | steven-vd <steven@vandorp.lu> | 2026-08-17 09:31:42 +0200 |
| commit | ee081ca70322cd01d0642fdb1860e6bfbfb48e85 (patch) | |
| tree | 295fdba93b08b24562c6d49e971bbf3425eac50c /benchmarks/pugixml/pubmed_len.cpp | |
Diffstat (limited to 'benchmarks/pugixml/pubmed_len.cpp')
| -rw-r--r-- | benchmarks/pugixml/pubmed_len.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/benchmarks/pugixml/pubmed_len.cpp b/benchmarks/pugixml/pubmed_len.cpp new file mode 100644 index 0000000..ec9e16c --- /dev/null +++ b/benchmarks/pugixml/pubmed_len.cpp @@ -0,0 +1,49 @@ +#include "pugixml.hpp" +#include <cstring> + +inline bool is_content_root(const char* name) { + return std::strcmp(name, "abstract") == 0 || std::strcmp(name, "body") == 0; +} + +// Sums length of all node_pcdata descendants (CDATA excluded to match +// quick-xml's Event::Text-only accounting). +size_t sum_pcdata(const pugi::xml_node& node) { + size_t total = 0; + for (pugi::xml_node child = node.first_child(); child; child = child.next_sibling()) { + if (child.type() == pugi::node_pcdata) { + total += std::strlen(child.value()); + } else if (child.type() == pugi::node_element) { + total += sum_pcdata(child); + } + } + return total; +} + +// Searches for <abstract>/<body> anywhere in the tree; once found, all +// descendant text counts regardless of further nesting. +size_t walk(const pugi::xml_node& node) { + size_t total = 0; + for (pugi::xml_node child = node.first_child(); child; child = child.next_sibling()) { + if (child.type() != pugi::node_element) continue; + if (is_content_root(child.name())) { + total += sum_pcdata(child); + } else { + total += walk(child); + } + } + return total; +} + +size_t pubmed_len(char* buffer, size_t len) { + pugi::xml_document doc; + unsigned int flags = pugi::parse_default | pugi::parse_escapes; + doc.load_buffer_inplace(buffer, len, flags); + return walk(doc); +} + +size_t pubmed_len_with_entities(char* buffer, size_t len) { + pugi::xml_document doc; + unsigned int flags = pugi::parse_default; + doc.load_buffer_inplace(buffer, len, flags); + return walk(doc); +} |
