From 30065db409354bab77142f72e4700a8c4e5562fd Mon Sep 17 00:00:00 2001 From: Sergei Ilinykh Date: Fri, 26 Apr 2024 19:37:38 +0300 Subject: [PATCH] More string optimizations --- src/xmpp/xmpp-core/protocol.cpp | 161 +++++++++++++++-------------- src/xmpp/xmpp-core/protocol.h | 12 +-- src/xmpp/xmpp-core/xmpp_stanza.cpp | 14 +-- 3 files changed, 94 insertions(+), 93 deletions(-) diff --git a/src/xmpp/xmpp-core/protocol.cpp b/src/xmpp/xmpp-core/protocol.cpp index e87c3773..157e8d42 100644 --- a/src/xmpp/xmpp-core/protocol.cpp +++ b/src/xmpp/xmpp-core/protocol.cpp @@ -95,48 +95,46 @@ StreamFeatures::StreamFeatures() //---------------------------------------------------------------------------- // BasicProtocol //---------------------------------------------------------------------------- -BasicProtocol::SASLCondEntry BasicProtocol::saslCondTable[] = { - { "aborted", SASLCond::Aborted }, - { "account-disabled", SASLCond::AccountDisabled }, - { "credentials-expired", SASLCond::CredentialsExpired }, - { "encryption-required", SASLCond::EncryptionRequired }, - { "incorrect-encoding", SASLCond::IncorrectEncoding }, - { "invalid-authzid", SASLCond::InvalidAuthzid }, - { "invalid-mechanism", SASLCond::InvalidMechanism }, - { "malformed-request", SASLCond::MalformedRequest }, - { "mechanism-too-weak", SASLCond::MechanismTooWeak }, - { "not-authorized", SASLCond::NotAuthorized }, - { "temporary-auth-failure", SASLCond::TemporaryAuthFailure }, - { nullptr, SASLCond(0) }, -}; - -BasicProtocol::StreamCondEntry BasicProtocol::streamCondTable[] = { - { "bad-format", StreamCond::BadFormat }, - { "bad-namespace-prefix", StreamCond::BadNamespacePrefix }, - { "conflict", StreamCond::Conflict }, - { "connection-timeout", StreamCond::ConnectionTimeout }, - { "host-gone", StreamCond::HostGone }, - { "host-unknown", StreamCond::HostUnknown }, - { "improper-addressing", StreamCond::ImproperAddressing }, - { "internal-server-error", StreamCond::InternalServerError }, - { "invalid-from", StreamCond::InvalidFrom }, - { "invalid-namespace", StreamCond::InvalidNamespace }, - { "invalid-xml", StreamCond::InvalidXml }, - { "not-authorized", StreamCond::NotAuthorized }, - { "not-well-formed", StreamCond::NotWellFormed }, - { "policy-violation", StreamCond::PolicyViolation }, - { "remote-connection-failed", StreamCond::RemoteConnectionFailed }, - { "reset", StreamCond::Reset }, - { "resource-constraint", StreamCond::ResourceConstraint }, - { "restricted-xml", StreamCond::RestrictedXml }, - { "see-other-host", StreamCond::SeeOtherHost }, - { "system-shutdown", StreamCond::SystemShutdown }, - { "undefined-condition", StreamCond::UndefinedCondition }, - { "unsupported-encoding", StreamCond::UnsupportedEncoding }, - { "unsupported-stanza-type", StreamCond::UnsupportedStanzaType }, - { "unsupported-version", StreamCond::UnsupportedVersion }, - { nullptr, StreamCond(0) }, -}; +std::array BasicProtocol::saslCondTable { { + { QStringLiteral("aborted"), SASLCond::Aborted }, + { QStringLiteral("account-disabled"), SASLCond::AccountDisabled }, + { QStringLiteral("credentials-expired"), SASLCond::CredentialsExpired }, + { QStringLiteral("encryption-required"), SASLCond::EncryptionRequired }, + { QStringLiteral("incorrect-encoding"), SASLCond::IncorrectEncoding }, + { QStringLiteral("invalid-authzid"), SASLCond::InvalidAuthzid }, + { QStringLiteral("invalid-mechanism"), SASLCond::InvalidMechanism }, + { QStringLiteral("malformed-request"), SASLCond::MalformedRequest }, + { QStringLiteral("mechanism-too-weak"), SASLCond::MechanismTooWeak }, + { QStringLiteral("not-authorized"), SASLCond::NotAuthorized }, + { QStringLiteral("temporary-auth-failure"), SASLCond::TemporaryAuthFailure }, +} }; + +std::array BasicProtocol::streamCondTable { { + { QStringLiteral("bad-format"), StreamCond::BadFormat }, + { QStringLiteral("bad-namespace-prefix"), StreamCond::BadNamespacePrefix }, + { QStringLiteral("conflict"), StreamCond::Conflict }, + { QStringLiteral("connection-timeout"), StreamCond::ConnectionTimeout }, + { QStringLiteral("host-gone"), StreamCond::HostGone }, + { QStringLiteral("host-unknown"), StreamCond::HostUnknown }, + { QStringLiteral("improper-addressing"), StreamCond::ImproperAddressing }, + { QStringLiteral("internal-server-error"), StreamCond::InternalServerError }, + { QStringLiteral("invalid-from"), StreamCond::InvalidFrom }, + { QStringLiteral("invalid-namespace"), StreamCond::InvalidNamespace }, + { QStringLiteral("invalid-xml"), StreamCond::InvalidXml }, + { QStringLiteral("not-authorized"), StreamCond::NotAuthorized }, + { QStringLiteral("not-well-formed"), StreamCond::NotWellFormed }, + { QStringLiteral("policy-violation"), StreamCond::PolicyViolation }, + { QStringLiteral("remote-connection-failed"), StreamCond::RemoteConnectionFailed }, + { QStringLiteral("reset"), StreamCond::Reset }, + { QStringLiteral("resource-constraint"), StreamCond::ResourceConstraint }, + { QStringLiteral("restricted-xml"), StreamCond::RestrictedXml }, + { QStringLiteral("see-other-host"), StreamCond::SeeOtherHost }, + { QStringLiteral("system-shutdown"), StreamCond::SystemShutdown }, + { QStringLiteral("undefined-condition"), StreamCond::UndefinedCondition }, + { QStringLiteral("unsupported-encoding"), StreamCond::UnsupportedEncoding }, + { QStringLiteral("unsupported-stanza-type"), StreamCond::UnsupportedStanzaType }, + { QStringLiteral("unsupported-version"), StreamCond::UnsupportedVersion }, +} }; BasicProtocol::BasicProtocol() : XmlProtocol() { init(); } @@ -239,36 +237,36 @@ void BasicProtocol::setSASLAuthed() { sasl_authed = true; } std::optional BasicProtocol::stringToSASLCond(const QString &s) { - for (int n = 0; saslCondTable[n].str; ++n) { - if (s == saslCondTable[n].str) - return saslCondTable[n].cond; + for (auto const &entry : saslCondTable) { + if (s == entry.str) + return entry.cond; } return {}; } std::optional BasicProtocol::stringToStreamCond(const QString &s) { - for (int n = 0; streamCondTable[n].str; ++n) { - if (s == streamCondTable[n].str) - return streamCondTable[n].cond; + for (auto const &entry : streamCondTable) { + if (s == entry.str) + return entry.cond; } return {}; } QString BasicProtocol::saslCondToString(SASLCond x) { - for (int n = 0; saslCondTable[n].str; ++n) { - if (x == saslCondTable[n].cond) - return saslCondTable[n].str; + for (auto const &entry : saslCondTable) { + if (x == entry.cond) + return entry.str; } return QString(); } QString BasicProtocol::streamCondToString(StreamCond x) { - for (int n = 0; streamCondTable[n].str; ++n) { - if (x == streamCondTable[n].cond) - return streamCondTable[n].str; + for (auto const &entry : streamCondTable) { + if (x == entry.cond) + return entry.str; } return QString(); } @@ -287,34 +285,37 @@ void BasicProtocol::extractStreamError(const QDomElement &e) } else errCond = stringToStreamCond(t.tagName()); - if (errCond.has_value()) { - if (std::get(*errCond) == StreamCond::SeeOtherHost) - otherHost = t.text(); - - auto nodes = e.elementsByTagNameNS(NS_STREAMS, "text"); - if (nodes.count()) { - for (int i = 0; i < nodes.count(); i++) { - auto e = nodes.item(i).toElement(); - QString lang = e.attributeNS(NS_STREAMS, "lang", ""); - langText.insert(lang, e.text()); - } - } else - text = t.text(); - - // find first non-standard namespaced element - QDomNodeList nl = e.childNodes(); - for (int n = 0; n < nl.count(); ++n) { - QDomNode i = nl.item(n); - if (i.isElement() && i.namespaceURI() != NS_STREAMS) { - appSpec = i.toElement(); - break; - } + if (!errCond.has_value()) { + qWarning("unknown stream error=%s", qUtf8Printable(t.tagName())); + // rfc6120 says we should treat unknows conditions as + errCond = StreamCond::UndefinedCondition; + } + if (std::get(*errCond) == StreamCond::SeeOtherHost) + otherHost = t.text(); + + auto nodes = e.elementsByTagNameNS(NS_STREAMS, "text"); + if (nodes.count()) { + for (int i = 0; i < nodes.count(); i++) { + auto e = nodes.item(i).toElement(); + QString lang = e.attributeNS(NS_STREAMS, "lang", ""); + langText.insert(lang, e.text()); + } + } else + text = t.text(); + + // find first non-standard namespaced element + QDomNodeList nl = e.childNodes(); + for (int n = 0; n < nl.count(); ++n) { + QDomNode i = nl.item(n); + if (i.isElement() && i.namespaceURI() != NS_STREAMS) { + appSpec = i.toElement(); + break; } - - errText = text; - errLangText = langText; - errAppSpec = appSpec; } + + errText = text; + errLangText = langText; + errAppSpec = appSpec; } void BasicProtocol::send(const QDomElement &e, bool clip) { writeElement(e, TypeElement, false, clip, false); } diff --git a/src/xmpp/xmpp-core/protocol.h b/src/xmpp/xmpp-core/protocol.h index deb0419f..09c8a9d0 100644 --- a/src/xmpp/xmpp-core/protocol.h +++ b/src/xmpp/xmpp-core/protocol.h @@ -227,16 +227,16 @@ class BasicProtocol : public XmlProtocol { private: struct SASLCondEntry { - const char *str; - SASLCond cond; + QString str; + SASLCond cond; }; - static SASLCondEntry saslCondTable[]; + static std::array saslCondTable; struct StreamCondEntry { - const char *str; - StreamCond cond; + QString str; + StreamCond cond; }; - static StreamCondEntry streamCondTable[]; + static std::array streamCondTable; struct SendItem { QDomElement stanzaToSend; diff --git a/src/xmpp/xmpp-core/xmpp_stanza.cpp b/src/xmpp/xmpp-core/xmpp_stanza.cpp index ce9999cb..aefe2588 100644 --- a/src/xmpp/xmpp-core/xmpp_stanza.cpp +++ b/src/xmpp/xmpp-core/xmpp_stanza.cpp @@ -363,7 +363,7 @@ bool Stanza::Error::fromXml(const QDomElement &e, const QString &baseNS) condition = ErrorCond(-1); } - by = e.attribute(QLatin1String("by")); + by = e.attribute(QStringLiteral("by")); QString textTag(QStringLiteral("text")); for (auto t = e.firstChildElement(); !t.isNull(); t = t.nextSiblingElement()) { if (t.namespaceURI() == NS_STANZAS) { @@ -464,11 +464,11 @@ class Stanza::Private { public: static int stringToKind(const QString &s) { - if (s == QLatin1String("message")) + if (s == QStringLiteral("message")) return Message; - else if (s == QLatin1String("presence")) + else if (s == QStringLiteral("presence")) return Presence; - else if (s == QLatin1String("iq")) + else if (s == QStringLiteral("iq")) return IQ; else return -1; @@ -477,11 +477,11 @@ class Stanza::Private { static QString kindToString(Kind k) { if (k == Message) - return QLatin1String("message"); + return QStringLiteral("message"); else if (k == Presence) - return QLatin1String("presence"); + return QStringLiteral("presence"); else - return QLatin1String("iq"); + return QStringLiteral("iq"); } Stream *s;