diff options
author | Michael Brasser <michael.brasser@live.com> | 2016-04-12 21:52:50 -0500 |
---|---|---|
committer | Michael Brasser <michael.brasser@live.com> | 2016-04-25 14:15:48 +0000 |
commit | 26ecc82374c476ee417ab04c455022f76909ba51 (patch) | |
tree | d197a7cf6b01451319c5b8880b1d375c587e4f00 /src | |
parent | 223516ae2f7131cc7f8972e9afdeca970d85473d (diff) |
Add string search functions from ECMAScript 2015.
Change-Id: I46d5e42186d9f1b2994778de967610725b0e0633
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/doc/src/javascript/functionlist.qdoc | 3 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4stringobject.cpp | 80 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4stringobject_p.h | 3 |
3 files changed, 86 insertions, 0 deletions
diff --git a/src/qml/doc/src/javascript/functionlist.qdoc b/src/qml/doc/src/javascript/functionlist.qdoc index 7f0e844b65..8b9c33f994 100644 --- a/src/qml/doc/src/javascript/functionlist.qdoc +++ b/src/qml/doc/src/javascript/functionlist.qdoc @@ -174,6 +174,8 @@ \li charAt(pos) \li charCodeAt(pos) \li concat([string1 [, string2 [, ...]]]) + \li endsWith(searchString [, endPosition ]) // ECMAScript 6: Added in Qt 5.8 + \li includes(searchString [, position ]) // ECMAScript 6: Added in 5.8 \li indexOf(searchString ,position) \li lastIndexOf(searchString, position) \li localeCompare(that) @@ -182,6 +184,7 @@ \li search(regexp) \li slice(start, end) \li split(separator, limit) + \li startsWith(searchString [, position ]) // ECMAScript 6: Added in Qt 5.8 \li substring(start, end) \li toLowerCase() \li toLocaleLowerCase() diff --git a/src/qml/jsruntime/qv4stringobject.cpp b/src/qml/jsruntime/qv4stringobject.cpp index b874766655..b4f04bbc76 100644 --- a/src/qml/jsruntime/qv4stringobject.cpp +++ b/src/qml/jsruntime/qv4stringobject.cpp @@ -196,7 +196,9 @@ void StringPrototype::init(ExecutionEngine *engine, Object *ctor) defineDefaultProperty(QStringLiteral("charAt"), method_charAt, 1); defineDefaultProperty(QStringLiteral("charCodeAt"), method_charCodeAt, 1); defineDefaultProperty(QStringLiteral("concat"), method_concat, 1); + defineDefaultProperty(QStringLiteral("endsWith"), method_endsWith, 1); defineDefaultProperty(QStringLiteral("indexOf"), method_indexOf, 1); + defineDefaultProperty(QStringLiteral("includes"), method_includes, 1); defineDefaultProperty(QStringLiteral("lastIndexOf"), method_lastIndexOf, 1); defineDefaultProperty(QStringLiteral("localeCompare"), method_localeCompare, 1); defineDefaultProperty(QStringLiteral("match"), method_match, 1); @@ -204,6 +206,7 @@ void StringPrototype::init(ExecutionEngine *engine, Object *ctor) defineDefaultProperty(QStringLiteral("search"), method_search, 1); defineDefaultProperty(QStringLiteral("slice"), method_slice, 2); defineDefaultProperty(QStringLiteral("split"), method_split, 2); + defineDefaultProperty(QStringLiteral("startsWith"), method_startsWith, 1); defineDefaultProperty(QStringLiteral("substr"), method_substr, 2); defineDefaultProperty(QStringLiteral("substring"), method_substring, 2); defineDefaultProperty(QStringLiteral("toLowerCase"), method_toLowerCase); @@ -293,6 +296,30 @@ ReturnedValue StringPrototype::method_concat(CallContext *context) return context->d()->engine->newString(value)->asReturnedValue(); } +ReturnedValue StringPrototype::method_endsWith(CallContext *context) +{ + QString value = getThisString(context); + if (context->d()->engine->hasException) + return Encode::undefined(); + + QString searchString; + if (context->argc()) { + if (context->args()[0].as<RegExpObject>()) + return context->engine()->throwTypeError(); + searchString = context->args()[0].toQString(); + } + + int pos = value.length(); + if (context->argc() > 1) + pos = (int) context->args()[1].toInteger(); + + if (pos == value.length()) + return Encode(value.endsWith(searchString)); + + QStringRef stringToSearch = value.leftRef(pos); + return Encode(stringToSearch.endsWith(searchString)); +} + ReturnedValue StringPrototype::method_indexOf(CallContext *context) { QString value = getThisString(context); @@ -314,6 +341,35 @@ ReturnedValue StringPrototype::method_indexOf(CallContext *context) return Encode(index); } +ReturnedValue StringPrototype::method_includes(CallContext *context) +{ + QString value = getThisString(context); + if (context->d()->engine->hasException) + return Encode::undefined(); + + QString searchString; + if (context->argc()) { + if (context->args()[0].as<RegExpObject>()) + return context->engine()->throwTypeError(); + searchString = context->args()[0].toQString(); + } + + int pos = 0; + if (context->argc() > 1) { + Scope scope(context); + ScopedValue posArg(scope, context->argument(1)); + pos = (int) posArg->toInteger(); + if (!posArg->isInteger() && posArg->isNumber() && qIsInf(posArg->toNumber())) + pos = value.length(); + } + + if (pos == 0) + return Encode(value.contains(searchString)); + + QStringRef stringToSearch = value.midRef(pos); + return Encode(stringToSearch.contains(searchString)); +} + ReturnedValue StringPrototype::method_lastIndexOf(CallContext *context) { Scope scope(context); @@ -716,6 +772,30 @@ ReturnedValue StringPrototype::method_split(CallContext *ctx) return array.asReturnedValue(); } +ReturnedValue StringPrototype::method_startsWith(CallContext *context) +{ + QString value = getThisString(context); + if (context->d()->engine->hasException) + return Encode::undefined(); + + QString searchString; + if (context->argc()) { + if (context->args()[0].as<RegExpObject>()) + return context->engine()->throwTypeError(); + searchString = context->args()[0].toQString(); + } + + int pos = 0; + if (context->argc() > 1) + pos = (int) context->args()[1].toInteger(); + + if (pos == 0) + return Encode(value.startsWith(searchString)); + + QStringRef stringToSearch = value.midRef(pos); + return Encode(stringToSearch.startsWith(searchString)); +} + ReturnedValue StringPrototype::method_substr(CallContext *context) { const QString value = getThisString(context); diff --git a/src/qml/jsruntime/qv4stringobject_p.h b/src/qml/jsruntime/qv4stringobject_p.h index 3930a011e6..fb229d4aff 100644 --- a/src/qml/jsruntime/qv4stringobject_p.h +++ b/src/qml/jsruntime/qv4stringobject_p.h @@ -115,7 +115,9 @@ struct StringPrototype: StringObject static ReturnedValue method_charAt(CallContext *context); static ReturnedValue method_charCodeAt(CallContext *context); static ReturnedValue method_concat(CallContext *context); + static ReturnedValue method_endsWith(CallContext *ctx); static ReturnedValue method_indexOf(CallContext *context); + static ReturnedValue method_includes(CallContext *context); static ReturnedValue method_lastIndexOf(CallContext *context); static ReturnedValue method_localeCompare(CallContext *context); static ReturnedValue method_match(CallContext *context); @@ -123,6 +125,7 @@ struct StringPrototype: StringObject static ReturnedValue method_search(CallContext *ctx); static ReturnedValue method_slice(CallContext *ctx); static ReturnedValue method_split(CallContext *ctx); + static ReturnedValue method_startsWith(CallContext *ctx); static ReturnedValue method_substr(CallContext *context); static ReturnedValue method_substring(CallContext *context); static ReturnedValue method_toLowerCase(CallContext *ctx); |