Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4stringiterator.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Crimson AS <info@crimson.no>
2// Copyright (C) 2018 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant
5
6#include <private/qv4iterator_p.h>
7#include <private/qv4stringiterator_p.h>
8#include <private/qv4symbol_p.h>
9
10using namespace QV4;
11
13
14void StringIteratorPrototype::init(ExecutionEngine *e)
15{
16 defineDefaultProperty(QStringLiteral("next"), method_next, 0);
17
18 Scope scope(e);
19 ScopedString val(scope, e->newString(QLatin1String("String Iterator")));
20 defineReadonlyConfigurableProperty(e->symbol_toStringTag(), val);
21}
22
23ReturnedValue StringIteratorPrototype::method_next(const FunctionObject *b, const Value *that, const Value *, int)
24{
25 Scope scope(b);
26 const StringIteratorObject *thisObject = that->as<StringIteratorObject>();
27 if (!thisObject)
28 return scope.engine->throwTypeError(QLatin1String("Not an String Iterator instance"));
29
30 ScopedString s(scope, thisObject->d()->iteratedString);
31 if (!s) {
32 QV4::Value undefined = Value::undefinedValue();
33 return IteratorPrototype::createIterResultObject(scope.engine, undefined, true);
34 }
35
36 quint32 index = thisObject->d()->nextIndex;
37
38 QString str = s->toQString();
39 quint32 len = str.size();
40
41 if (index >= len) {
42 thisObject->d()->iteratedString.set(scope.engine, nullptr);
43 QV4::Value undefined = Value::undefinedValue();
44 return IteratorPrototype::createIterResultObject(scope.engine, undefined, true);
45 }
46
47 QChar ch = str.at(index);
48 int num = 1;
49 if (ch.unicode() >= 0xd800 && ch.unicode() <= 0xdbff && index + 1 != len) {
50 ch = str.at(index + 1);
51 if (ch.unicode() >= 0xdc00 && ch.unicode() <= 0xdfff)
52 num = 2;
53 }
54
55 thisObject->d()->nextIndex += num;
56
57 ScopedString resultString(scope, scope.engine->newString(s->toQString().mid(index, num)));
58 return IteratorPrototype::createIterResultObject(scope.engine, resultString, false);
59}
DEFINE_OBJECT_VTABLE(StringIteratorObject)