| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qscriptecmastring_p.h" | 
|---|
| 43 |  | 
|---|
| 44 | #ifndef QT_NO_SCRIPT | 
|---|
| 45 |  | 
|---|
| 46 | #include "qscriptengine_p.h" | 
|---|
| 47 | #include "qscriptvalueimpl_p.h" | 
|---|
| 48 | #include "qscriptcontext_p.h" | 
|---|
| 49 | #include "qscriptmember_p.h" | 
|---|
| 50 | #include "qscriptobject_p.h" | 
|---|
| 51 | #include "qscriptclassdata_p.h" | 
|---|
| 52 |  | 
|---|
| 53 | #include <QtCore/QStringList> | 
|---|
| 54 | #include <QtCore/QtDebug> | 
|---|
| 55 | #include <QtCore/qnumeric.h> | 
|---|
| 56 |  | 
|---|
| 57 | #include <limits.h> | 
|---|
| 58 |  | 
|---|
| 59 | QT_BEGIN_NAMESPACE | 
|---|
| 60 |  | 
|---|
| 61 | namespace QScript { namespace Ecma { | 
|---|
| 62 |  | 
|---|
| 63 | class StringClassData: public QScriptClassData | 
|---|
| 64 | { | 
|---|
| 65 | QScriptClassInfo *m_classInfo; | 
|---|
| 66 |  | 
|---|
| 67 | public: | 
|---|
| 68 | StringClassData(QScriptClassInfo *classInfo); | 
|---|
| 69 | virtual ~StringClassData(); | 
|---|
| 70 |  | 
|---|
| 71 | inline QScriptClassInfo *classInfo() const | 
|---|
| 72 | { return m_classInfo; } | 
|---|
| 73 |  | 
|---|
| 74 | virtual bool resolve(const QScriptValueImpl &object, | 
|---|
| 75 | QScriptNameIdImpl *nameId, | 
|---|
| 76 | QScript::Member *member, QScriptValueImpl *base, | 
|---|
| 77 | QScript::AccessMode access); | 
|---|
| 78 | virtual bool get(const QScriptValueImpl &object, const Member &member, | 
|---|
| 79 | QScriptValueImpl *out_value); | 
|---|
| 80 | virtual bool put(QScriptValueImpl *object, const Member &member, | 
|---|
| 81 | const QScriptValueImpl &value); | 
|---|
| 82 | virtual QScriptClassDataIterator *newIterator(const QScriptValueImpl &object); | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 | class StringClassDataIterator: public QScriptClassDataIterator | 
|---|
| 86 | { | 
|---|
| 87 | public: | 
|---|
| 88 | StringClassDataIterator(int length); | 
|---|
| 89 | virtual ~StringClassDataIterator(); | 
|---|
| 90 |  | 
|---|
| 91 | virtual bool hasNext() const; | 
|---|
| 92 | virtual void next(QScript::Member *member); | 
|---|
| 93 |  | 
|---|
| 94 | virtual bool hasPrevious() const; | 
|---|
| 95 | virtual void previous(QScript::Member *member); | 
|---|
| 96 |  | 
|---|
| 97 | virtual void toFront(); | 
|---|
| 98 | virtual void toBack(); | 
|---|
| 99 |  | 
|---|
| 100 | private: | 
|---|
| 101 | int m_length; | 
|---|
| 102 | int m_pos; | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | StringClassData::StringClassData(QScriptClassInfo *classInfo): | 
|---|
| 106 | m_classInfo(classInfo) | 
|---|
| 107 | { | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | StringClassData::~StringClassData() | 
|---|
| 111 | { | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | bool StringClassData::resolve(const QScriptValueImpl &object, | 
|---|
| 115 | QScriptNameIdImpl *nameId, | 
|---|
| 116 | QScript::Member *member, | 
|---|
| 117 | QScriptValueImpl *base, | 
|---|
| 118 | QScript::AccessMode /*access*/) | 
|---|
| 119 | { | 
|---|
| 120 | if (object.classInfo() != classInfo()) | 
|---|
| 121 | return false; | 
|---|
| 122 |  | 
|---|
| 123 | QScriptEnginePrivate *eng = object.engine(); | 
|---|
| 124 |  | 
|---|
| 125 | if (nameId == eng->idTable()->id_length) { | 
|---|
| 126 | member->native(nameId, /*id=*/ 0, | 
|---|
| 127 | QScriptValue::Undeletable | 
|---|
| 128 | | QScriptValue::ReadOnly | 
|---|
| 129 | | QScriptValue::SkipInEnumeration); | 
|---|
| 130 | *base = object; | 
|---|
| 131 | return true; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | bool ok = false; | 
|---|
| 135 | int index = nameId->s.toInt(&ok); | 
|---|
| 136 | if (!ok || (index < 0)) | 
|---|
| 137 | return false; | 
|---|
| 138 |  | 
|---|
| 139 | QScriptNameIdImpl *ref = object.internalValue().stringValue(); | 
|---|
| 140 | if (index >= ref->s.length()) | 
|---|
| 141 | return false; | 
|---|
| 142 |  | 
|---|
| 143 | member->native(nameId, index, QScriptValue::Undeletable | QScriptValue::ReadOnly); | 
|---|
| 144 | return true; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | bool StringClassData::get(const QScriptValueImpl &object, | 
|---|
| 148 | const QScript::Member &member, | 
|---|
| 149 | QScriptValueImpl *result) | 
|---|
| 150 | { | 
|---|
| 151 | Q_ASSERT(member.isValid()); | 
|---|
| 152 |  | 
|---|
| 153 | if (object.classInfo() != classInfo()) | 
|---|
| 154 | return false; | 
|---|
| 155 |  | 
|---|
| 156 | QScriptEnginePrivate *eng = object.engine(); | 
|---|
| 157 | if (! member.isNativeProperty()) | 
|---|
| 158 | return false; | 
|---|
| 159 |  | 
|---|
| 160 | QScriptNameIdImpl *ref = object.internalValue().stringValue(); | 
|---|
| 161 | int len = ref->s.length(); | 
|---|
| 162 |  | 
|---|
| 163 | if (member.nameId() == eng->idTable()->id_length) | 
|---|
| 164 | *result = QScriptValueImpl(len); | 
|---|
| 165 |  | 
|---|
| 166 | else if (member.id() >= 0 && member.id() < len) | 
|---|
| 167 | eng->newString(result, ref->s.at(member.id())); | 
|---|
| 168 |  | 
|---|
| 169 | else | 
|---|
| 170 | *result = eng->undefinedValue(); | 
|---|
| 171 |  | 
|---|
| 172 | return true; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | bool StringClassData::put(QScriptValueImpl *, const Member &, | 
|---|
| 176 | const QScriptValueImpl &) | 
|---|
| 177 | { | 
|---|
| 178 | // writes to string elements are ignored | 
|---|
| 179 | return true; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | QScriptClassDataIterator *StringClassData::newIterator(const QScriptValueImpl &object) | 
|---|
| 183 | { | 
|---|
| 184 | QScriptNameIdImpl *id = object.internalValue().stringValue(); | 
|---|
| 185 | return new StringClassDataIterator(id->s.length()); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | StringClassDataIterator::StringClassDataIterator(int length) | 
|---|
| 189 | { | 
|---|
| 190 | m_length = length; | 
|---|
| 191 | m_pos = 0; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | StringClassDataIterator::~StringClassDataIterator() | 
|---|
| 195 | { | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | bool StringClassDataIterator::hasNext() const | 
|---|
| 199 | { | 
|---|
| 200 | return m_pos < m_length; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | void StringClassDataIterator::next(QScript::Member *member) | 
|---|
| 204 | { | 
|---|
| 205 | member->native(/*nameId=*/ 0, m_pos, QScriptValue::Undeletable | QScriptValue::ReadOnly); | 
|---|
| 206 | ++m_pos; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | bool StringClassDataIterator::hasPrevious() const | 
|---|
| 210 | { | 
|---|
| 211 | return (m_pos - 1) >= 0; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | void StringClassDataIterator::previous(QScript::Member *member) | 
|---|
| 215 | { | 
|---|
| 216 | --m_pos; | 
|---|
| 217 | member->native(/*nameId=*/ 0, m_pos, QScriptValue::Undeletable | QScriptValue::ReadOnly); | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | void StringClassDataIterator::toFront() | 
|---|
| 221 | { | 
|---|
| 222 | m_pos = 0; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | void StringClassDataIterator::toBack() | 
|---|
| 226 | { | 
|---|
| 227 | m_pos = m_length; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 |  | 
|---|
| 231 |  | 
|---|
| 232 | String::String(QScriptEnginePrivate *eng): | 
|---|
| 233 | Core(eng, QLatin1String("String"), QScriptClassInfo::StringType) | 
|---|
| 234 | { | 
|---|
| 235 | classInfo()->setData(new StringClassData(classInfo())); | 
|---|
| 236 |  | 
|---|
| 237 | newString(&publicPrototype, QString()); | 
|---|
| 238 |  | 
|---|
| 239 | eng->newConstructor(&ctor, this, publicPrototype); | 
|---|
| 240 |  | 
|---|
| 241 | addPrototypeFunction(QLatin1String("toString"), method_toString, 0); | 
|---|
| 242 | addPrototypeFunction(QLatin1String("valueOf"), method_valueOf, 0); | 
|---|
| 243 | addPrototypeFunction(QLatin1String("charAt"), method_charAt, 1); | 
|---|
| 244 | addPrototypeFunction(QLatin1String("charCodeAt"), method_charCodeAt, 1); | 
|---|
| 245 | addPrototypeFunction(QLatin1String("concat"), method_concat, 1); | 
|---|
| 246 | addPrototypeFunction(QLatin1String("indexOf"), method_indexOf, 1); | 
|---|
| 247 | addPrototypeFunction(QLatin1String("lastIndexOf"), method_lastIndexOf, 1); | 
|---|
| 248 | addPrototypeFunction(QLatin1String("localeCompare"), method_localeCompare, 1); | 
|---|
| 249 | addPrototypeFunction(QLatin1String("match"), method_match, 1); | 
|---|
| 250 | addPrototypeFunction(QLatin1String("replace"), method_replace, 2); | 
|---|
| 251 | addPrototypeFunction(QLatin1String("search"), method_search, 1); | 
|---|
| 252 | addPrototypeFunction(QLatin1String("slice"), method_slice, 2); | 
|---|
| 253 | addPrototypeFunction(QLatin1String("split"), method_split, 2); | 
|---|
| 254 | addPrototypeFunction(QLatin1String("substr"), method_substr, 2); | 
|---|
| 255 | addPrototypeFunction(QLatin1String("substring"), method_substring, 2); | 
|---|
| 256 | addPrototypeFunction(QLatin1String("toLowerCase"), method_toLowerCase, 0); | 
|---|
| 257 | addPrototypeFunction(QLatin1String("toLocaleLowerCase"), method_toLocaleLowerCase, 0); | 
|---|
| 258 | addPrototypeFunction(QLatin1String("toUpperCase"), method_toUpperCase, 0); | 
|---|
| 259 | addPrototypeFunction(QLatin1String("toLocaleUpperCase"), method_toLocaleUpperCase, 0); | 
|---|
| 260 |  | 
|---|
| 261 | addConstructorFunction(QLatin1String("fromCharCode"), method_fromCharCode, 1); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | String::~String() | 
|---|
| 265 | { | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | void String::execute(QScriptContextPrivate *context) | 
|---|
| 269 | { | 
|---|
| 270 | #ifndef Q_SCRIPT_NO_EVENT_NOTIFY | 
|---|
| 271 | engine()->notifyFunctionEntry(context); | 
|---|
| 272 | #endif | 
|---|
| 273 | QString value; | 
|---|
| 274 |  | 
|---|
| 275 | if (context->argumentCount() > 0) | 
|---|
| 276 | value = context->argument(0).toString(); | 
|---|
| 277 |  | 
|---|
| 278 | QScriptValueImpl str(engine(), value); | 
|---|
| 279 | if (!context->isCalledAsConstructor()) { | 
|---|
| 280 | context->setReturnValue(str); | 
|---|
| 281 | } else { | 
|---|
| 282 | QScriptValueImpl &obj = context->m_thisObject; | 
|---|
| 283 | obj.setClassInfo(classInfo()); | 
|---|
| 284 | obj.setInternalValue(str); | 
|---|
| 285 | obj.setPrototype(publicPrototype); | 
|---|
| 286 | context->setReturnValue(obj); | 
|---|
| 287 | } | 
|---|
| 288 | #ifndef Q_SCRIPT_NO_EVENT_NOTIFY | 
|---|
| 289 | engine()->notifyFunctionExit(context); | 
|---|
| 290 | #endif | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | void String::newString(QScriptValueImpl *result, const QString &value) | 
|---|
| 294 | { | 
|---|
| 295 | engine()->newObject(result, publicPrototype, classInfo()); | 
|---|
| 296 | result->setInternalValue(QScriptValueImpl(engine(), value)); | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | QScriptValueImpl String::method_toString(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *classInfo) | 
|---|
| 300 | { | 
|---|
| 301 | QScriptValueImpl self = context->thisObject(); | 
|---|
| 302 | if (self.classInfo() != classInfo) { | 
|---|
| 303 | return context->throwError(QScriptContext::TypeError, QLatin1String("String.prototype.toString")); | 
|---|
| 304 | } | 
|---|
| 305 | return (self.internalValue()); | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | QScriptValueImpl String::method_valueOf(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *classInfo) | 
|---|
| 309 | { | 
|---|
| 310 | QScriptValueImpl self = context->thisObject(); | 
|---|
| 311 | if (self.classInfo() != classInfo) { | 
|---|
| 312 | return throwThisObjectTypeError( | 
|---|
| 313 | context, QLatin1String("String.prototype.valueOf")); | 
|---|
| 314 | } | 
|---|
| 315 | return (self.internalValue()); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | QScriptValueImpl String::method_charAt(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 319 | { | 
|---|
| 320 | QString str = context->thisObject().toString(); | 
|---|
| 321 |  | 
|---|
| 322 | int pos = 0; | 
|---|
| 323 | if (context->argumentCount() > 0) | 
|---|
| 324 | pos = int (context->argument(0).toInteger()); | 
|---|
| 325 |  | 
|---|
| 326 | QString result; | 
|---|
| 327 | if (pos >= 0 && pos < str.length()) | 
|---|
| 328 | result += str.at(pos); | 
|---|
| 329 |  | 
|---|
| 330 | return (QScriptValueImpl(eng, result)); | 
|---|
| 331 | } | 
|---|
| 332 |  | 
|---|
| 333 | QScriptValueImpl String::method_charCodeAt(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *) | 
|---|
| 334 | { | 
|---|
| 335 | QString str = context->thisObject().toString(); | 
|---|
| 336 |  | 
|---|
| 337 | int pos = 0; | 
|---|
| 338 | if (context->argumentCount() > 0) | 
|---|
| 339 | pos = int (context->argument(0).toInteger()); | 
|---|
| 340 |  | 
|---|
| 341 | qsreal result = qSNaN(); | 
|---|
| 342 |  | 
|---|
| 343 | if (pos >= 0 && pos < str.length()) | 
|---|
| 344 | result = str.at(pos).unicode(); | 
|---|
| 345 |  | 
|---|
| 346 | return (QScriptValueImpl(result)); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | QScriptValueImpl String::method_concat(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 350 | { | 
|---|
| 351 | QString value = context->thisObject().toString(); | 
|---|
| 352 |  | 
|---|
| 353 | for (int i = 0; i < context->argumentCount(); ++i) | 
|---|
| 354 | value += context->argument(i).toString(); | 
|---|
| 355 |  | 
|---|
| 356 | return (QScriptValueImpl(eng, value)); | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | QScriptValueImpl String::method_indexOf(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *) | 
|---|
| 360 | { | 
|---|
| 361 | QString value = context->thisObject().toString(); | 
|---|
| 362 |  | 
|---|
| 363 | QString searchString = context->argument(0).toString(); | 
|---|
| 364 |  | 
|---|
| 365 | int pos = 0; | 
|---|
| 366 | if (context->argumentCount() > 1) | 
|---|
| 367 | pos = int (context->argument(1).toInteger()); | 
|---|
| 368 |  | 
|---|
| 369 | int index = -1; | 
|---|
| 370 | if (! value.isEmpty()) | 
|---|
| 371 | index = value.indexOf(searchString, qMin(qMax(pos, 0), value.length())); | 
|---|
| 372 |  | 
|---|
| 373 | return (QScriptValueImpl(index)); | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 | QScriptValueImpl String::method_lastIndexOf(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *) | 
|---|
| 377 | { | 
|---|
| 378 | QString value = context->thisObject().toString(); | 
|---|
| 379 |  | 
|---|
| 380 | QString searchString = context->argument(0).toString(); | 
|---|
| 381 |  | 
|---|
| 382 | qsreal position = context->argument(1).toNumber(); | 
|---|
| 383 | if (qIsNaN(position)) | 
|---|
| 384 | position = +qInf(); | 
|---|
| 385 | else | 
|---|
| 386 | position = QScriptEnginePrivate::toInteger(position); | 
|---|
| 387 |  | 
|---|
| 388 | int pos = QScriptEnginePrivate::toInt32(qMin(qMax(position, 0.0), qsreal(value.length()))); | 
|---|
| 389 | if (!searchString.isEmpty() && pos == value.length()) | 
|---|
| 390 | --pos; | 
|---|
| 391 | int index = value.lastIndexOf(searchString, pos); | 
|---|
| 392 | return (QScriptValueImpl(index)); | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | QScriptValueImpl String::method_localeCompare(QScriptContextPrivate *context, QScriptEnginePrivate *, QScriptClassInfo *) | 
|---|
| 396 | { | 
|---|
| 397 | QString value = context->thisObject().toString(); | 
|---|
| 398 | QString that = context->argument(0).toString(); | 
|---|
| 399 | return QScriptValueImpl(QString::localeAwareCompare(value, that)); | 
|---|
| 400 | } | 
|---|
| 401 |  | 
|---|
| 402 | QScriptValueImpl String::method_match(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 403 | { | 
|---|
| 404 | QScriptValueImpl pattern = context->argument(0); | 
|---|
| 405 |  | 
|---|
| 406 | if (! eng->regexpConstructor->get(pattern)) | 
|---|
| 407 | eng->regexpConstructor->newRegExp(&pattern, context->argument(0).toString(), /*flags=*/0); | 
|---|
| 408 |  | 
|---|
| 409 | QScriptValueImpl rx_exec = pattern.property(QLatin1String("exec"), QScriptValue::ResolvePrototype); | 
|---|
| 410 | if (! (rx_exec.isValid() && rx_exec.isFunction())) { | 
|---|
| 411 | return context->throwError(QScriptContext::TypeError, | 
|---|
| 412 | QLatin1String("String.prototype.match")); | 
|---|
| 413 | } | 
|---|
| 414 |  | 
|---|
| 415 | QScriptValueImplList args; | 
|---|
| 416 | args << context->thisObject(); | 
|---|
| 417 |  | 
|---|
| 418 | QScriptValueImpl global = pattern.property(QLatin1String("global")); | 
|---|
| 419 | if (! (global.isValid() && global.toBoolean())) | 
|---|
| 420 | return (rx_exec.call(pattern, args)); | 
|---|
| 421 |  | 
|---|
| 422 | QScript::Array result(eng); | 
|---|
| 423 |  | 
|---|
| 424 | QScriptNameIdImpl *lastIndexId = eng->nameId(QLatin1String("lastIndex")); | 
|---|
| 425 | QScriptNameIdImpl *zeroId = eng->nameId(QLatin1String("0")); | 
|---|
| 426 |  | 
|---|
| 427 | pattern.setProperty(lastIndexId, QScriptValueImpl(0)); | 
|---|
| 428 | int n = 0; | 
|---|
| 429 | while (true) { | 
|---|
| 430 | qsreal lastIndex = pattern.property(lastIndexId).toNumber(); | 
|---|
| 431 | QScriptValueImpl r = rx_exec.call(pattern, args); | 
|---|
| 432 | if (r.isNull()) | 
|---|
| 433 | break; | 
|---|
| 434 | qsreal newLastIndex = pattern.property(lastIndexId).toNumber(); | 
|---|
| 435 | if (newLastIndex == lastIndex) | 
|---|
| 436 | pattern.setProperty(lastIndexId, QScriptValueImpl(lastIndex + 1)); | 
|---|
| 437 | result.assign(n++, r.property(zeroId)); | 
|---|
| 438 | } | 
|---|
| 439 |  | 
|---|
| 440 | return (eng->newArray(result)); | 
|---|
| 441 | } | 
|---|
| 442 |  | 
|---|
| 443 | QScriptValueImpl String::method_replace(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 444 | { | 
|---|
| 445 | QString input = context->thisObject().toString(); | 
|---|
| 446 | QScriptValueImpl searchValue = context->argument(0); | 
|---|
| 447 | QScriptValueImpl replaceValue = context->argument(1); | 
|---|
| 448 |  | 
|---|
| 449 | QString output; | 
|---|
| 450 | if (searchValue.classInfo() == eng->regexpConstructor->classInfo()) { | 
|---|
| 451 | // searchValue is a RegExp | 
|---|
| 452 | QScriptValueImpl rx_exec = searchValue.property(QLatin1String("exec"), QScriptValue::ResolvePrototype); | 
|---|
| 453 | if (!rx_exec.isFunction()) { | 
|---|
| 454 | return context->throwError(QScriptContext::TypeError, | 
|---|
| 455 | QLatin1String("String.prototype.replace")); | 
|---|
| 456 | } | 
|---|
| 457 | QVector<QScriptValueImpl> occurrences; | 
|---|
| 458 | QScriptValueImpl global = searchValue.property(QLatin1String("global")); | 
|---|
| 459 | QScriptValueImplList args; | 
|---|
| 460 | args << QScriptValueImpl(eng, input); | 
|---|
| 461 | if (!global.toBoolean()) { | 
|---|
| 462 | QScriptValueImpl r = rx_exec.call(searchValue, args); | 
|---|
| 463 | if (!r.isNull()) | 
|---|
| 464 | occurrences.append(r); | 
|---|
| 465 | } else { | 
|---|
| 466 | QScriptNameIdImpl *lastIndexId = eng->nameId(QLatin1String("lastIndex")); | 
|---|
| 467 | searchValue.setProperty(lastIndexId, QScriptValueImpl(0)); | 
|---|
| 468 | while (true) { | 
|---|
| 469 | qsreal lastIndex = searchValue.property(lastIndexId).toNumber(); | 
|---|
| 470 | QScriptValueImpl r = rx_exec.call(searchValue, args); | 
|---|
| 471 | if (r.isNull()) | 
|---|
| 472 | break; | 
|---|
| 473 | qsreal newLastIndex = searchValue.property(lastIndexId).toNumber(); | 
|---|
| 474 | if (newLastIndex == lastIndex) | 
|---|
| 475 | searchValue.setProperty(lastIndexId, QScriptValueImpl(lastIndex + 1)); | 
|---|
| 476 | occurrences.append(r); | 
|---|
| 477 | } | 
|---|
| 478 | } | 
|---|
| 479 | int pos = 0; | 
|---|
| 480 | if (replaceValue.isFunction()) { | 
|---|
| 481 | QScriptNameIdImpl *indexId = eng->nameId(QLatin1String("index")); | 
|---|
| 482 | QScriptNameIdImpl *lengthId = eng->nameId(QLatin1String("length")); | 
|---|
| 483 | for (int i = 0; i < occurrences.count(); ++i) { | 
|---|
| 484 | QScriptValueImpl needle = occurrences.at(i); | 
|---|
| 485 | int index = int (needle.property(indexId).toInteger()); | 
|---|
| 486 | uint length = eng->toUint32(needle.property(lengthId).toNumber()); | 
|---|
| 487 | output += input.mid(pos, index - pos); | 
|---|
| 488 | args.clear(); | 
|---|
| 489 | for (uint j = 0; j < length; ++j) | 
|---|
| 490 | args << needle.property(j); | 
|---|
| 491 | args << QScriptValueImpl(index); | 
|---|
| 492 | args << QScriptValueImpl(eng, input); | 
|---|
| 493 | QScriptValueImpl ret = replaceValue.call(eng->nullValue(), args); | 
|---|
| 494 | output += ret.toString(); | 
|---|
| 495 | pos = index + args[0].toString().length(); | 
|---|
| 496 | } | 
|---|
| 497 | } else { | 
|---|
| 498 | // use string representation of replaceValue | 
|---|
| 499 | const QString replaceString = replaceValue.toString(); | 
|---|
| 500 | const QLatin1Char dollar = QLatin1Char('$'); | 
|---|
| 501 | QScriptNameIdImpl *indexId = eng->nameId(QLatin1String("index")); | 
|---|
| 502 | QScriptNameIdImpl *zeroId = eng->nameId(QLatin1String("0")); | 
|---|
| 503 | for (int i = 0; i < occurrences.count(); ++i) { | 
|---|
| 504 | QScriptValueImpl needle = occurrences.at(i); | 
|---|
| 505 | int index = int (needle.property(indexId).toInteger()); | 
|---|
| 506 | output += input.mid(pos, index - pos); | 
|---|
| 507 | int j = 0; | 
|---|
| 508 | while (j < replaceString.length()) { | 
|---|
| 509 | const QChar c = replaceString.at(j++); | 
|---|
| 510 | if ((c == dollar) && (j < replaceString.length())) { | 
|---|
| 511 | const QChar nc = replaceString.at(j); | 
|---|
| 512 | if (nc == dollar) { | 
|---|
| 513 | ++j; | 
|---|
| 514 | } else if (nc == QLatin1Char('`')) { | 
|---|
| 515 | ++j; | 
|---|
| 516 | output += input.left(index); | 
|---|
| 517 | continue; | 
|---|
| 518 | } else if (nc == QLatin1Char('\'')) { | 
|---|
| 519 | ++j; | 
|---|
| 520 | output += input.mid(index + needle.property(zeroId).toString().length()); | 
|---|
| 521 | continue; | 
|---|
| 522 | } else if (nc.isDigit()) { | 
|---|
| 523 | ++j; | 
|---|
| 524 | int cap = nc.toLatin1() - '0'; | 
|---|
| 525 | if ((j < replaceString.length()) && replaceString.at(j).isDigit()) { | 
|---|
| 526 | cap = cap * 10; | 
|---|
| 527 | cap = replaceString.at(j++).toLatin1() - '0'; | 
|---|
| 528 | } | 
|---|
| 529 | output += needle.property(QScriptValueImpl(cap).toString()).toString(); | 
|---|
| 530 | continue; | 
|---|
| 531 | } | 
|---|
| 532 | } | 
|---|
| 533 | output += c; | 
|---|
| 534 | } | 
|---|
| 535 | pos = index + needle.property(zeroId).toString().length(); | 
|---|
| 536 | } | 
|---|
| 537 | } | 
|---|
| 538 | output += input.mid(pos); | 
|---|
| 539 | } else { | 
|---|
| 540 | // use string representation of searchValue | 
|---|
| 541 | const QString searchString = searchValue.toString(); | 
|---|
| 542 | int pos = 0; | 
|---|
| 543 | if (replaceValue.isFunction()) { | 
|---|
| 544 | int index = input.indexOf(searchString, pos); | 
|---|
| 545 | if (index != -1) { | 
|---|
| 546 | output += input.mid(pos, index - pos); | 
|---|
| 547 | QScriptValueImplList args; | 
|---|
| 548 | args << QScriptValueImpl(eng, searchString); | 
|---|
| 549 | args << QScriptValueImpl(index); | 
|---|
| 550 | args << QScriptValueImpl(eng, input); | 
|---|
| 551 | QScriptValueImpl ret = replaceValue.call(eng->nullValue(), args); | 
|---|
| 552 | output += ret.toString(); | 
|---|
| 553 | pos = index + searchString.length(); | 
|---|
| 554 | } | 
|---|
| 555 | } else { | 
|---|
| 556 | // use string representation of replaceValue | 
|---|
| 557 | const QString replaceString = replaceValue.toString(); | 
|---|
| 558 | const QLatin1Char dollar = QLatin1Char('$'); | 
|---|
| 559 | int index = input.indexOf(searchString, pos); | 
|---|
| 560 | if (index != -1) { | 
|---|
| 561 | output += input.mid(pos, index - pos); | 
|---|
| 562 | int j = 0; | 
|---|
| 563 | while (j < replaceString.length()) { | 
|---|
| 564 | const QChar c = replaceString.at(j++); | 
|---|
| 565 | if ((c == dollar) && (j < replaceString.length())) { | 
|---|
| 566 | const QChar nc = replaceString.at(j); | 
|---|
| 567 | if (nc == dollar) { | 
|---|
| 568 | ++j; | 
|---|
| 569 | } else if (nc == QLatin1Char('`')) { | 
|---|
| 570 | output += input.left(index); | 
|---|
| 571 | ++j; | 
|---|
| 572 | continue; | 
|---|
| 573 | } else if (nc == QLatin1Char('\'')) { | 
|---|
| 574 | output += input.mid(index + searchString.length()); | 
|---|
| 575 | ++j; | 
|---|
| 576 | continue; | 
|---|
| 577 | } | 
|---|
| 578 | } | 
|---|
| 579 | output += c; | 
|---|
| 580 | } | 
|---|
| 581 | pos = index + searchString.length(); | 
|---|
| 582 | } | 
|---|
| 583 | } | 
|---|
| 584 | output += input.mid(pos); | 
|---|
| 585 | } | 
|---|
| 586 | return QScriptValueImpl(eng, output); | 
|---|
| 587 | } | 
|---|
| 588 |  | 
|---|
| 589 | QScriptValueImpl String::method_search(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 590 | { | 
|---|
| 591 | QScriptValueImpl pattern = context->argument(0); | 
|---|
| 592 |  | 
|---|
| 593 | Ecma::RegExp::Instance *rx_data = 0; | 
|---|
| 594 | if (0 == (rx_data = eng->regexpConstructor->get(pattern))) { | 
|---|
| 595 | eng->regexpConstructor->newRegExp(&pattern, context->argument(0).toString(), /*flags=*/0); | 
|---|
| 596 | rx_data = eng->regexpConstructor->get(pattern); | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | QString value = context->thisObject().toString(); | 
|---|
| 600 | #ifndef QT_NO_REGEXP | 
|---|
| 601 | return (QScriptValueImpl(value.indexOf(rx_data->value))); | 
|---|
| 602 | #else | 
|---|
| 603 | return eng->nullValue(); | 
|---|
| 604 | #endif | 
|---|
| 605 | } | 
|---|
| 606 |  | 
|---|
| 607 | QScriptValueImpl String::method_slice(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 608 | { | 
|---|
| 609 | QString text = context->thisObject().toString(); | 
|---|
| 610 | int length = text.length(); | 
|---|
| 611 |  | 
|---|
| 612 | int start = int (context->argument(0).toInteger()); | 
|---|
| 613 | int end = context->argument(1).isUndefined() | 
|---|
| 614 | ? length : int (context->argument(1).toInteger()); | 
|---|
| 615 |  | 
|---|
| 616 | if (start < 0) | 
|---|
| 617 | start = qMax(length + start, 0); | 
|---|
| 618 | else | 
|---|
| 619 | start = qMin(start, length); | 
|---|
| 620 |  | 
|---|
| 621 | if (end < 0) | 
|---|
| 622 | end = qMax(length + end, 0); | 
|---|
| 623 | else | 
|---|
| 624 | end = qMin(end, length); | 
|---|
| 625 |  | 
|---|
| 626 | int count = qMax(0, end - start); | 
|---|
| 627 | return (QScriptValueImpl(eng, text.mid(start, count))); | 
|---|
| 628 | } | 
|---|
| 629 |  | 
|---|
| 630 | QScriptValueImpl String::method_split(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 631 | { | 
|---|
| 632 | QScriptValueImpl l = context->argument(1); | 
|---|
| 633 | quint32 lim = l.isUndefined() ? UINT_MAX : QScriptEnginePrivate::toUint32(l.toNumber()); | 
|---|
| 634 |  | 
|---|
| 635 | if (lim == 0) | 
|---|
| 636 | return eng->newArray(); | 
|---|
| 637 |  | 
|---|
| 638 | QString S = context->thisObject().toString(); | 
|---|
| 639 | QScriptValueImpl separator = context->argument(0); | 
|---|
| 640 |  | 
|---|
| 641 | QScript::Array A(eng); | 
|---|
| 642 | // the argumentCount() check is for compatibility with spidermonkey; | 
|---|
| 643 | // it is not according to ECMA-262 | 
|---|
| 644 | if (separator.isUndefined() && (context->argumentCount() == 0)) { | 
|---|
| 645 | A.assign(0, QScriptValueImpl(eng, S)); | 
|---|
| 646 | } else { | 
|---|
| 647 | QStringList matches; | 
|---|
| 648 | #ifndef QT_NO_REGEXP | 
|---|
| 649 | if (Ecma::RegExp::Instance *rx = eng->regexpConstructor->get(separator)) { | 
|---|
| 650 | matches = S.split(rx->value, rx->value.pattern().isEmpty() | 
|---|
| 651 | ? QString::SkipEmptyParts : QString::KeepEmptyParts); | 
|---|
| 652 | } else | 
|---|
| 653 | #endif // QT_NO_REGEXP | 
|---|
| 654 | { | 
|---|
| 655 | QString sep = separator.toString(); | 
|---|
| 656 | matches = S.split(sep, sep.isEmpty() | 
|---|
| 657 | ? QString::SkipEmptyParts : QString::KeepEmptyParts); | 
|---|
| 658 | } | 
|---|
| 659 | uint count = qMin(lim, uint(matches.count())); | 
|---|
| 660 | for (uint i = 0; i < count; ++i) | 
|---|
| 661 | A.assign(i, QScriptValueImpl(eng, matches.at(i))); | 
|---|
| 662 | } | 
|---|
| 663 |  | 
|---|
| 664 | return eng->newArray(A); | 
|---|
| 665 | } | 
|---|
| 666 |  | 
|---|
| 667 | QScriptValueImpl String::method_substr(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 668 | { | 
|---|
| 669 | QString value = context->thisObject().toString(); | 
|---|
| 670 |  | 
|---|
| 671 | qsreal start = 0; | 
|---|
| 672 | if (context->argumentCount() > 0) | 
|---|
| 673 | start = context->argument(0).toInteger(); | 
|---|
| 674 |  | 
|---|
| 675 | qsreal length = +qInf(); | 
|---|
| 676 | if (context->argumentCount() > 1) | 
|---|
| 677 | length = context->argument(1).toInteger(); | 
|---|
| 678 |  | 
|---|
| 679 | qsreal count = value.length(); | 
|---|
| 680 | if (start < 0) | 
|---|
| 681 | start = qMax(count + start, 0.0); | 
|---|
| 682 |  | 
|---|
| 683 | length = qMin(qMax(length, 0.0), count - start); | 
|---|
| 684 |  | 
|---|
| 685 | qint32 x = QScriptEnginePrivate::toInt32(start); | 
|---|
| 686 | qint32 y = QScriptEnginePrivate::toInt32(length); | 
|---|
| 687 | return QScriptValueImpl(eng, value.mid(x, y)); | 
|---|
| 688 | } | 
|---|
| 689 |  | 
|---|
| 690 | QScriptValueImpl String::method_substring(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 691 | { | 
|---|
| 692 | QString value = context->thisObject().toString(); | 
|---|
| 693 | int length = value.length(); | 
|---|
| 694 |  | 
|---|
| 695 | qsreal start = 0; | 
|---|
| 696 | qsreal end = length; | 
|---|
| 697 |  | 
|---|
| 698 | if (context->argumentCount() > 0) | 
|---|
| 699 | start = context->argument(0).toInteger(); | 
|---|
| 700 |  | 
|---|
| 701 | if (context->argumentCount() > 1) | 
|---|
| 702 | end = context->argument(1).toInteger(); | 
|---|
| 703 |  | 
|---|
| 704 | if (qIsNaN(start) || start < 0) | 
|---|
| 705 | start = 0; | 
|---|
| 706 |  | 
|---|
| 707 | if (qIsNaN(end) || end < 0) | 
|---|
| 708 | end = 0; | 
|---|
| 709 |  | 
|---|
| 710 | if (start > length) | 
|---|
| 711 | start = length; | 
|---|
| 712 |  | 
|---|
| 713 | if (end > length) | 
|---|
| 714 | end = length; | 
|---|
| 715 |  | 
|---|
| 716 | if (start > end) { | 
|---|
| 717 | qsreal was = start; | 
|---|
| 718 | start = end; | 
|---|
| 719 | end = was; | 
|---|
| 720 | } | 
|---|
| 721 |  | 
|---|
| 722 | qint32 x = QScriptEnginePrivate::toInt32(start); | 
|---|
| 723 | qint32 y = QScriptEnginePrivate::toInt32(end - start); | 
|---|
| 724 |  | 
|---|
| 725 | return (QScriptValueImpl(eng, value.mid(x, y))); | 
|---|
| 726 | } | 
|---|
| 727 |  | 
|---|
| 728 | QScriptValueImpl String::method_toLowerCase(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 729 | { | 
|---|
| 730 | QString value = context->thisObject().toString(); | 
|---|
| 731 | return (QScriptValueImpl(eng, value.toLower())); | 
|---|
| 732 | } | 
|---|
| 733 |  | 
|---|
| 734 | QScriptValueImpl String::method_toLocaleLowerCase(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo) | 
|---|
| 735 | { | 
|---|
| 736 | return method_toLowerCase(context, eng, classInfo); // ### check me | 
|---|
| 737 | } | 
|---|
| 738 |  | 
|---|
| 739 | QScriptValueImpl String::method_toUpperCase(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 740 | { | 
|---|
| 741 | QString value = context->thisObject().toString(); | 
|---|
| 742 | return (QScriptValueImpl(eng, value.toUpper())); | 
|---|
| 743 | } | 
|---|
| 744 |  | 
|---|
| 745 | QScriptValueImpl String::method_toLocaleUpperCase(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo) | 
|---|
| 746 | { | 
|---|
| 747 | return method_toUpperCase(context, eng, classInfo); // ### check me | 
|---|
| 748 | } | 
|---|
| 749 |  | 
|---|
| 750 | QScriptValueImpl String::method_fromCharCode(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 751 | { | 
|---|
| 752 | QString str; | 
|---|
| 753 | for (int i = 0; i < context->argumentCount(); ++i) { | 
|---|
| 754 | QChar c(context->argument(i).toUInt16()); | 
|---|
| 755 | str += c; | 
|---|
| 756 | } | 
|---|
| 757 | return (QScriptValueImpl(eng, str)); | 
|---|
| 758 | } | 
|---|
| 759 |  | 
|---|
| 760 | // Qt extensions | 
|---|
| 761 |  | 
|---|
| 762 | QScriptValueImpl String::method_ext_arg(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *) | 
|---|
| 763 | { | 
|---|
| 764 | QString value = context->thisObject().toString(); | 
|---|
| 765 | QScriptValueImpl arg = context->argument(0); | 
|---|
| 766 | QString result; | 
|---|
| 767 | if (arg.isString()) | 
|---|
| 768 | result = value.arg(arg.toString()); | 
|---|
| 769 | else if (arg.isNumber()) | 
|---|
| 770 | result = value.arg(arg.toNumber()); | 
|---|
| 771 | return QScriptValueImpl(eng, result); | 
|---|
| 772 | } | 
|---|
| 773 |  | 
|---|
| 774 | } } // namespace QScript::Ecma | 
|---|
| 775 |  | 
|---|
| 776 | QT_END_NAMESPACE | 
|---|
| 777 |  | 
|---|
| 778 | #endif // QT_NO_SCRIPT | 
|---|