QLoggingRegistry: use QStringView/QLatin1String more

- QLoggingRule::parse() and the ctor take pattern as QStringView

- parseNextLine takes lines as QStringView and produces the pattern as
  QStringView for QLoggingRule

- (setContent has to wait for QStringTokenizer)

- QLoggingRule::pass()'s first argument is always QLatin1String, so
  take it as one

Use chopped() more, add a std::move().

Change-Id: Ic95ea77464a9922fef452846bc6d5053bd5de56e
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Marc Mutz 2019-08-19 23:17:48 +02:00
parent c58249c327
commit 16f927a4f1
3 changed files with 27 additions and 28 deletions

View File

@ -75,7 +75,7 @@ QLoggingRule::QLoggingRule() :
\internal \internal
Constructs a logging rule. Constructs a logging rule.
*/ */
QLoggingRule::QLoggingRule(const QStringRef &pattern, bool enabled) : QLoggingRule::QLoggingRule(QStringView pattern, bool enabled) :
messageType(-1), messageType(-1),
enabled(enabled) enabled(enabled)
{ {
@ -87,7 +87,7 @@ QLoggingRule::QLoggingRule(const QStringRef &pattern, bool enabled) :
Return value 1 means filter passed, 0 means filter doesn't influence this Return value 1 means filter passed, 0 means filter doesn't influence this
category, -1 means category doesn't pass this filter. category, -1 means category doesn't pass this filter.
*/ */
int QLoggingRule::pass(const QString &cat, QtMsgType msgType) const int QLoggingRule::pass(QLatin1String cat, QtMsgType msgType) const
{ {
// check message type // check message type
if (messageType > -1 && messageType != msgType) if (messageType > -1 && messageType != msgType)
@ -113,7 +113,7 @@ int QLoggingRule::pass(const QString &cat, QtMsgType msgType) const
return (enabled ? 1 : -1); return (enabled ? 1 : -1);
} else if (flags == RightFilter) { } else if (flags == RightFilter) {
// matches right // matches right
if (idx == (cat.count() - category.count())) if (idx == (cat.size() - category.count()))
return (enabled ? 1 : -1); return (enabled ? 1 : -1);
} }
} }
@ -129,26 +129,22 @@ int QLoggingRule::pass(const QString &cat, QtMsgType msgType) const
*.io.warning RightFilter, QtWarningMsg *.io.warning RightFilter, QtWarningMsg
*.core.* MidFilter *.core.* MidFilter
*/ */
void QLoggingRule::parse(const QStringRef &pattern) void QLoggingRule::parse(QStringView pattern)
{ {
QStringRef p; QStringView p;
// strip trailing ".messagetype" // strip trailing ".messagetype"
if (pattern.endsWith(QLatin1String(".debug"))) { if (pattern.endsWith(QLatin1String(".debug"))) {
p = QStringRef(pattern.string(), pattern.position(), p = pattern.chopped(6); // strlen(".debug")
pattern.length() - 6); // strlen(".debug")
messageType = QtDebugMsg; messageType = QtDebugMsg;
} else if (pattern.endsWith(QLatin1String(".info"))) { } else if (pattern.endsWith(QLatin1String(".info"))) {
p = QStringRef(pattern.string(), pattern.position(), p = pattern.chopped(5); // strlen(".info")
pattern.length() - 5); // strlen(".info")
messageType = QtInfoMsg; messageType = QtInfoMsg;
} else if (pattern.endsWith(QLatin1String(".warning"))) { } else if (pattern.endsWith(QLatin1String(".warning"))) {
p = QStringRef(pattern.string(), pattern.position(), p = pattern.chopped(8); // strlen(".warning")
pattern.length() - 8); // strlen(".warning")
messageType = QtWarningMsg; messageType = QtWarningMsg;
} else if (pattern.endsWith(QLatin1String(".critical"))) { } else if (pattern.endsWith(QLatin1String(".critical"))) {
p = QStringRef(pattern.string(), pattern.position(), p = pattern.chopped(9); // strlen(".critical")
pattern.length() - 9); // strlen(".critical")
messageType = QtCriticalMsg; messageType = QtCriticalMsg;
} else { } else {
p = pattern; p = pattern;
@ -159,11 +155,11 @@ void QLoggingRule::parse(const QStringRef &pattern)
} else { } else {
if (p.endsWith(QLatin1Char('*'))) { if (p.endsWith(QLatin1Char('*'))) {
flags |= LeftFilter; flags |= LeftFilter;
p = QStringRef(p.string(), p.position(), p.length() - 1); p = p.chopped(1);
} }
if (p.startsWith(QLatin1Char('*'))) { if (p.startsWith(QLatin1Char('*'))) {
flags |= RightFilter; flags |= RightFilter;
p = QStringRef(p.string(), p.position() + 1, p.length() - 1); p = p.mid(1);
} }
if (p.contains(QLatin1Char('*'))) // '*' only supported at start/end if (p.contains(QLatin1Char('*'))) // '*' only supported at start/end
flags = PatternFlags(); flags = PatternFlags();
@ -208,7 +204,7 @@ void QLoggingSettingsParser::setContent(QTextStream &stream)
_rules.clear(); _rules.clear();
QString line; QString line;
while (stream.readLineInto(&line)) while (stream.readLineInto(&line))
parseNextLine(QStringRef(&line)); parseNextLine(qToStringViewIgnoringNull(line));
} }
/*! /*!
@ -216,7 +212,7 @@ void QLoggingSettingsParser::setContent(QTextStream &stream)
Parses one line of the configuation file Parses one line of the configuation file
*/ */
void QLoggingSettingsParser::parseNextLine(QStringRef line) void QLoggingSettingsParser::parseNextLine(QStringView line)
{ {
// Remove whitespace at start and end of line: // Remove whitespace at start and end of line:
line = line.trimmed(); line = line.trimmed();
@ -227,7 +223,7 @@ void QLoggingSettingsParser::parseNextLine(QStringRef line)
if (line.startsWith(QLatin1Char('[')) && line.endsWith(QLatin1Char(']'))) { if (line.startsWith(QLatin1Char('[')) && line.endsWith(QLatin1Char(']'))) {
// new section // new section
auto sectionName = line.mid(1, line.size() - 2).trimmed(); auto sectionName = line.mid(1).chopped(1).trimmed();
m_inRulesSection = sectionName.compare(QLatin1String("rules"), Qt::CaseInsensitive) == 0; m_inRulesSection = sectionName.compare(QLatin1String("rules"), Qt::CaseInsensitive) == 0;
return; return;
} }
@ -240,9 +236,9 @@ void QLoggingSettingsParser::parseNextLine(QStringRef line)
#if QT_CONFIG(settings) #if QT_CONFIG(settings)
QString tmp; QString tmp;
QSettingsPrivate::iniUnescapedKey(key.toUtf8(), 0, key.length(), tmp); QSettingsPrivate::iniUnescapedKey(key.toUtf8(), 0, key.length(), tmp);
QStringRef pattern = QStringRef(&tmp, 0, tmp.length()); QStringView pattern = qToStringViewIgnoringNull(tmp);
#else #else
QStringRef pattern = key; QStringView pattern = key;
#endif #endif
const auto valueStr = line.mid(equalPos + 1).trimmed(); const auto valueStr = line.mid(equalPos + 1).trimmed();
int value = -1; int value = -1;
@ -252,7 +248,7 @@ void QLoggingSettingsParser::parseNextLine(QStringRef line)
value = 0; value = 0;
QLoggingRule rule(pattern, (value == 1)); QLoggingRule rule(pattern, (value == 1));
if (rule.flags != 0 && (value != -1)) if (rule.flags != 0 && (value != -1))
_rules.append(rule); _rules.append(std::move(rule));
else else
warnMsg("Ignoring malformed logging rule: '%s'", line.toUtf8().constData()); warnMsg("Ignoring malformed logging rule: '%s'", line.toUtf8().constData());
} else { } else {
@ -460,7 +456,7 @@ void QLoggingRegistry::defaultCategoryFilter(QLoggingCategory *cat)
debug = false; debug = false;
} }
QString categoryName = QLatin1String(cat->categoryName()); const auto categoryName = QLatin1String(cat->categoryName());
for (const auto &ruleSet : reg->ruleSets) { for (const auto &ruleSet : reg->ruleSets) {
for (const auto &rule : ruleSet) { for (const auto &rule : ruleSet) {

View File

@ -67,8 +67,8 @@ class Q_AUTOTEST_EXPORT QLoggingRule
{ {
public: public:
QLoggingRule(); QLoggingRule();
QLoggingRule(const QStringRef &pattern, bool enabled); QLoggingRule(QStringView pattern, bool enabled);
int pass(const QString &categoryName, QtMsgType type) const; int pass(QLatin1String categoryName, QtMsgType type) const;
enum PatternFlag { enum PatternFlag {
FullText = 0x1, FullText = 0x1,
@ -84,7 +84,7 @@ public:
bool enabled; bool enabled;
private: private:
void parse(const QStringRef &pattern); void parse(QStringView pattern);
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(QLoggingRule::PatternFlags) Q_DECLARE_OPERATORS_FOR_FLAGS(QLoggingRule::PatternFlags)
@ -101,7 +101,7 @@ public:
QVector<QLoggingRule> rules() const { return _rules; } QVector<QLoggingRule> rules() const { return _rules; }
private: private:
void parseNextLine(QStringRef line); void parseNextLine(QStringView line);
private: private:
bool m_inRulesSection = false; bool m_inRulesSection = false;

View File

@ -158,10 +158,13 @@ private slots:
QFETCH(QtMsgType, msgType); QFETCH(QtMsgType, msgType);
QFETCH(LoggingRuleState, result); QFETCH(LoggingRuleState, result);
QLoggingRule rule(QStringRef(&pattern), true); const auto categoryL1 = category.toLatin1();
const auto categoryL1S = QLatin1String(categoryL1);
QLoggingRule rule(pattern, true);
LoggingRuleState state = Invalid; LoggingRuleState state = Invalid;
if (rule.flags != 0) { if (rule.flags != 0) {
switch (rule.pass(category, msgType)) { switch (rule.pass(categoryL1S, msgType)) {
case -1: QFAIL("Shoudn't happen, we set pattern to true"); break; case -1: QFAIL("Shoudn't happen, we set pattern to true"); break;
case 0: state = NoMatch; break; case 0: state = NoMatch; break;
case 1: state = Match; break; case 1: state = Match; break;