QStringMatcher: add QStringView support

While touching the code, deduplicate some methods.

Change-Id: I28f469f0e9ae000a34466b0ecc604b5f3bd09e63
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Anton Kudryavtsev 2019-03-12 02:40:01 -07:00 committed by Anton Kudryavtsev
parent 29a70348d4
commit b35eec360d
3 changed files with 47 additions and 10 deletions

View File

@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2019 Mail.ru Group.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -175,13 +176,26 @@ QStringMatcher::QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs)
by \a uc with the given \a length and case sensitivity specified by \a cs.
*/
QStringMatcher::QStringMatcher(const QChar *uc, int len, Qt::CaseSensitivity cs)
: d_ptr(0), q_cs(cs)
: QStringMatcher(QStringView(uc, len), cs)
{
p.uc = uc;
p.len = len;
bm_init_skiptable((const ushort *)p.uc, len, p.q_skiptable, cs);
}
/*!
\fn QStringMatcher::QStringMatcher(QStringView str, Qt::CaseSensitivity cs)
\since 5.14
Constructs a string matcher that will search for \a pattern, with
case sensitivity \a cs.
Call indexIn() to perform a search.
*/
QStringMatcher::QStringMatcher(QStringView str, Qt::CaseSensitivity cs)
: d_ptr(nullptr), q_cs(cs)
{
p.uc = str.data();
p.len = int(str.size());
bm_init_skiptable((const ushort *)p.uc, p.len, p.q_skiptable, cs);
}
/*!
Copies the \a other string matcher to this string matcher.
*/
@ -267,11 +281,7 @@ void QStringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs)
*/
int QStringMatcher::indexIn(const QString &str, int from) const
{
if (from < 0)
from = 0;
return bm_find((const ushort *)str.unicode(), str.size(), from,
(const ushort *)p.uc, p.len,
p.q_skiptable, q_cs);
return int(indexIn(QStringView(str), from));
}
/*!
@ -287,10 +297,26 @@ int QStringMatcher::indexIn(const QString &str, int from) const
\sa setPattern(), setCaseSensitivity()
*/
int QStringMatcher::indexIn(const QChar *str, int length, int from) const
{
return int(indexIn(QStringView(str, length), from));
}
/*!
\since 5.14
Searches the string \a str from character position \a from
(default 0, i.e. from the first character), for the string
pattern() that was set in the constructor or in the most recent
call to setPattern(). Returns the position where the pattern()
matched in \a str, or -1 if no match was found.
\sa setPattern(), setCaseSensitivity()
*/
qsizetype QStringMatcher::indexIn(QStringView str, qsizetype from) const
{
if (from < 0)
from = 0;
return bm_find((const ushort *)str, length, from,
return bm_find((const ushort *)str.data(), str.size(), from,
(const ushort *)p.uc, p.len,
p.q_skiptable, q_cs);
}

View File

@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2019 Mail.ru Group.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -41,6 +42,7 @@
#define QSTRINGMATCHER_H
#include <QtCore/qstring.h>
#include <QtCore/qstringview.h>
QT_BEGIN_NAMESPACE
@ -55,6 +57,8 @@ public:
Qt::CaseSensitivity cs = Qt::CaseSensitive);
QStringMatcher(const QChar *uc, int len,
Qt::CaseSensitivity cs = Qt::CaseSensitive);
QStringMatcher(QStringView pattern,
Qt::CaseSensitivity cs = Qt::CaseSensitive);
QStringMatcher(const QStringMatcher &other);
~QStringMatcher();
@ -65,6 +69,7 @@ public:
int indexIn(const QString &str, int from = 0) const;
int indexIn(const QChar *str, int length, int from = 0) const;
qsizetype indexIn(QStringView str, qsizetype from = 0) const;
QString pattern() const;
inline Qt::CaseSensitivity caseSensitivity() const { return q_cs; }

View File

@ -100,6 +100,11 @@ void tst_QStringMatcher::indexIn()
matcher.setPattern(needle);
QCOMPARE(matcher.indexIn(haystack, from), indexIn);
const auto needleSV = QStringView(needle);
QStringMatcher matcherSV(needleSV);
QCOMPARE(matcherSV.indexIn(QStringView(haystack), from), indexIn);
}
void tst_QStringMatcher::setCaseSensitivity_data()
@ -128,6 +133,7 @@ void tst_QStringMatcher::setCaseSensitivity()
matcher.setCaseSensitivity(static_cast<Qt::CaseSensitivity> (cs));
QCOMPARE(matcher.indexIn(haystack, from), indexIn);
QCOMPARE(matcher.indexIn(QStringView(haystack), from), indexIn);
}
void tst_QStringMatcher::assignOperator()