-
Notifications
You must be signed in to change notification settings - Fork 38
/
bhj_help.cpp
93 lines (83 loc) · 2.4 KB
/
bhj_help.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "bhj_help.hpp"
#include <QMap>
#include <QFile>
#include <QTextStream>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QDebug>
bool matchOneString(const QStringList& strList, const QString& str)
{
foreach(const QString& entry, strList) {
if (entry.contains(str, Qt::CaseInsensitive)) {
return true;
}
}
return false;
}
QStringList filterMatchedStrings(const QStringList& strList, const QString& str)
{
QStringList res;
foreach(const QString& entry, strList) {
if (entry.contains(str, Qt::CaseInsensitive)) {
res << entry;
}
}
return res;
}
QStringList getPinyinSpelling(const QString& str, bool only_first_spell)
{
if (only_first_spell == 0 && str.size() > 20) {
only_first_spell = 1;
}
static QMap<QString, QStringList> pinYinMap;
if (pinYinMap.isEmpty()) {
QFile py("uc-to-py.tbl");
if (!py.open(QIODevice::ReadOnly | QIODevice::Text)) {
return QStringList(str);
}
QTextStream in(&py);
QRegularExpression re("^([0-9a-f]{4})\\s+\\((.*?)\\)\\s*$", QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match;
QRegularExpression splitRe(":|[0-9]|,");
while (!in.atEnd()) {
QString line = in.readLine();
if (line.contains(re, &match)) {
QString chr = match.captured(1);
QString pinyinStr = match.captured(2);
QStringList pinyinList = pinyinStr.split(splitRe, QString::SkipEmptyParts);
QChar ch = chr.toInt(0, 16);
pinYinMap[ch] = pinyinList;
}
}
}
QStringList res("");
foreach(const QChar& ch, str) {
QStringList pyList;
if (pinYinMap.contains(QString(ch))) {
if (only_first_spell) {
pyList << pinYinMap[QString(ch)][0];
} else {
pyList = pinYinMap[QString(ch)];
}
} else {
pyList << ch;
}
QStringList midRes;
foreach(const QString& pre, res) {
foreach(const QString& post, pyList) {
midRes << (pre + post);
}
}
res = midRes;
}
return res;
}
static bool gIsWrenchQuitting;
bool isWrenchQuitting()
{
return gIsWrenchQuitting;
}
void wrenchSetQuitting()
{
gIsWrenchQuitting = true;
}