Changeset 49423 in webkit


Ignore:
Timestamp:
Oct 10, 2009 8:47:01 PM (15 years ago)
Author:
oliver@apple.com
Message:

Support for String.trim(), String.trimLeft() and String.trimRight() methods
https://bugs.webkit.org/show_bug.cgi?id=26590

Reviewed by Maciej Stachowiak.

Implement trim, trimLeft, and trimRight

Location:
trunk
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r49409 r49423  
     12009-10-10  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Maciej Stachowiak.
     4
     5        Support for String.trim(), String.trimLeft() and String.trimRight() methods
     6        https://bugs.webkit.org/show_bug.cgi?id=26590
     7
     8        Implement trim, trimLeft, and trimRight
     9
     10        * runtime/StringPrototype.cpp:
     11        (JSC::isTrimWhitespace):
     12           Our normal string whitespace function does not include U+200B which
     13           is needed for compatibility with mozilla's implementation of trim.
     14           U+200B does not appear to be expected according to spec, however I am
     15           choosing to be lax, and match mozilla behavior so have added this
     16           exception.
     17        (JSC::trimString):
     18
    1192009-10-09  Geoffrey Garen  <ggaren@apple.com>
    220
  • trunk/JavaScriptCore/runtime/StringPrototype.cpp

    r49371 r49423  
    2626#include "Error.h"
    2727#include "Executable.h"
     28#include "JSGlobalObjectFunctions.h"
    2829#include "JSArray.h"
    2930#include "JSFunction.h"
     
    7273static JSValue JSC_HOST_CALL stringProtoFuncAnchor(ExecState*, JSObject*, JSValue, const ArgList&);
    7374static JSValue JSC_HOST_CALL stringProtoFuncLink(ExecState*, JSObject*, JSValue, const ArgList&);
     75
     76static JSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState*, JSObject*, JSValue, const ArgList&);
     77static JSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState*, JSObject*, JSValue, const ArgList&);
     78static JSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState*, JSObject*, JSValue, const ArgList&);
    7479
    7580}
     
    118123    anchor                stringProtoFuncAnchor            DontEnum|Function       1
    119124    link                  stringProtoFuncLink              DontEnum|Function       1
     125    trim                  stringProtoFuncTrim              DontEnum|Function       0
     126    trimLeft              stringProtoFuncTrimLeft          DontEnum|Function       0
     127    trimRight             stringProtoFuncTrimRight         DontEnum|Function       0
    120128@end
    121129*/
     
    900908}
    901909
     910enum {
     911    TrimLeft = 1,
     912    TrimRight = 2
     913};
     914
     915static inline bool isTrimWhitespace(UChar c)
     916{
     917    return isStrWhiteSpace(c) || c == 0x200b;
     918}
     919
     920static inline JSValue trimString(ExecState* exec, JSValue thisValue, int trimKind)
     921{
     922    UString str = thisValue.toThisString(exec);
     923    int left = 0;
     924    if (trimKind & TrimLeft) {
     925        while (left < str.size() && isTrimWhitespace(str[left]))
     926            left++;
     927    }
     928    int right = str.size();
     929    if (trimKind & TrimRight) {
     930        while (right > left && isTrimWhitespace(str[right - 1]))
     931            right--;
     932    }
     933
     934    // Don't gc allocate a new string if we don't have to.
     935    if (left == 0 && right == str.size() && thisValue.isString())
     936        return thisValue;
     937
     938    return jsString(exec, str.substr(left, right - left));
     939}
     940
     941JSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
     942{
     943    return trimString(exec, thisValue, TrimLeft | TrimRight);
     944}
     945
     946JSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
     947{
     948    return trimString(exec, thisValue, TrimLeft);
     949}
     950
     951JSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
     952{
     953    return trimString(exec, thisValue, TrimRight);
     954}
     955   
     956   
    902957} // namespace JSC
  • trunk/LayoutTests/ChangeLog

    r49422 r49423  
     12009-10-10  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Maciej Stachowiak.
     4
     5        Support for String.trim(), String.trimLeft() and String.trimRight() methods
     6        https://bugs.webkit.org/show_bug.cgi?id=26590
     7
     8        Add tests for trim, trimLeft, and trimRight
     9
     10        * fast/js/script-tests/string-trim.js: Added.
     11        * fast/js/string-trim-expected.txt: Added.
     12        * fast/js/string-trim.html: Added.
     13
    1142009-10-10  Ryosuke Niwa  <rniwa@webkit.org>
    215
Note: See TracChangeset for help on using the changeset viewer.