diff options
author | Olivier Goffart <ogoffart@trolltech.com> | 2009-07-23 11:14:23 (GMT) |
---|---|---|
committer | Olivier Goffart <ogoffart@trolltech.com> | 2009-07-23 11:39:48 (GMT) |
commit | 2792e2570b1eef7652890958dd2b48209aa1a37b (patch) | |
tree | 4630aab4b8ffb846d654f149b4429aa7106e6675 /src/script | |
parent | 9c2a2e94f4d95b5fc26e929650e475adffbee6ae (diff) | |
download | Qt-2792e2570b1eef7652890958dd2b48209aa1a37b.zip Qt-2792e2570b1eef7652890958dd2b48209aa1a37b.tar.gz Qt-2792e2570b1eef7652890958dd2b48209aa1a37b.tar.bz2 |
Try best to convert a regexp to a ECMAScript expression
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/api/qscriptengine.cpp | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 0a3fbf1..43f14fa 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -1435,7 +1435,46 @@ QScriptValue QScriptEngine::newRegExp(const QRegExp ®exp) JSC::ExecState* exec = d->currentFrame; JSC::JSValue buf[2]; JSC::ArgList args(buf, sizeof(buf)); - JSC::UString jscPattern = QScript::qtStringToJSCUString(regexp.pattern()); + + //convert the pattern to a ECMAScript pattern + extern QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax); + QString pattern = qt_regexp_toCanonical(regexp.pattern(), regexp.patternSyntax()); + if (regexp.isMinimal()) { + QString ecmaPattern; + int len = pattern.length(); + ecmaPattern.reserve(len); + int i = 0; + const QChar *wc = pattern.unicode(); + bool inBracket = false; + while (i < len) { + QChar c = wc[i++]; + ecmaPattern += c; + switch (c.unicode()) { + case '?': + case '+': + case '*': + case '}': + if (!inBracket) + ecmaPattern += QLatin1Char('?'); + break; + case '\\': + if (i < len) + ecmaPattern += wc[i++]; + break; + case '[': + inBracket = true; + break; + case ']': + inBracket = false; + break; + default: + break; + } + } + pattern = ecmaPattern; + } + + JSC::UString jscPattern = QScript::qtStringToJSCUString(pattern); QString flags; if (regexp.caseSensitivity() == Qt::CaseInsensitive) flags.append(QLatin1Char('i')); |