Files
小子 bf82b8ceba Add search function (#676)
* Add search func

* Add interface judgment

* Add extra-contents_path search field judgment in ts file

* Added search box prompt

* Remove qm files

* Add missing header files

* Solve the problem of type conversion errors
2020-08-18 09:03:28 +08:00

58 lines
1.1 KiB
C++

#include "pinyin.h"
#include <QFile>
#include <QTextStream>
static QHash<uint, QString> dict = {};
const char kDictFile[] = ":/dpinyin.dict";
static void InitDict() {
if (!dict.isEmpty()) {
return;
}
dict.reserve(25333);
QFile file(kDictFile);
if (!file.open(QIODevice::ReadOnly))
return;
QByteArray content = file.readAll();
file.close();
QTextStream stream(&content, QIODevice::ReadOnly);
while (!stream.atEnd()) {
const QString line = stream.readLine();
const QStringList items = line.split(QChar(':'));
if (items.size() == 2) {
dict.insert(items[0].toInt(nullptr, 16), items[1]);
}
}
}
QString Chinese2Pinyin(const QString &words)
{
InitDict();
QString result;
for (int i = 0; i < words.length(); ++i) {
const uint key = words.at(i).unicode();
auto find_result = dict.find(key);
if (find_result != dict.end()) {
result.append(find_result.value());
} else {
result.append(words.at(i));
}
}
return result;
}