diff options
Diffstat (limited to 'src/qml/jsruntime/qv4stringobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4stringobject.cpp | 145 |
1 files changed, 55 insertions, 90 deletions
diff --git a/src/qml/jsruntime/qv4stringobject.cpp b/src/qml/jsruntime/qv4stringobject.cpp index 14c1e3ed54..ad3c39c7b9 100644 --- a/src/qml/jsruntime/qv4stringobject.cpp +++ b/src/qml/jsruntime/qv4stringobject.cpp @@ -1,47 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qv4stringobject_p.h" #include "qv4regexp_p.h" #include "qv4regexpobject_p.h" -#include "qv4objectproto_p.h" #include <private/qv4mm_p.h> #include "qv4scopedvalue_p.h" #include "qv4symbol_p.h" @@ -51,6 +14,7 @@ #include <QtCore/QDateTime> #include <QtCore/QDebug> #include <QtCore/QStringList> +#include <QtQml/private/qv4runtime_p.h> #include <cassert> @@ -62,10 +26,11 @@ # include "qplatformdefs.h" # endif #else -# include <windows.h> +# include <qt_windows.h> #endif using namespace QV4; +using namespace Qt::Literals::StringLiterals; DEFINE_OBJECT_VTABLE(StringObject); @@ -87,7 +52,7 @@ void Heap::StringObject::init(const QV4::String *str) Heap::String *Heap::StringObject::getIndex(uint index) const { QString str = string->toQString(); - if (index >= (uint)str.length()) + if (index >= (uint)str.size()) return nullptr; return internalClass->engine->newString(str.mid(index, 1)); } @@ -103,7 +68,7 @@ bool StringObject::virtualDeleteProperty(Managed *m, PropertyKey id) if (id.isArrayIndex()) { StringObject *o = static_cast<StringObject *>(m); uint index = id.asArrayIndex(); - if (index < static_cast<uint>(o->d()->string->toQString().length())) + if (index < static_cast<uint>(o->d()->string->toQString().size())) return false; } return Object::virtualDeleteProperty(m, id); @@ -119,7 +84,7 @@ struct StringObjectOwnPropertyKeyIterator : ObjectOwnPropertyKeyIterator PropertyKey StringObjectOwnPropertyKeyIterator::next(const QV4::Object *o, Property *pd, PropertyAttributes *attrs) { const StringObject *s = static_cast<const StringObject *>(o); - uint slen = s->d()->string->toQString().length(); + uint slen = s->d()->string->toQString().size(); if (arrayIndex < slen) { uint index = arrayIndex; ++arrayIndex; @@ -155,7 +120,7 @@ PropertyAttributes StringObject::virtualGetOwnProperty(const Managed *m, Propert if (id.isArrayIndex()) { const uint index = id.asArrayIndex(); const auto s = static_cast<const StringObject *>(m); - if (index < uint(s->d()->string->toQString().length())) { + if (index < uint(s->d()->string->toQString().size())) { if (p) p->value = s->getIndex(index); return Attr_NotConfigurable|Attr_NotWritable; @@ -266,14 +231,14 @@ ReturnedValue StringCtor::method_raw(const FunctionObject *f, const Value *, con while (1) { val = raw->get(nextIndex); result += val->toQString(); - if (scope.engine->hasException) + if (scope.hasException()) return Encode::undefined(); if (nextIndex + 1 == literalSegments) return scope.engine->newString(result)->asReturnedValue(); if (nextIndex < static_cast<uint>(argc)) result += argv[nextIndex].toQString(); - if (scope.engine->hasException) + if (scope.hasException()) return Encode::undefined(); ++nextIndex; } @@ -369,12 +334,12 @@ ReturnedValue StringPrototype::method_charAt(const FunctionObject *b, const Valu if (v4->hasException) return QV4::Encode::undefined(); - int pos = 0; + double pos = 0; if (argc > 0) - pos = (int) argv[0].toInteger(); + pos = argv[0].toInteger(); QString result; - if (pos >= 0 && pos < str.length()) + if (pos >= 0 && pos < str.size()) result += str.at(pos); return Encode(v4->newString(result)); @@ -387,12 +352,12 @@ ReturnedValue StringPrototype::method_charCodeAt(const FunctionObject *b, const if (v4->hasException) return QV4::Encode::undefined(); - int pos = 0; + double pos = 0; if (argc > 0) - pos = (int) argv[0].toInteger(); + pos = argv[0].toInteger(); - if (pos >= 0 && pos < str.length()) + if (pos >= 0 && pos < str.size()) RETURN_RESULT(Encode(str.at(pos).unicode())); return Encode(qt_qnan()); @@ -405,7 +370,7 @@ ReturnedValue StringPrototype::method_codePointAt(const FunctionObject *f, const if (v4->hasException) return QV4::Encode::undefined(); - int index = argc ? argv[0].toInteger() : 0; + double index = argc ? argv[0].toInteger() : 0.0; if (v4->hasException) return QV4::Encode::undefined(); @@ -455,11 +420,11 @@ ReturnedValue StringPrototype::method_endsWith(const FunctionObject *b, const Va if (v4->hasException) return Encode::undefined(); - int pos = value.length(); + double pos = value.size(); if (argc > 1) - pos = (int) argv[1].toInteger(); + pos = argv[1].toInteger(); - if (pos == value.length()) + if (pos == value.size()) RETURN_RESULT(Encode(value.endsWith(searchString))); QStringView stringToSearch = QStringView{value}.left(pos); @@ -477,13 +442,13 @@ ReturnedValue StringPrototype::method_indexOf(const FunctionObject *b, const Val if (v4->hasException) return Encode::undefined(); - int pos = 0; + double pos = 0; if (argc > 1) - pos = (int) argv[1].toInteger(); + pos = argv[1].toInteger(); int index = -1; - if (! value.isEmpty()) - index = value.indexOf(searchString, qMin(qMax(pos, 0), value.length())); + if (!value.isEmpty()) + index = value.indexOf(searchString, qMin(qMax(pos, 0.0), double(value.size()))); return Encode(index); } @@ -501,12 +466,12 @@ ReturnedValue StringPrototype::method_includes(const FunctionObject *b, const Va if (v4->hasException) return Encode::undefined(); - int pos = 0; + double pos = 0; if (argc > 1) { const Value &posArg = argv[1]; - pos = (int) posArg.toInteger(); + pos = posArg.toInteger(); if (!posArg.isInteger() && posArg.isNumber() && qIsInf(posArg.toNumber())) - pos = value.length(); + pos = value.size(); } if (pos == 0) @@ -533,8 +498,8 @@ ReturnedValue StringPrototype::method_lastIndexOf(const FunctionObject *b, const else position = std::trunc(position); - int pos = std::trunc(qMin(qMax(position, 0.0), double(value.length()))); - if (!searchString.isEmpty() && pos == value.length()) + int pos = std::trunc(qMin(qMax(position, 0.0), double(value.size()))); + if (!searchString.isEmpty() && pos == value.size()) --pos; if (searchString.isNull() && pos == 0) RETURN_RESULT(Encode(-1)); @@ -632,10 +597,10 @@ ReturnedValue StringPrototype::method_padEnd(const FunctionObject *f, const Valu if (!argc) return s->asReturnedValue(); - int maxLen = argv[0].toInteger(); + double maxLen = argv[0].toInteger(); if (maxLen <= s->d()->length()) return s->asReturnedValue(); - QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : QString::fromLatin1(" "); + QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : u" "_s; if (v4->hasException) return Encode::undefined(); @@ -643,12 +608,12 @@ ReturnedValue StringPrototype::method_padEnd(const FunctionObject *f, const Valu return s->asReturnedValue(); QString padded = s->toQString(); - int oldLength = padded.length(); + int oldLength = padded.size(); int toFill = maxLen - oldLength; padded.resize(maxLen); QChar *ch = padded.data() + oldLength; while (toFill) { - int copy = qMin(fillString.length(), toFill); + int copy = qMin(fillString.size(), toFill); memcpy(ch, fillString.constData(), copy*sizeof(QChar)); toFill -= copy; ch += copy; @@ -671,10 +636,10 @@ ReturnedValue StringPrototype::method_padStart(const FunctionObject *f, const Va if (!argc) return s->asReturnedValue(); - int maxLen = argv[0].toInteger(); + double maxLen = argv[0].toInteger(); if (maxLen <= s->d()->length()) return s->asReturnedValue(); - QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : QString::fromLatin1(" "); + QString fillString = (argc > 1 && !argv[1].isUndefined()) ? argv[1].toQString() : u" "_s; if (v4->hasException) return Encode::undefined(); @@ -682,13 +647,13 @@ ReturnedValue StringPrototype::method_padStart(const FunctionObject *f, const Va return s->asReturnedValue(); QString original = s->toQString(); - int oldLength = original.length(); + int oldLength = original.size(); int toFill = maxLen - oldLength; QString padded; padded.resize(maxLen); QChar *ch = padded.data(); while (toFill) { - int copy = qMin(fillString.length(), toFill); + int copy = qMin(fillString.size(), toFill); memcpy(ch, fillString.constData(), copy*sizeof(QChar)); toFill -= copy; ch += copy; @@ -718,9 +683,9 @@ ReturnedValue StringPrototype::method_repeat(const FunctionObject *b, const Valu static void appendReplacementString(QString *result, const QString &input, const QString& replaceValue, uint* matchOffsets, int captureCount) { - result->reserve(result->length() + replaceValue.length()); - for (int i = 0; i < replaceValue.length(); ++i) { - if (replaceValue.at(i) == QLatin1Char('$') && i < replaceValue.length() - 1) { + result->reserve(result->size() + replaceValue.size()); + for (int i = 0; i < replaceValue.size(); ++i) { + if (replaceValue.at(i) == QLatin1Char('$') && i < replaceValue.size() - 1) { ushort ch = replaceValue.at(i + 1).unicode(); uint substStart = JSC::Yarr::offsetNoMatch; uint substEnd = JSC::Yarr::offsetNoMatch; @@ -739,12 +704,12 @@ static void appendReplacementString(QString *result, const QString &input, const skip = 1; } else if (ch == '\'') { substStart = matchOffsets[1]; - substEnd = input.length(); + substEnd = input.size(); skip = 1; } else if (ch >= '0' && ch <= '9') { uint capture = ch - '0'; skip = 1; - if (i < replaceValue.length() - 2) { + if (i < replaceValue.size() - 2) { ch = replaceValue.at(i + 2).unicode(); if (ch >= '0' && ch <= '9') { uint c = capture*10 + ch - '0'; @@ -829,7 +794,7 @@ ReturnedValue StringPrototype::method_replace(const FunctionObject *b, const Val if (idx != -1) { numStringMatches = 1; matchOffsets[0] = idx; - matchOffsets[1] = idx + searchString.length(); + matchOffsets[1] = idx + searchString.size(); } } @@ -838,7 +803,7 @@ ReturnedValue StringPrototype::method_replace(const FunctionObject *b, const Val ScopedValue replaceValue(scope, argc > 1 ? argv[1] : Value::undefinedValue()); ScopedFunctionObject searchCallback(scope, replaceValue); if (!!searchCallback) { - result.reserve(string.length() + 10*numStringMatches); + result.reserve(string.size() + 10*numStringMatches); ScopedValue entry(scope); Value *arguments = scope.alloc(numCaptures + 2); int lastEnd = 0; @@ -868,7 +833,7 @@ ReturnedValue StringPrototype::method_replace(const FunctionObject *b, const Val result += QStringView{string}.mid(lastEnd); } else { QString newString = replaceValue->toQString(); - result.reserve(string.length() + numStringMatches*newString.size()); + result.reserve(string.size() + numStringMatches*newString.size()); int lastEnd = 0; for (int i = 0; i < numStringMatches; ++i) { @@ -895,13 +860,13 @@ ReturnedValue StringPrototype::method_search(const FunctionObject *b, const Valu { Scope scope(b); QString string = getThisString(scope.engine, thisObject); - if (scope.engine->hasException) + if (scope.hasException()) return QV4::Encode::undefined(); Scoped<RegExpObject> regExp(scope, argc ? argv[0] : Value::undefinedValue()); if (!regExp) { regExp = scope.engine->regExpCtor()->callAsConstructor(argv, 1); - if (scope.engine->hasException) + if (scope.hasException()) return QV4::Encode::undefined(); Q_ASSERT(regExp); @@ -1011,7 +976,7 @@ ReturnedValue StringPrototype::method_split(const FunctionObject *b, const Value } else { QString separator = separatorValue->toQString(); if (separator.isEmpty()) { - for (uint i = 0; i < qMin(limit, uint(text.length())); ++i) + for (uint i = 0; i < qMin(limit, uint(text.size())); ++i) array->push_back((s = scope.engine->newString(text.mid(i, 1)))); return array.asReturnedValue(); } @@ -1043,9 +1008,9 @@ ReturnedValue StringPrototype::method_startsWith(const FunctionObject *b, const if (v4->hasException) return Encode::undefined(); - int pos = 0; + double pos = 0; if (argc > 1) - pos = (int) argv[1].toInteger(); + pos = argv[1].toInteger(); if (pos == 0) return Encode(value.startsWith(searchString)); @@ -1069,7 +1034,7 @@ ReturnedValue StringPrototype::method_substr(const FunctionObject *b, const Valu if (argc > 1) length = argv[1].toInteger(); - double count = value.length(); + double count = value.size(); if (start < 0) start = qMax(count + start, 0.0); @@ -1087,7 +1052,7 @@ ReturnedValue StringPrototype::method_substring(const FunctionObject *b, const V if (v4->hasException) return QV4::Encode::undefined(); - int length = value.length(); + int length = value.size(); double start = 0; double end = length; @@ -1160,11 +1125,11 @@ ReturnedValue StringPrototype::method_trim(const FunctionObject *b, const Value const QChar *chars = s.constData(); int start, end; - for (start = 0; start < s.length(); ++start) { + for (start = 0; start < s.size(); ++start) { if (!chars[start].isSpace() && chars[start].unicode() != 0xfeff) break; } - for (end = s.length() - 1; end >= start; --end) { + for (end = s.size() - 1; end >= start; --end) { if (!chars[end].isSpace() && chars[end].unicode() != 0xfeff) break; } |