diff --git a/Telegram/SourceFiles/codegen/numbers/generator.cpp b/Telegram/SourceFiles/codegen/numbers/generator.cpp index 71e2deed3..97ef8f370 100644 --- a/Telegram/SourceFiles/codegen/numbers/generator.cpp +++ b/Telegram/SourceFiles/codegen/numbers/generator.cpp @@ -44,70 +44,28 @@ bool Generator::writeHeader() { bool Generator::writeSource() { source_ = std::make_unique(basePath_ + ".cpp", project_); - + source_->include("map", true); source_->include("QVector", true); - source_->stream() << "\ -QVector phoneNumberParse(const QString &number) {\n\ - QVector result;\n\ -\n\ - qint32 len = number.size();\n\ - if (len > 0) switch (number.at(0).unicode()) {\n"; - QString already; - for (auto i = rules_.data.cend(), e = rules_.data.cbegin(); i != e;) { - --i; - QString k = i.key(); - bool onlyLastChanged = true; - while (!already.isEmpty() && (already.size() > k.size() || !already.endsWith(k.at(already.size() - 1)))) { - if (!onlyLastChanged) { - source_->stream() << QString("\t").repeated(1 + already.size()) << "}\n"; - source_->stream() << QString("\t").repeated(already.size()) << "break;\n"; - } - already = already.mid(0, already.size() - 1); - onlyLastChanged = false; + source_->stream() << R"code(const std::map> RulesMap = { +)code"; + for (auto rule = rules_.data.cbegin(), e = rules_.data.cend(); rule != e; ++rule) { + auto k =rule.key(); + source_->stream() << "{\"" << k << "\",{"; + for (auto &c: rule.key()) { + source_->stream() << c.toLatin1() << ","; } - if (already == k) { - source_->stream() << QString("\t").repeated(1 + already.size()) << "}\n"; - } else { - bool onlyFirstCheck = true; - while (already.size() < k.size()) { - if (!onlyFirstCheck) - source_->stream() << QString("\t").repeated(1 + already.size()) << "if (len > " << already.size() - << ") switch (number.at(" << already.size() << ").unicode()) {\n"; - source_->stream() << QString("\t").repeated(1 + already.size()) << "case '" - << k.at(already.size()).toLatin1() << "':\n"; - already.push_back(k.at(already.size())); - onlyFirstCheck = false; - } - } - if (i.value().isEmpty()) { - source_->stream() << QString("\t").repeated(1 + already.size()) << "return QVector(1, " << k.size() - << ");\n"; - } else { - source_->stream() << QString("\t").repeated(1 + already.size()) << "result.reserve(" - << (i.value().size() + 1) << ");\n"; - source_->stream() << QString("\t").repeated(1 + already.size()) << "result.push_back(" << k.size() - << ");\n"; - for (int j = 0, l = i.value().size(); j < l; ++j) { - source_->stream() << QString("\t").repeated(1 + already.size()) << "result.push_back(" - << i.value().at(j) << ");\n"; - } - source_->stream() << QString("\t").repeated(1 + already.size()) << "return result;\n"; - } - } - bool onlyLastChanged = true; - while (!already.isEmpty()) { - if (!onlyLastChanged) { - source_->stream() << QString("\t").repeated(1 + already.size()) << "}\n"; - } - already = already.mid(0, already.size() - 1); - onlyLastChanged = false; + source_->stream()<< "}},\n"; } source_->stream() << "\ - }\n\ -\n\ - return result;\n\ -}\n"; +};"; + + source_->stream() << R"code( + +QVector phoneNumberParse(const QString &number) { + return RulesMap.find(number) != RulesMap.end() ? RulesMap.at(number) : QVector(); +} +)code"; return source_->finalize(); }