kdecore: reimplement KuitSemantics

is interesting how much simpler it can be by assuming that the tags are
strict and by using Qt::mightBeRichText() for the rich text detection.
while mostly compatible (entities are not converted, some tags were not
used and some even did not do anything) it is subject to optimization by
using QStringMatcher or other tricks (like using a separate tag for the
numbers precision)

overall - no regular expression matching, no XML parser is used and does
what the old implementation was doing but with less code. also passes
most tests (the exception is KLocalizedString test case that expect
entities to be replaced)

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-08-17 01:41:38 +03:00
parent 9fee47106a
commit 9a5af1214b
5 changed files with 193 additions and 1453 deletions

View file

@ -330,19 +330,15 @@ QString KLocalizedStringPrivate::postFormat(const QString &text,
return text;
}
static QString wrapNum (const QString &tag, const QString &numstr,
int fieldWidth, const QChar &fillChar, const int precision)
static QString wrapNum(const QString &tag, const QString &numstr, const int precision)
{
QString optag;
if (fieldWidth != 0 || precision >= 0) {
QString fillString = Qt::escape(fillChar);
optag = QString::fromLatin1("<%1 width='%2' fill='%3' precision='%4'>")
.arg(tag, QString::number(fieldWidth), fillString, QString::number(precision));
} else {
optag = QString::fromLatin1("<%1>").arg(tag);
QString prtag;
if (precision != -1) {
prtag = QString::fromLatin1("<%1>%2</%1>").arg(QString::fromLatin1(KUIT_NUMPREC), QString::number(precision));
}
QString optag = QString::fromLatin1("<%1>").arg(tag);
QString cltag = QString::fromLatin1("</%1>").arg(tag);
return optag + numstr + cltag;
return prtag + optag + numstr + cltag;
}
KLocalizedString KLocalizedString::subs(int a, int fieldWidth, int base,
@ -354,8 +350,13 @@ KLocalizedString KLocalizedString::subs(int a, int fieldWidth, int base,
kls.d->numberSet = true;
kls.d->numberOrd = d->args.size();
}
kls.d->args.append(wrapNum(QString::fromLatin1(KUIT_NUMINTG), QString::number(a, base),
fieldWidth, fillChar, -1));
kls.d->args.append(
wrapNum(
QString::fromLatin1(KUIT_NUMINTG),
QString::fromLatin1("%1").arg(a, fieldWidth, base, fillChar),
-1
)
);
kls.d->vals.append(static_cast<intn>(a));
return kls;
}
@ -369,8 +370,13 @@ KLocalizedString KLocalizedString::subs(uint a, int fieldWidth, int base,
kls.d->numberSet = true;
kls.d->numberOrd = d->args.size();
}
kls.d->args.append(wrapNum(QString::fromLatin1(KUIT_NUMINTG), QString::number(a, base),
fieldWidth, fillChar, -1));
kls.d->args.append(
wrapNum(
QString::fromLatin1(KUIT_NUMINTG),
QString::fromLatin1("%1").arg(a, fieldWidth, base, fillChar),
-1
)
);
kls.d->vals.append(static_cast<uintn>(a));
return kls;
}
@ -384,8 +390,13 @@ KLocalizedString KLocalizedString::subs(long a, int fieldWidth, int base,
kls.d->numberSet = true;
kls.d->numberOrd = d->args.size();
}
kls.d->args.append(wrapNum(QString::fromLatin1(KUIT_NUMINTG), QString::number(a, base),
fieldWidth, fillChar, -1));
kls.d->args.append(
wrapNum(
QString::fromLatin1(KUIT_NUMINTG),
QString::fromLatin1("%1").arg(a, fieldWidth, base, fillChar),
-1
)
);
kls.d->vals.append(static_cast<intn>(a));
return kls;
}
@ -399,8 +410,13 @@ KLocalizedString KLocalizedString::subs(ulong a, int fieldWidth, int base,
kls.d->numberSet = true;
kls.d->numberOrd = d->args.size();
}
kls.d->args.append(wrapNum(QString::fromLatin1(KUIT_NUMINTG), QString::number(a, base),
fieldWidth, fillChar, -1));
kls.d->args.append(
wrapNum(
QString::fromLatin1(KUIT_NUMINTG),
QString::fromLatin1("%1").arg(a, fieldWidth, base, fillChar),
-1
)
);
kls.d->vals.append(static_cast<uintn>(a));
return kls;
}
@ -414,8 +430,13 @@ KLocalizedString KLocalizedString::subs(qlonglong a, int fieldWidth, int base,
kls.d->numberSet = true;
kls.d->numberOrd = d->args.size();
}
kls.d->args.append(wrapNum(QString::fromLatin1(KUIT_NUMINTG), QString::number(a, base),
fieldWidth, fillChar, -1));
kls.d->args.append(
wrapNum(
QString::fromLatin1(KUIT_NUMINTG),
QString::fromLatin1("%1").arg(a, fieldWidth, base, fillChar),
-1
)
);
kls.d->vals.append(static_cast<intn>(a));
return kls;
}
@ -429,8 +450,13 @@ KLocalizedString KLocalizedString::subs(qulonglong a, int fieldWidth, int base,
kls.d->numberSet = true;
kls.d->numberOrd = d->args.size();
}
kls.d->args.append(wrapNum(QString::fromLatin1(KUIT_NUMINTG), QString::number(a, base),
fieldWidth, fillChar, -1));
kls.d->args.append(
wrapNum(
QString::fromLatin1(KUIT_NUMINTG),
QString::fromLatin1("%1").arg(a, fieldWidth, base, fillChar),
-1
)
);
kls.d->vals.append(static_cast<uintn>(a));
return kls;
}
@ -440,9 +466,13 @@ KLocalizedString KLocalizedString::subs(double a, int fieldWidth,
const QChar &fillChar) const
{
KLocalizedString kls(*this);
kls.d->args.append(wrapNum(QString::fromLatin1(KUIT_NUMREAL),
QString::number(a, format, precision),
fieldWidth, fillChar, precision));
kls.d->args.append(
wrapNum(
QString::fromLatin1(KUIT_NUMREAL),
QString::fromLatin1("%1").arg(a, fieldWidth, format, precision == -1 ? 2 : precision, fillChar),
precision
)
);
kls.d->vals.append(static_cast<realn>(a));
return kls;
}

File diff suppressed because it is too large Load diff

View file

@ -1,10 +1,9 @@
/* This file is part of the KDE libraries
Copyright (C) 2007 Chusslove Illich <caslav.ilic@gmx.net>
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
License version 2, as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -20,10 +19,16 @@
#ifndef KUITSEMANTICS_P_H
#define KUITSEMANTICS_P_H
#include <QString>
#include "kcatalog_p.h"
class KuitSemantics;
class KuitSemanticsPrivate;
#include <QString>
#include <QMap>
struct KuitFormat
{
QString plain;
QString rich;
};
/**
* @internal
@ -32,19 +37,18 @@ class KuitSemanticsPrivate;
* KuitSemantics resolves semantic markup in user interface text
* into appropriate visual formatting.
*
* @author Chusslove Illich <caslav.ilic@gmx.net>
* @author Ivailo Monev <xakepa10@gmail.com>
* @short class for formatting semantic markup in UI messages
*/
class KuitSemantics
{
public:
public:
/**
* Constructor.
*
* @param lang language to create the formatter for
*/
KuitSemantics (const QString &lang);
KuitSemantics(const QString &lang);
/**
* Transforms the semantic markup in the given text into visual formatting.
@ -54,23 +58,16 @@ class KuitSemantics
* @param text text containing the semantic markup
* @param ctxt context of the text
*/
QString format (const QString &text, const QString &ctxt) const;
QString format(const QString &text, const QString &ctxt) const;
/**
* Destructor.
*/
~KuitSemantics ();
private:
KuitSemantics (const KuitSemantics &t);
KuitSemantics &operator= (const KuitSemantics &t);
KuitSemanticsPrivate *d;
private:
KCatalog m_catalog;
QMap<QString,KuitFormat> m_patterns;
};
// Some stuff needed in klocalizedstring.cpp too.
#define KUIT_NUMINTG "numintg"
#define KUIT_NUMREAL "numreal"
#define KUIT_NUMPREC "numprec"
#endif

View file

@ -108,7 +108,7 @@ Test11::Test11( )
}
mEmailClientItem = new MyPrefs::ItemEnum( currentGroup(), QLatin1String( "EmailClient" ), mEmailClient, valuesEmailClient, kmail );
mEmailClientItem->setLabel( i18nc("@label", "Email client") );
mEmailClientItem->setWhatsThis( i18nc("@info:whatsthis", "<para>How to send email when an email alarm is triggered.<list><item>KMail: The email is sent automatically via <application>KMail</application>. <application>KMail</application> is started first if necessary.</item><item>Sendmail: The email is sent automatically. This option will only work if your system is configured to use <application>sendmail</application> or a sendmail compatible mail transport agent.</item></list></para>") );
mEmailClientItem->setWhatsThis( i18nc("@info:whatsthis", "<para>How to send email when an email alarm is triggered.<para>KMail: The email is sent automatically via <application>KMail</application>. <application>KMail</application> is started first if necessary.</para><para>Sendmail: The email is sent automatically. This option will only work if your system is configured to use <application>sendmail</application> or a sendmail compatible mail transport agent.</para></para>") );
addItem( mEmailClientItem, QLatin1String( "EmailClient" ) );
QList<MyPrefs::ItemEnum::Choice> valuesDefaultReminderUnits;
{

View file

@ -108,7 +108,7 @@ Test11a::Test11a( )
}
mEmailClientItem = new MyPrefs::ItemEnum( currentGroup(), QLatin1String( "EmailClient" ), mEmailClient, valuesEmailClient, kmail );
mEmailClientItem->setLabel( i18nc("@label", "Email client") );
mEmailClientItem->setWhatsThis( i18nc("@info:whatsthis", "<para>How to send email when an email alarm is triggered.<list><item>KMail: The email is sent automatically via <application>KMail</application>. <application>KMail</application> is started first if necessary.</item><item>Sendmail: The email is sent automatically. This option will only work if your system is configured to use <application>sendmail</application> or a sendmail compatible mail transport agent.</item></list></para>") );
mEmailClientItem->setWhatsThis( i18nc("@info:whatsthis", "<para>How to send email when an email alarm is triggered.<para>KMail: The email is sent automatically via <application>KMail</application>. <application>KMail</application> is started first if necessary.</para><para>Sendmail: The email is sent automatically. This option will only work if your system is configured to use <application>sendmail</application> or a sendmail compatible mail transport agent.</para></para>") );
addItem( mEmailClientItem, QLatin1String( "EmailClient" ) );
QList<MyPrefs::ItemEnum::Choice> valuesDefaultReminderUnits;
{