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(-) 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