diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 113fbf5..98789a0 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -773,3 +773,69 @@ void FileUtils::getTxtContent(QString &path, QString &textcontent) { return; } + +QString FileUtils::chineseSubString(const std::string &data, int start, int length) +{ + std::string afterSub = ""; + //越界保护 + if(start < 0 || length < 0){ + return " "; + } + if (length >= data.length()) { + return QString::fromStdString(data); + } + + QString snippet = ""; + QFont ft(QApplication::font().family(),QApplication::font().pointSize()); + QFontMetrics fm (ft); + + if (start + length <= data.length()) { + afterSub = data.substr(start,length); //截取 + snippet = QString::fromStdString(afterSub); //转QString + + if(start + length < data.length()){ + snippet.replace(snippet.length()-3,3,"..."); //替换后三位 + } + snippet = fm.elidedText(snippet, Qt::ElideRight, 2*366); //超过两行则省略 + } else { + int newStart = data.length()-length; //更新截取位置 + int bodyByteCount = 0; + int letterCount = 0; + + for(int i = newStart;i > 0;i--){ + if(data[i] & 0x80) { + char tmp = data[i] << 1; + if(tmp & 0x80){ + break; + } else { + bodyByteCount++; + } + + } else { + letterCount++; + if(letterCount == 3) { + break; + } + + } + } + int correctionValue = letterCount + (bodyByteCount); + afterSub = data.substr(newStart - correctionValue, length + correctionValue); + snippet=QString::fromStdString(afterSub); + + switch(letterCount) + { + case 0: + snippet.replace(0,1,".").prepend(".."); + break; + case 1: + snippet.replace(0,2,"..").prepend("."); + break; + default: + snippet.replace(0,3,"..."); + } + + snippet = fm.elidedText(snippet, Qt::ElideLeft, 2*500); + } + return snippet; +} diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index f0a9085..b3ef3ae 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include @@ -83,6 +85,7 @@ public: static void getXlsxTextContent(QString &path, QString &textcontent); static void getPdfTextContent(QString &path, QString &textcontent); static void getTxtContent(QString &path, QString &textcontent); + static QString chineseSubString(const std::string &data, int start, int length); static size_t _max_index_count; static size_t _current_index_count; //this one has been Abandoned,do not use it. static unsigned short _index_status; diff --git a/libsearch/index/search-manager.cpp b/libsearch/index/search-manager.cpp index c501594..09edbf1 100644 --- a/libsearch/index/search-manager.cpp +++ b/libsearch/index/search-manager.cpp @@ -338,22 +338,26 @@ int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) { // qWarning()< 6 + QString::fromStdString(keyWord).size()) { - snippet.replace(0, 3, "...").replace(snippet.size() - 3, 3, "..."); - } else { - snippet.append("...").prepend("..."); - } - snippets.append(snippet); - QString().swap(snippet); - std::string().swap(s); - ++count; - } + auto pos = term.positionlist_begin(); + int length = 120; + QString snippet = FileUtils::chineseSubString(data,*pos,length); +// int count = 0; +// for(auto pos = term.positionlist_begin(); pos != term.positionlist_end() && count < 6; ++pos) { +// std::string s = data.substr((*pos < 60) ? 0 : (*pos - 60), size + 120); +// QString snippet = QString::fromStdString(s); +// if(snippet.size() > 6 + QString::fromStdString(keyWord).size()) { +// snippet.replace(0, 3, "...").replace(snippet.size() - 3, 3, "..."); +// } else { +// snippet.append("...").prepend("..."); +// } +// snippets.append(snippet); +// QString().swap(snippet); +// std::string().swap(s); +// ++count; +// } + snippets.append(snippet); std::string().swap(data); // for(QString i : QString::fromStdString(keyWord).split(" ",QString::SkipEmptyParts))