simplify table lookups

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-06-20 11:55:50 +00:00
parent 0ed6bc7bba
commit 7232176609
6 changed files with 8791 additions and 14379 deletions

View file

@ -49,13 +49,10 @@ QT_BEGIN_NAMESPACE
static bool containsTLDEntry(const QString &entry) static bool containsTLDEntry(const QString &entry)
{ {
int index = qHash(entry) % tldCount; for (int i = 0; i < TLDTblSize; i++) {
uint currentDomainIndex = tldIndices[index]; QString currentEntry = QString::fromUtf8(TLDTbl[i]);
while (currentDomainIndex < tldIndices[index+1]) {
QString currentEntry = QString::fromUtf8(tldData + currentDomainIndex);
if (currentEntry == entry) if (currentEntry == entry)
return true; return true;
currentDomainIndex += qstrlen(tldData + currentDomainIndex) + 1; // +1 for the ending \0
} }
return false; return false;
} }

File diff suppressed because it is too large Load diff

View file

@ -231,7 +231,7 @@ static inline QByteArray normalizeTypeInternal(const char *t, const char *e)
result.remove(0, 5); result.remove(0, 5);
} }
/// substitutions for 'unsigned x' with those defined in global header // substitute 'unsigned x' with those defined in global header
result.replace("unsigned int", "uint"); result.replace("unsigned int", "uint");
result.replace("unsigned long long", "ulonglong"); result.replace("unsigned long long", "ulonglong");
result.replace("unsigned long", "ulong"); result.replace("unsigned long", "ulong");

View file

@ -742,9 +742,8 @@ static int translateKeySym(const uint key)
{ {
int code = -1; int code = -1;
for (int i = 0; i < KeyTblSize; i++) { for (int i = 0; i < KeyTblSize; i++) {
const KeyTblData data = KeyTbl[i]; if (KeyTbl[i].x11key == key) {
if (data.x11key == key) { code = KeyTbl[i].qtkey;
code = data.qtkey;
break; break;
} }
} }

View file

@ -263,17 +263,13 @@ static const struct RGBData {
static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData); static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData);
inline bool operator<(const char *name, const RGBData &data)
{ return qstrcmp(name, data.name) < 0; }
inline bool operator<(const RGBData &data, const char *name)
{ return qstrcmp(data.name, name) < 0; }
bool qt_get_named_rgb(const char *name, QRgb* rgb) bool qt_get_named_rgb(const char *name, QRgb* rgb)
{ {
const RGBData *r = qBinaryFind(rgbTbl, rgbTbl + rgbTblSize, name); for (int i = 0; i < rgbTblSize; i++) {
if (r != rgbTbl + rgbTblSize) { if (qstrcmp(rgbTbl[i].name, name) == 0) {
*rgb = r->value; *rgb = rgbTbl[i].value;
return true; return true;
}
} }
return false; return false;
} }

View file

@ -76,8 +76,8 @@ int main(int argc, char **argv) {
printf("file, do the following:\n\n"); printf("file, do the following:\n\n");
printf(" wget https://publicsuffix.org/list/effective_tld_names.dat -O effective_tld_names.dat\n"); printf(" wget https://publicsuffix.org/list/effective_tld_names.dat -O effective_tld_names.dat\n");
printf(" grep '^[^\\/\\/]' effective_tld_names.dat > effective_tld_names.dat.trimmed\n"); printf(" grep '^[^\\/\\/]' effective_tld_names.dat > effective_tld_names.dat.trimmed\n");
printf(" %s effective_tld_names.dat.trimmed effective_tld_names.dat.qt\n\n", argv[0]); printf(" %s effective_tld_names.dat.trimmed effective_tld_names.dat.txt\n\n", argv[0]);
printf("Now copy the data from effective_tld_names.dat.qt to the file src/core/io/qurltlds_p.h in your Qt repo\n\n"); printf("Now copy the data from effective_tld_names.dat.txt to the file src/core/io/qurltlds_p.h in your Qt repo\n\n");
exit(1); exit(1);
} }
QFile file(QString::fromLatin1(argv[1])); QFile file(QString::fromLatin1(argv[1]));
@ -85,76 +85,33 @@ int main(int argc, char **argv) {
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
outFile.open(QIODevice::WriteOnly); outFile.open(QIODevice::WriteOnly);
QByteArray outIndicesBufferBA;
QBuffer outIndicesBuffer(&outIndicesBufferBA);
outIndicesBuffer.open(QIODevice::WriteOnly);
QByteArray outDataBufferBA; QByteArray outDataBufferBA;
QBuffer outDataBuffer(&outDataBufferBA); QBuffer outDataBuffer(&outDataBufferBA);
outDataBuffer.open(QIODevice::WriteOnly); outDataBuffer.open(QIODevice::WriteOnly);
int lineCount = 0; int lineCount = 0;
while (!file.atEnd()) {
file.readLine();
lineCount++;
}
file.reset();
QVector<QString> strings(lineCount);
while (!file.atEnd()) { while (!file.atEnd()) {
QString s = QString::fromUtf8(file.readLine()); QString s = QString::fromUtf8(file.readLine());
QString st = s.trimmed(); QString st = s.trimmed();
int num = qHash(st) % lineCount;
QString utf8String = utf8encode(st.toUtf8()); QString utf8String = utf8encode(st.toUtf8());
// for domain 1.com, we could get something like outDataBuffer.write(" \"");
// a.com\01.com, which would be interpreted as octal 01, outDataBuffer.write(utf8String.toUtf8());
// so we need to separate those strings with quotes outDataBuffer.write("\\0\",\n");
QRegExp regexpOctalEscape(QLatin1String("^[0-9]"));
if (!strings.at(num).isEmpty() && st.contains(regexpOctalEscape))
strings[num].append(QLatin1String("\"\""));
strings[num].append(utf8String); lineCount++;
strings[num].append(QLatin1String("\\0"));
} }
outIndicesBuffer.write("static const quint32 tldCount = ");
outIndicesBuffer.write(QByteArray::number(lineCount));
outIndicesBuffer.write(";\n");
outIndicesBuffer.write("static const quint32 tldIndices[");
// outIndicesBuffer.write(QByteArray::number(lineCount+1)); // not needed
outIndicesBuffer.write("] = {\n");
int utf8Size = 0;
// int charSize = 0;
for (int a = 0; a < lineCount; a++) {
bool lineIsEmpty = strings.at(a).isEmpty();
if (!lineIsEmpty) {
strings[a].prepend(QLatin1Char('"'));
strings[a].append(QLatin1Char('"'));
}
int zeroCount = strings.at(a).count(QLatin1String("\\0"));
int utf8CharsCount = strings.at(a).count(QLatin1String("\\x"));
int quoteCount = strings.at(a).count(QLatin1Char('"'));
outDataBuffer.write(strings.at(a).toUtf8());
if (!lineIsEmpty)
outDataBuffer.write("\n");
outIndicesBuffer.write(QByteArray::number(utf8Size));
outIndicesBuffer.write(",\n");
utf8Size += strings.at(a).count() - (zeroCount + quoteCount + utf8CharsCount * 3);
// charSize += strings.at(a).count();
}
outIndicesBuffer.write(QByteArray::number(utf8Size));
outIndicesBuffer.write("};\n");
outIndicesBuffer.close();
outFile.write(outIndicesBufferBA);
outDataBuffer.close(); outDataBuffer.close();
outFile.write("\nstatic const char tldData["); outFile.write("\nstatic const char* TLDTbl[");
// outFile.write(QByteArray::number(charSize)); // not needed // outFile.write(QByteArray::number(charSize)); // not needed
outFile.write("] = {\n"); outFile.write("] = {\n");
outFile.write(outDataBufferBA); outFile.write(outDataBufferBA);
outFile.write("};\n"); outFile.write("};\n\n");
outFile.write("static const qint32 TLDTblSize = ");
outFile.write(QByteArray::number(lineCount));
outFile.write(";\n");
outFile.close(); outFile.close();
printf("data generated to %s . Now copy the data from this file to src/core/io/qurltlds_p.h in your Qt repo\n", argv[2]); printf("data generated to %s . Now copy the data from this file to src/core/io/qurltlds_p.h in your Qt repo\n", argv[2]);
exit(0); exit(0);