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)
{
int index = qHash(entry) % tldCount;
uint currentDomainIndex = tldIndices[index];
while (currentDomainIndex < tldIndices[index+1]) {
QString currentEntry = QString::fromUtf8(tldData + currentDomainIndex);
for (int i = 0; i < TLDTblSize; i++) {
QString currentEntry = QString::fromUtf8(TLDTbl[i]);
if (currentEntry == entry)
return true;
currentDomainIndex += qstrlen(tldData + currentDomainIndex) + 1; // +1 for the ending \0
}
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);
}
/// 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 long long", "ulonglong");
result.replace("unsigned long", "ulong");

View file

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

View file

@ -263,17 +263,13 @@ static const struct 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)
{
const RGBData *r = qBinaryFind(rgbTbl, rgbTbl + rgbTblSize, name);
if (r != rgbTbl + rgbTblSize) {
*rgb = r->value;
return true;
for (int i = 0; i < rgbTblSize; i++) {
if (qstrcmp(rgbTbl[i].name, name) == 0) {
*rgb = rgbTbl[i].value;
return true;
}
}
return false;
}

View file

@ -76,8 +76,8 @@ int main(int argc, char **argv) {
printf("file, do the following:\n\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(" %s effective_tld_names.dat.trimmed effective_tld_names.dat.qt\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(" %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.txt to the file src/core/io/qurltlds_p.h in your Qt repo\n\n");
exit(1);
}
QFile file(QString::fromLatin1(argv[1]));
@ -85,76 +85,33 @@ int main(int argc, char **argv) {
file.open(QIODevice::ReadOnly);
outFile.open(QIODevice::WriteOnly);
QByteArray outIndicesBufferBA;
QBuffer outIndicesBuffer(&outIndicesBufferBA);
outIndicesBuffer.open(QIODevice::WriteOnly);
QByteArray outDataBufferBA;
QBuffer outDataBuffer(&outDataBufferBA);
outDataBuffer.open(QIODevice::WriteOnly);
int lineCount = 0;
while (!file.atEnd()) {
file.readLine();
lineCount++;
}
file.reset();
QVector<QString> strings(lineCount);
while (!file.atEnd()) {
QString s = QString::fromUtf8(file.readLine());
QString st = s.trimmed();
int num = qHash(st) % lineCount;
QString utf8String = utf8encode(st.toUtf8());
// for domain 1.com, we could get something like
// a.com\01.com, which would be interpreted as octal 01,
// so we need to separate those strings with quotes
QRegExp regexpOctalEscape(QLatin1String("^[0-9]"));
if (!strings.at(num).isEmpty() && st.contains(regexpOctalEscape))
strings[num].append(QLatin1String("\"\""));
outDataBuffer.write(" \"");
outDataBuffer.write(utf8String.toUtf8());
outDataBuffer.write("\\0\",\n");
strings[num].append(utf8String);
strings[num].append(QLatin1String("\\0"));
lineCount++;
}
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();
outFile.write("\nstatic const char tldData[");
outFile.write("\nstatic const char* TLDTbl[");
// outFile.write(QByteArray::number(charSize)); // not needed
outFile.write("] = {\n");
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();
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);