From 08a6dbc3c8a3253ac15a882001c9c60b31f33ad7 Mon Sep 17 00:00:00 2001 From: niansa Date: Tue, 10 Nov 2020 16:16:44 +0100 Subject: [PATCH] Improved libcommsy performance --- libcommsy.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libcommsy.cpp b/libcommsy.cpp index 4fb4df0..98ec5fb 100644 --- a/libcommsy.cpp +++ b/libcommsy.cpp @@ -69,7 +69,7 @@ std::vector merge_strvects(std::vector base, const std extern long curlreq(std::stringstream &responsebuffer, std::string SID, std::string URL); -void gumbo_search_by_attr(std::vector &elemvect, GumboNode* node, std::string attrname, std::string searchword, GumboTag expectedtag) { +void gumbo_search_by_attr(std::vector *elemvect, GumboNode* node, const std::string& attrname, const std::string& searchword, const GumboTag& expectedtag) { if (node->type != GUMBO_NODE_ELEMENT) { return; } @@ -78,7 +78,7 @@ void gumbo_search_by_attr(std::vector &elemvect, GumboNode* node, s if (node->v.element.tag == expectedtag && (hclass = gumbo_get_attribute(&node->v.element.attributes, attrname.c_str()))) { if (hclass->value == searchword) { - elemvect.push_back(node); + elemvect->push_back(node); } } @@ -88,13 +88,13 @@ void gumbo_search_by_attr(std::vector &elemvect, GumboNode* node, s } } -void gumbo_search_by_class(std::vector &elemvect, GumboNode* node, std::string searchword, GumboTag expectedtag) { +inline void gumbo_search_by_class(std::vector *elemvect, GumboNode* node, const std::string& searchword, const GumboTag& expectedtag) { return gumbo_search_by_attr(elemvect, node, "class", searchword, expectedtag); } -GumboNode *gumbo_search_by_id(GumboNode* node, std::string searchword, GumboTag expectedtag) { +GumboNode *gumbo_search_by_id(GumboNode* node, const std::string& searchword, const GumboTag& expectedtag) { std::vector elemvect; - gumbo_search_by_attr(elemvect, node, "id", searchword, expectedtag); + gumbo_search_by_attr(&elemvect, node, "id", searchword, expectedtag); // Use first node found if (elemvect.size() > 0) { return elemvect[0]; @@ -103,13 +103,13 @@ GumboNode *gumbo_search_by_id(GumboNode* node, std::string searchword, GumboTag throw parsingNoSuchIDError(); } -void gumbo_search_by_tag(std::vector &elemvect, GumboNode* node, GumboTag searchedtag) { +void gumbo_search_by_tag(std::vector *elemvect, GumboNode* node, const GumboTag& searchedtag) { if (node->type != GUMBO_NODE_ELEMENT) { return; } if (node->v.element.tag == searchedtag) { - elemvect.push_back(node); + elemvect->push_back(node); } GumboVector* children = &node->v.element.children; @@ -144,7 +144,7 @@ static std::string gumbo_cleantext(GumboNode* node) { } } -std::vector gumbo_get_attr(GumboNode *node, std::string attrkey, GumboTag expected_tag) { +std::vector gumbo_get_attr(GumboNode *node, const std::string& attrkey, const GumboTag& expected_tag) { std::vector attrvals; GumboNode *childnode; GumboVector* children = &node->v.element.children; @@ -176,7 +176,7 @@ std::vector gumbo_get_attr(GumboNode *node, std::string attrkey, Gu return attrvals; } -std::string gumbo_find_text_by_tag(GumboNode *node, GumboTag searchtag) { +std::string gumbo_find_text_by_tag(GumboNode *node, const GumboTag& searchtag) { GumboNode *childnode; GumboVector* children = &node->v.element.children; @@ -194,13 +194,13 @@ std::string gumbo_find_text_by_tag(GumboNode *node, GumboTag searchtag) { auto get_posts(GumboNode *node) { std::vector posts; - gumbo_search_by_class(posts, node, "uk-comment", GUMBO_TAG_ARTICLE); + gumbo_search_by_class(&posts, node, "uk-comment", GUMBO_TAG_ARTICLE); return posts; } std::string get_post_name(GumboNode *node) { std::vector titlenodes; - gumbo_search_by_class(titlenodes, node, "uk-comment-title", GUMBO_TAG_H4); + gumbo_search_by_class(&titlenodes, node, "uk-comment-title", GUMBO_TAG_H4); return trim(gumbo_cleantext(titlenodes[0])); } @@ -210,19 +210,19 @@ std::string get_post_id(GumboNode *node) { std::string get_post_meta(GumboNode *node) { std::vector metanodes; - gumbo_search_by_class(metanodes, node, "uk-comment-meta", GUMBO_TAG_DIV); + gumbo_search_by_class(&metanodes, node, "uk-comment-meta", GUMBO_TAG_DIV); return clean_spaces(trim(gumbo_cleantext(metanodes[1]))); } std::string get_post_url(GumboNode *node) { std::vector titlenodes; - gumbo_search_by_class(titlenodes, node, "uk-comment-title", GUMBO_TAG_H4); + gumbo_search_by_class(&titlenodes, node, "uk-comment-title", GUMBO_TAG_H4); return gumbo_get_attr(titlenodes[0], "href", GUMBO_TAG_A)[0]; } bool get_post_unread(GumboNode *node) { std::vector elems; - gumbo_search_by_class(elems, node, "cs-comment-change-info", GUMBO_TAG_DIV); + gumbo_search_by_class(&elems, node, "cs-comment-change-info", GUMBO_TAG_DIV); return !elems.empty(); } @@ -253,7 +253,7 @@ std::vector> get_post_files(GumboNode *node) std::map tmpmap; // Get meta nodes - gumbo_search_by_class(metanodes, node, "uk-comment-meta", GUMBO_TAG_DIV); + gumbo_search_by_class(&metanodes, node, "uk-comment-meta", GUMBO_TAG_DIV); // Get URLs fileurls = gumbo_get_attr(metanodes[2], "href", GUMBO_TAG_A); // Get filenames @@ -301,7 +301,7 @@ std::string get_post_desc(const std::string& post_url, const std::string& server // Get description element desc_node = gumbo_search_by_id(post_document->root, "description" + material_id, GUMBO_TAG_DIV); // Extract description - gumbo_search_by_tag(results, desc_node, GUMBO_TAG_P); + gumbo_search_by_tag(&results, desc_node, GUMBO_TAG_P); // Cencenate occurencies std::string result_string;