summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2013-01-19 00:35:21 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-02-05 13:46:25 +0100
commit53f9e77140a07eb8f36eeea460f13a54dad7330e (patch)
treeb44c428c5c94938283cda6243559b03999a70a39 /src/corelib
parentd0804ff2dd3d289a0f0c58aa30c4334e66ea9be0 (diff)
QRegularExpression: add method for extracting the capturing group names
It may be useful to know which named capturing groups are defined in an regular expression, and for each of them, what's the corresponding index. This commit adds the needed method to QRegularExpression. Note that extracting the information doesn't happen while holding the mutex in the private -- pcre_fullinfo just reads information from the compiled pattern, so that's thread-safe. Task-number: QTBUG-29079 Change-Id: I50c00ee860f06427c2e6ea10417d5c0733cc8303 Reviewed-by: Lars Knoll <lars.knoll@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qregularexpression.cpp48
-rw-r--r--src/corelib/tools/qregularexpression.h2
2 files changed, 50 insertions, 0 deletions
diff --git a/src/corelib/tools/qregularexpression.cpp b/src/corelib/tools/qregularexpression.cpp
index 503185dab5..510b7112af 100644
--- a/src/corelib/tools/qregularexpression.cpp
+++ b/src/corelib/tools/qregularexpression.cpp
@@ -1497,6 +1497,54 @@ int QRegularExpression::captureCount() const
}
/*!
+ \since 5.1
+
+ Returns a list of captureCount() elements, containing the names of the named
+ capturing groups in the pattern string. The list is sorted such that the
+ i-th element of the list is the name of the i-th capturing group, if it has
+ a name, or an empty string if the capturing group is unnamed.
+
+ If the regular expression is not valid, returns an empty list.
+
+ \sa isValid(), QRegularExpressionMatch::captured(), QString::isEmpty()
+*/
+QStringList QRegularExpression::namedCaptureGroups() const
+{
+ if (!isValid()) // isValid() will compile the pattern
+ return QStringList();
+
+ // namedCapturingTable will point to a table of
+ // namedCapturingTableEntryCount entries, each one of which
+ // contains one ushort followed by the name, NUL terminated.
+ // The ushort is the numerical index of the name in the pattern.
+ // The length of each entry is namedCapturingTableEntrySize.
+ ushort *namedCapturingTable;
+ int namedCapturingTableEntryCount;
+ int namedCapturingTableEntrySize;
+
+ pcre16_fullinfo(d->compiledPattern, 0, PCRE_INFO_NAMETABLE, &namedCapturingTable);
+ pcre16_fullinfo(d->compiledPattern, 0, PCRE_INFO_NAMECOUNT, &namedCapturingTableEntryCount);
+ pcre16_fullinfo(d->compiledPattern, 0, PCRE_INFO_NAMEENTRYSIZE, &namedCapturingTableEntrySize);
+
+ QStringList result;
+
+ // no QList::resize nor fill is available. The +1 is for the implicit group #0
+ result.reserve(d->capturingCount + 1);
+ for (int i = 0; i < d->capturingCount + 1; ++i)
+ result.append(QString());
+
+ for (int i = 0; i < namedCapturingTableEntryCount; ++i) {
+ const ushort * const currentNamedCapturingTableRow = namedCapturingTable +
+ namedCapturingTableEntrySize * i;
+
+ const int index = *currentNamedCapturingTableRow;
+ result[index] = QString::fromUtf16(currentNamedCapturingTableRow + 1);
+ }
+
+ return result;
+}
+
+/*!
Returns true if the regular expression is a valid regular expression (that
is, it contains no syntax errors, etc.), or false otherwise. Use
errorString() to obtain a textual description of the error.
diff --git a/src/corelib/tools/qregularexpression.h b/src/corelib/tools/qregularexpression.h
index a056b4f01b..97dbee9256 100644
--- a/src/corelib/tools/qregularexpression.h
+++ b/src/corelib/tools/qregularexpression.h
@@ -46,6 +46,7 @@
#ifndef QT_NO_REGULAREXPRESSION
#include <QtCore/qstring.h>
+#include <QtCore/qstringlist.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qvariant.h>
@@ -94,6 +95,7 @@ public:
QString errorString() const;
int captureCount() const;
+ QStringList namedCaptureGroups() const;
enum MatchType {
NormalMatch = 0,