From ce06c25e93183282be42ab79bfed2ab7c02791ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 20 Oct 2016 19:32:20 +0200 Subject: Very little optimization by using a simpler scope_guard when possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The version with the vector, that can be disabled etc, is “very” slow, so we use unique_ptr when we don’t need to disable it, and when it only contains one function --- louloulibs/utils/encoding.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'louloulibs/utils/encoding.cpp') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index 507f38a..4b20797 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -76,7 +76,7 @@ namespace utils { // The given string MUST be a valid utf-8 string unsigned char* res = new unsigned char[original.size()]; - ScopeGuard sg([&res]() { delete[] res;}); + const auto sg = utils::make_scope_guard([&res](auto&&) { delete[] res;}); // pointer where we write valid chars unsigned char* r = res; @@ -140,7 +140,7 @@ namespace utils else throw std::runtime_error("Invalid UTF-8 passed to remove_invalid_xml_chars"); } - return std::string(reinterpret_cast(res), r-res); + return {reinterpret_cast(res), static_cast(r-res)}; } std::string convert_to_utf8(const std::string& str, const char* charset) @@ -152,7 +152,7 @@ namespace utils throw std::runtime_error("Cannot convert into UTF-8"); // Make sure cd is always closed when we leave this function - ScopeGuard sg([&]{ iconv_close(cd); }); + const auto sg = utils::make_scope_guard([&](auto&&){ iconv_close(cd); }); size_t inbytesleft = str.size(); @@ -169,7 +169,7 @@ namespace utils char* outbuf_ptr = outbuf; // Make sure outbuf is always deleted when we leave this function - sg.add_callback([&]{ delete[] outbuf; }); + const auto sg2 = utils::make_scope_guard([&](auto&&){ delete[] outbuf; }); bool done = false; while (done == false) -- cgit v1.2.3 From ac61450184112ccb22971cff6cfa6117b4ddfbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 27 Oct 2016 01:37:55 +0200 Subject: Refactor remove_invalid_xml_chars to use correct types directly --- louloulibs/utils/encoding.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'louloulibs/utils/encoding.cpp') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index 4b20797..712028e 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -75,13 +75,12 @@ namespace utils std::string remove_invalid_xml_chars(const std::string& original) { // The given string MUST be a valid utf-8 string - unsigned char* res = new unsigned char[original.size()]; - const auto sg = utils::make_scope_guard([&res](auto&&) { delete[] res;}); + std::vector res(original.size(), '\0'); // pointer where we write valid chars - unsigned char* r = res; + char* r = res.data(); - const unsigned char* str = reinterpret_cast(original.c_str()); + const char* str = original.c_str(); std::bitset<20> codepoint; while (*str) @@ -140,7 +139,7 @@ namespace utils else throw std::runtime_error("Invalid UTF-8 passed to remove_invalid_xml_chars"); } - return {reinterpret_cast(res), static_cast(r-res)}; + return {res.data(), static_cast(r - res.data())}; } std::string convert_to_utf8(const std::string& str, const char* charset) -- cgit v1.2.3 From ae02e58b9dc276b247be84e1d708ca50a1f5bbd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 31 Oct 2016 13:54:25 +0100 Subject: Some cleanups --- louloulibs/utils/encoding.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'louloulibs/utils/encoding.cpp') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index 712028e..cb953c0 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -196,12 +196,8 @@ namespace utils outbuf_ptr++; done = true; break; - case E2BIG: - // This should never happen - done = true; - break; - default: - // This should happen even neverer + case E2BIG: // This should never happen + default: // This should happen even neverer done = true; break; } -- cgit v1.2.3 From f50f50653dc064575e4730c31b5615301f00e057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 1 Nov 2016 19:43:56 +0100 Subject: Refactor load_certs() --- louloulibs/utils/encoding.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'louloulibs/utils/encoding.cpp') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index cb953c0..60f2212 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -151,7 +151,7 @@ namespace utils throw std::runtime_error("Cannot convert into UTF-8"); // Make sure cd is always closed when we leave this function - const auto sg = utils::make_scope_guard([&](auto&&){ iconv_close(cd); }); + const auto sg = utils::make_scope_guard([&cd](auto&&){ iconv_close(cd); }); size_t inbytesleft = str.size(); @@ -168,7 +168,7 @@ namespace utils char* outbuf_ptr = outbuf; // Make sure outbuf is always deleted when we leave this function - const auto sg2 = utils::make_scope_guard([&](auto&&){ delete[] outbuf; }); + const auto sg2 = utils::make_scope_guard([outbuf](auto&&){ delete[] outbuf; }); bool done = false; while (done == false) -- cgit v1.2.3