mirror of https://github.com/procxx/kepka.git
Remove now unused Generator::writeSetSearch
This commit is contained in:
parent
f1b4a86cfe
commit
16318f93fc
|
@ -346,151 +346,6 @@ QString GetOriginalValue(LangKey key) {\n\
|
|||
return source_->finalize();
|
||||
}
|
||||
|
||||
template <typename ComputeResult>
|
||||
void Generator::writeSetSearch(const std::set<QString, std::greater<QString>> &set, ComputeResult computeResult,
|
||||
const QString &invalidResult) {
|
||||
auto tabs = [](int size) { return QString(size, '\t'); };
|
||||
|
||||
enum class UsedCheckType {
|
||||
Switch,
|
||||
If,
|
||||
UpcomingIf,
|
||||
};
|
||||
auto checkTypes = QVector<UsedCheckType>();
|
||||
auto checkLengthHistory = QVector<int>(1, 0);
|
||||
auto chars = QString();
|
||||
auto tabsUsed = 1;
|
||||
|
||||
// Returns true if at least one check was finished.
|
||||
auto finishChecksTillKey = [this, &chars, &checkTypes, &checkLengthHistory, &tabsUsed, tabs](const QString &key) {
|
||||
auto result = false;
|
||||
while (!chars.isEmpty() && key.midRef(0, chars.size()) != chars) {
|
||||
result = true;
|
||||
|
||||
auto wasType = checkTypes.back();
|
||||
chars.resize(chars.size() - 1);
|
||||
checkTypes.pop_back();
|
||||
checkLengthHistory.pop_back();
|
||||
if (wasType == UsedCheckType::Switch || wasType == UsedCheckType::If) {
|
||||
--tabsUsed;
|
||||
if (wasType == UsedCheckType::Switch) {
|
||||
source_->stream() << tabs(tabsUsed) << "break;\n";
|
||||
}
|
||||
if ((!chars.isEmpty() && key.midRef(0, chars.size()) != chars) || key == chars) {
|
||||
source_->stream() << tabs(tabsUsed) << "}\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// Check if we can use "if" for a check on "charIndex" in "it" (otherwise only "switch")
|
||||
auto canUseIfForCheck = [](auto it, auto end, int charIndex) {
|
||||
auto key = *it;
|
||||
auto i = it;
|
||||
auto keyStart = key.mid(0, charIndex);
|
||||
for (++i; i != end; ++i) {
|
||||
auto nextKey = *i;
|
||||
if (nextKey.mid(0, charIndex) != keyStart) {
|
||||
return true;
|
||||
} else if (nextKey.size() > charIndex && nextKey[charIndex] != key[charIndex]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
auto countMinimalLength = [](auto it, auto end, int charIndex) {
|
||||
auto key = *it;
|
||||
auto i = it;
|
||||
auto keyStart = key.mid(0, charIndex);
|
||||
auto result = key.size();
|
||||
for (++i; i != end; ++i) {
|
||||
auto nextKey = *i;
|
||||
if (nextKey.mid(0, charIndex) != keyStart) {
|
||||
break;
|
||||
} else if (nextKey.size() > charIndex && result > nextKey.size()) {
|
||||
result = nextKey.size();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
for (auto i = set.begin(), e = set.end(); i != e; ++i) {
|
||||
// If we use just "auto" here and "name" becomes mutable,
|
||||
// the operator[] will return QCharRef instead of QChar,
|
||||
// and "auto ch = name[index]" will behave like "auto &ch =",
|
||||
// if you assign something to "ch" after that you'll change "name" (!)
|
||||
const auto name = *i;
|
||||
|
||||
auto weContinueOldSwitch = finishChecksTillKey(name);
|
||||
while (chars.size() != name.size()) {
|
||||
auto checking = chars.size();
|
||||
auto partialKey = name.mid(0, checking);
|
||||
|
||||
auto keyChar = name[checking];
|
||||
auto usedIfForCheckCount = 0;
|
||||
auto minimalLengthCheck = countMinimalLength(i, e, checking);
|
||||
for (; checking + usedIfForCheckCount != name.size(); ++usedIfForCheckCount) {
|
||||
if (!canUseIfForCheck(i, e, checking + usedIfForCheckCount) ||
|
||||
countMinimalLength(i, e, checking + usedIfForCheckCount) != minimalLengthCheck) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
auto usedIfForCheck = !weContinueOldSwitch && (usedIfForCheckCount > 0);
|
||||
auto checkLengthCondition = QString();
|
||||
if (weContinueOldSwitch) {
|
||||
weContinueOldSwitch = false;
|
||||
} else {
|
||||
checkLengthCondition = (minimalLengthCheck > checkLengthHistory.back()) ?
|
||||
("size >= " + QString::number(minimalLengthCheck)) :
|
||||
QString();
|
||||
if (!usedIfForCheck) {
|
||||
source_->stream() << tabs(tabsUsed)
|
||||
<< (checkLengthCondition.isEmpty() ? QString() :
|
||||
("if (" + checkLengthCondition + ") "))
|
||||
<< "switch (data[" << checking << "]) {\n";
|
||||
}
|
||||
}
|
||||
if (usedIfForCheck) {
|
||||
auto conditions = QStringList();
|
||||
if (usedIfForCheckCount > 1) {
|
||||
conditions.push_back("!memcmp(data + " + QString::number(checking) + ", \"" +
|
||||
name.mid(checking, usedIfForCheckCount) + "\", " +
|
||||
QString::number(usedIfForCheckCount) + ")");
|
||||
} else {
|
||||
conditions.push_back("data[" + QString::number(checking) + "] == '" + keyChar + "'");
|
||||
}
|
||||
if (!checkLengthCondition.isEmpty()) {
|
||||
conditions.push_front(checkLengthCondition);
|
||||
}
|
||||
source_->stream() << tabs(tabsUsed) << "if (" << conditions.join(" && ") << ") {\n";
|
||||
checkTypes.push_back(UsedCheckType::If);
|
||||
for (auto i = 1; i != usedIfForCheckCount; ++i) {
|
||||
checkTypes.push_back(UsedCheckType::UpcomingIf);
|
||||
chars.push_back(keyChar);
|
||||
checkLengthHistory.push_back(std::max(minimalLengthCheck, checkLengthHistory.back()));
|
||||
keyChar = name[checking + i];
|
||||
}
|
||||
} else {
|
||||
source_->stream() << tabs(tabsUsed) << "case '" << keyChar << "':\n";
|
||||
checkTypes.push_back(UsedCheckType::Switch);
|
||||
}
|
||||
++tabsUsed;
|
||||
chars.push_back(keyChar);
|
||||
checkLengthHistory.push_back(std::max(minimalLengthCheck, checkLengthHistory.back()));
|
||||
}
|
||||
source_->stream() << tabs(tabsUsed) << "return (size == " << chars.size() << ") ? " << computeResult(name)
|
||||
<< " : " << invalidResult << ";\n";
|
||||
}
|
||||
finishChecksTillKey(QString());
|
||||
|
||||
source_->stream() << "\
|
||||
\n\
|
||||
return " << invalidResult
|
||||
<< ";\n";
|
||||
}
|
||||
|
||||
QString Generator::getFullKey(const LangPack::Entry &entry) {
|
||||
if (!entry.keyBase.isEmpty() || entry.tags.empty()) {
|
||||
return entry.key;
|
||||
|
|
Loading…
Reference in New Issue