aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4regexp.cpp
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2018-08-08 15:04:42 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2018-08-15 14:24:38 +0000
commit4bfd94c35e5099557cafcc9ae9b7d7a970089c9f (patch)
treeb221ef374ace6fca0eaa1f041d338145dc83b083 /src/qml/jsruntime/qv4regexp.cpp
parentcecd8be881034153fc6b92e96f152a4a0c189ae4 (diff)
Implement RegExp.prototype[Symbol.replace]
Change-Id: I5a2c9cb1e9dcca664526b3949671d72d2ffee427 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4regexp.cpp')
-rw-r--r--src/qml/jsruntime/qv4regexp.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4regexp.cpp b/src/qml/jsruntime/qv4regexp.cpp
index fe44720b8a..05c3b4c4ca 100644
--- a/src/qml/jsruntime/qv4regexp.cpp
+++ b/src/qml/jsruntime/qv4regexp.cpp
@@ -76,6 +76,60 @@ uint RegExp::match(const QString &string, int start, uint *matchOffsets)
return JSC::Yarr::interpret(byteCode(), s.characters16(), string.length(), start, matchOffsets);
}
+QString RegExp::getSubstitution(const QString &matched, const QString &str, int position, const Value *captures, int nCaptures, const QString &replacement)
+{
+ QString result;
+
+ int matchedLength = matched.length();
+ Q_ASSERT(position >= 0 && position <= str.length());
+ int tailPos = position + matchedLength;
+ int seenDollar = -1;
+ for (int i = 0; i < replacement.length(); ++i) {
+ QChar ch = replacement.at(i);
+ if (seenDollar >= 0) {
+ if (ch.unicode() == '$') {
+ result += QLatin1Char('$');
+ } else if (ch.unicode() == '&') {
+ result += matched;
+ } else if (ch.unicode() == '`') {
+ result += str.left(position);
+ } else if (ch.unicode() == '\'') {
+ result += str.mid(tailPos);
+ } else if (ch.unicode() >= '0' && ch.unicode() <= '9') {
+ int n = ch.unicode() - '0';
+ if (i + 1 < replacement.length()) {
+ ch = replacement.at(i + 1);
+ if (ch.unicode() >= '0' && ch.unicode() <= '9') {
+ n = n*10 + (ch.unicode() - '0');
+ ++i;
+ }
+ }
+ if (n > 0 && n <= nCaptures) {
+ String *s = captures[n].stringValue();
+ if (s)
+ result += s->toQString();
+ } else {
+ for (int j = seenDollar; j <= i; ++j)
+ result += replacement.at(j);
+ }
+ } else {
+ result += QLatin1Char('$');
+ result += ch;
+ }
+ seenDollar = -1;
+ } else {
+ if (ch == QLatin1Char('$')) {
+ seenDollar = i;
+ continue;
+ }
+ result += ch;
+ }
+ }
+ if (seenDollar >= 0)
+ result += QLatin1Char('$');
+ return result;
+}
+
QString Heap::RegExp::flagsAsString() const
{
QString result;