From 400d263e404e6c0fac5be8319013554a50659e42 Mon Sep 17 00:00:00 2001 From: xiaoyifang Date: Sat, 16 Apr 2022 15:14:26 +0800 Subject: [PATCH] fix:mdx compact html ,the end close tag error some mdx entry item has such item `1`abc.jpg\r\n\u0000 when replaced with predefined tags. will result in **** --- mdictparser.cc | 5 ++--- utils.hh | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/mdictparser.cc b/mdictparser.cc index dee23a45..882bc75f 100644 --- a/mdictparser.cc +++ b/mdictparser.cc @@ -40,6 +40,7 @@ #include "decompress.hh" #include "gddebug.hh" #include "ripemd.hh" +#include "utils.hh" namespace Mdict { @@ -183,8 +184,6 @@ QString MdictParser::toUtf16( const char * fromCode, const char * from, size_t f if ( !fromCode || !from ) return QString(); - - QTextCodec *codec =QTextCodec::codecForName(fromCode); return codec->toUnicode(from,fromSize); } @@ -647,7 +646,7 @@ QString & MdictParser::substituteStylesheet( QString & article, MdictParser::Sty } if( pos ) { - articleNewText += article.mid( pos ); + articleNewText += Utils::rstripnull( article.mid( pos )); article = articleNewText; articleNewText.clear(); } diff --git a/utils.hh b/utils.hh index 299823d5..eb6cd472 100644 --- a/utils.hh +++ b/utils.hh @@ -26,6 +26,22 @@ inline QString rstrip(const QString &str) { return ""; } +/** + * str="abc\r\n\u0000" should be returned as "abc" + * @brief rstripnull + * @param str + * @return + */ +inline QString rstripnull(const QString &str) { + int n = str.size() - 1; + for (; n >= 0; --n) { + if (!str.at(n).isSpace()&&!str.at(n).isNull()) { + return str.left(n + 1); + } + } + return ""; +} + inline bool isExternalLink(QUrl const &url) { return url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp" || url.scheme() == "mailto" ||