From 8097fa53b5e0e6228bd5737de6de8cd77c04dddf Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Fri, 30 Oct 2009 13:23:28 +0100 Subject: Add QScriptString::toArrayIndex() function Avoid hacking a custom toArrayIndex() in the example; instead provide an ECMA-compliant conversion that's as fast as possible (having to convert the QScriptString to a QString and then convert the result to a number is considerably slower than calling JSC's Identifier::toArrayIndex() function directly). Reviewed-by: Olivier Goffart --- examples/script/customclass/bytearrayclass.cpp | 17 +++--------- src/script/api/qscriptstring.cpp | 29 ++++++++++++++++++++ src/script/api/qscriptstring.h | 2 ++ tests/auto/qscriptstring/tst_qscriptstring.cpp | 37 ++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/examples/script/customclass/bytearrayclass.cpp b/examples/script/customclass/bytearrayclass.cpp index 7291b97..bce69e4 100644 --- a/examples/script/customclass/bytearrayclass.cpp +++ b/examples/script/customclass/bytearrayclass.cpp @@ -72,18 +72,6 @@ private: int m_last; }; -static qint32 toArrayIndex(const QString &str) -{ - QByteArray bytes = str.toUtf8(); - char *eptr; - quint32 pos = strtoul(bytes.constData(), &eptr, 10); - if ((eptr == bytes.constData() + bytes.size()) - && (QByteArray::number(pos) == bytes)) { - return pos; - } - return -1; -} - //! [0] ByteArrayClass::ByteArrayClass(QScriptEngine *engine) : QObject(engine), QScriptClass(engine) @@ -120,8 +108,9 @@ QScriptClass::QueryFlags ByteArrayClass::queryProperty(const QScriptValue &objec if (name == length) { return flags; } else { - qint32 pos = toArrayIndex(name); - if (pos == -1) + bool isArrayIndex; + qint32 pos = name.toArrayIndex(&isArrayIndex); + if (!isArrayIndex) return 0; *id = pos; if ((flags & HandlesReadAccess) && (pos >= ba->size())) diff --git a/src/script/api/qscriptstring.cpp b/src/script/api/qscriptstring.cpp index 1ede51c..10fccd0 100644 --- a/src/script/api/qscriptstring.cpp +++ b/src/script/api/qscriptstring.cpp @@ -68,6 +68,10 @@ QT_BEGIN_NAMESPACE Call the toString() function to obtain the string that a QScriptString represents. + + Call the toArrayIndex() function to convert a QScriptString to an + array index. This is useful when using QScriptClass to implement + array-like objects. */ /*! @@ -164,6 +168,31 @@ bool QScriptString::operator!=(const QScriptString &other) const } /*! + \since 4.6 + + Attempts to convert this QScriptString to a QtScript array index, + and returns the result. + + If a conversion error occurs, *\a{ok} is set to false; otherwise + *\a{ok} is set to true. +*/ +quint32 QScriptString::toArrayIndex(bool *ok) const +{ + Q_D(const QScriptString); + if (!d) { + if (ok) + *ok = false; + return -1; + } + bool tmp; + bool *okok = ok ? ok : &tmp; + quint32 result = d->identifier.toArrayIndex(okok); + if (!*okok) + result = -1; + return result; +} + +/*! Returns the string that this QScriptString represents, or a null string if this QScriptString is not valid. diff --git a/src/script/api/qscriptstring.h b/src/script/api/qscriptstring.h index 40d156c..bf5d1d5 100644 --- a/src/script/api/qscriptstring.h +++ b/src/script/api/qscriptstring.h @@ -67,6 +67,8 @@ public: bool operator==(const QScriptString &other) const; bool operator!=(const QScriptString &other) const; + quint32 toArrayIndex(bool *ok = 0) const; + QString toString() const; operator QString() const; diff --git a/tests/auto/qscriptstring/tst_qscriptstring.cpp b/tests/auto/qscriptstring/tst_qscriptstring.cpp index e1a4bc1..1229f4a 100644 --- a/tests/auto/qscriptstring/tst_qscriptstring.cpp +++ b/tests/auto/qscriptstring/tst_qscriptstring.cpp @@ -59,6 +59,8 @@ public: private slots: void test(); void hash(); + void toArrayIndex_data(); + void toArrayIndex(); }; tst_QScriptString::tst_QScriptString() @@ -155,5 +157,40 @@ void tst_QScriptString::hash() QCOMPARE(stringToInt.value(foo), 123); } +void tst_QScriptString::toArrayIndex_data() +{ + QTest::addColumn("input"); + QTest::addColumn("expectSuccess"); + QTest::addColumn("expectedIndex"); + QTest::newRow("foo") << QString::fromLatin1("foo") << false << quint32(0xffffffff); + QTest::newRow("empty") << QString::fromLatin1("") << false << quint32(0xffffffff); + QTest::newRow("0") << QString::fromLatin1("0") << true << quint32(0); + QTest::newRow("00") << QString::fromLatin1("00") << false << quint32(0xffffffff); + QTest::newRow("1") << QString::fromLatin1("1") << true << quint32(1); + QTest::newRow("123") << QString::fromLatin1("123") << true << quint32(123); + QTest::newRow("-1") << QString::fromLatin1("-1") << false << quint32(0xffffffff); + QTest::newRow("0a") << QString::fromLatin1("0a") << false << quint32(0xffffffff); + QTest::newRow("0x1") << QString::fromLatin1("0x1") << false << quint32(0xffffffff); + QTest::newRow("01") << QString::fromLatin1("01") << false << quint32(0xffffffff); + QTest::newRow("4294967294") << QString::fromLatin1("4294967294") << true << quint32(0xfffffffe); + QTest::newRow("4294967295") << QString::fromLatin1("4294967295") << false << quint32(0xffffffff); +} + +void tst_QScriptString::toArrayIndex() +{ + QFETCH(QString, input); + QFETCH(bool, expectSuccess); + QFETCH(quint32, expectedIndex); + QScriptEngine engine; + for (int x = 0; x < 2; ++x) { + bool isArrayIndex; + bool *ptr = (x == 0) ? &isArrayIndex : (bool*)0; + quint32 result = engine.toStringHandle(input).toArrayIndex(ptr); + if (x == 0) + QCOMPARE(isArrayIndex, expectSuccess); + QCOMPARE(result, expectedIndex); + } +} + QTEST_MAIN(tst_QScriptString) #include "tst_qscriptstring.moc" -- cgit v0.12