From cd4d2e98c34ed413ca164dbad61ba19636377f77 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 19 Jul 2021 19:28:45 +0200 Subject: Add hotexamples --- src/NetUtils.cpp | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'src/NetUtils.cpp') diff --git a/src/NetUtils.cpp b/src/NetUtils.cpp index cc19094..28256cb 100644 --- a/src/NetUtils.cpp +++ b/src/NetUtils.cpp @@ -34,12 +34,43 @@ namespace QuickMedia { std::string unescaped_str; }; + static bool to_num(const char *str, size_t size, int &num) { + num = 0; + for(size_t i = 0; i < size; ++i) { + const char num_c = str[i] - '0'; + if(num_c < 0 || num_c > 9) + return false; + num = (num * 10) + num_c; + } + return true; + } + + static void html_unescape_sequence_numbers(std::string &str) { + size_t index = 0; + while(true) { + index = str.find("&#", index); + if(index == std::string::npos) + break; + + index += 2; + size_t end_index = str.find(';', index); + if(end_index != std::string::npos && end_index - index <= 3) { + const size_t num_length = end_index - index; + int num; + if(to_num(str.c_str() + index, num_length, num)) { + const char num_c = (char)num; + str.replace(index - 2, 2 + num_length + 1, &num_c, 1); + index += (-2 + 1); + } + } + } + } + void html_unescape_sequences(std::string &str) { - const std::array unescape_sequences = { + html_unescape_sequence_numbers(str); + + const std::array unescape_sequences = { HtmlUnescapeSequence { """, "\"" }, - HtmlUnescapeSequence { "'", "'" }, - HtmlUnescapeSequence { "'", "'" }, - HtmlUnescapeSequence { " ", "\n" }, HtmlUnescapeSequence { "<", "<" }, HtmlUnescapeSequence { ">", ">" }, HtmlUnescapeSequence { "&", "&" } // This should be last, to not accidentally replace a new sequence caused by replacing this -- cgit v1.2.3