src/3rdparty/webkit/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp
changeset 30 5dc02b23752f
parent 0 1918ee327afb
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    25 #include "config.h"
    25 #include "config.h"
    26 #include "JSGlobalObjectFunctions.h"
    26 #include "JSGlobalObjectFunctions.h"
    27 
    27 
    28 #include "CallFrame.h"
    28 #include "CallFrame.h"
    29 #include "GlobalEvalFunction.h"
    29 #include "GlobalEvalFunction.h"
       
    30 #include "Interpreter.h"
    30 #include "JSGlobalObject.h"
    31 #include "JSGlobalObject.h"
       
    32 #include "JSString.h"
       
    33 #include "JSStringBuilder.h"
       
    34 #include "Lexer.h"
    31 #include "LiteralParser.h"
    35 #include "LiteralParser.h"
    32 #include "JSString.h"
    36 #include "Nodes.h"
    33 #include "Interpreter.h"
       
    34 #include "Parser.h"
    37 #include "Parser.h"
       
    38 #include "StringBuilder.h"
       
    39 #include "StringExtras.h"
    35 #include "dtoa.h"
    40 #include "dtoa.h"
    36 #include "Lexer.h"
       
    37 #include "Nodes.h"
       
    38 #include <stdio.h>
    41 #include <stdio.h>
    39 #include <stdlib.h>
    42 #include <stdlib.h>
    40 #include <string.h>
    43 #include <string.h>
    41 #include <wtf/ASCIICType.h>
    44 #include <wtf/ASCIICType.h>
    42 #include <wtf/Assertions.h>
    45 #include <wtf/Assertions.h>
    53     UString str = args.at(0).toString(exec);
    56     UString str = args.at(0).toString(exec);
    54     CString cstr = str.UTF8String(true);
    57     CString cstr = str.UTF8String(true);
    55     if (!cstr.c_str())
    58     if (!cstr.c_str())
    56         return throwError(exec, URIError, "String contained an illegal UTF-16 sequence.");
    59         return throwError(exec, URIError, "String contained an illegal UTF-16 sequence.");
    57 
    60 
    58     UString result = "";
    61     JSStringBuilder builder;
    59     const char* p = cstr.c_str();
    62     const char* p = cstr.c_str();
    60     for (size_t k = 0; k < cstr.size(); k++, p++) {
    63     for (size_t k = 0; k < cstr.size(); k++, p++) {
    61         char c = *p;
    64         char c = *p;
    62         if (c && strchr(doNotEscape, c))
    65         if (c && strchr(doNotEscape, c))
    63             result.append(c);
    66             builder.append(c);
    64         else {
    67         else {
    65             char tmp[4];
    68             char tmp[4];
    66             sprintf(tmp, "%%%02X", static_cast<unsigned char>(c));
    69             snprintf(tmp, 4, "%%%02X", static_cast<unsigned char>(c));
    67             result += tmp;
    70             builder.append(tmp);
    68         }
    71         }
    69     }
    72     }
    70     return jsString(exec, result);
    73     return builder.build(exec);
    71 }
    74 }
    72 
    75 
    73 static JSValue decode(ExecState* exec, const ArgList& args, const char* doNotUnescape, bool strict)
    76 static JSValue decode(ExecState* exec, const ArgList& args, const char* doNotUnescape, bool strict)
    74 {
    77 {
    75     UString result = "";
    78     JSStringBuilder builder;
    76     UString str = args.at(0).toString(exec);
    79     UString str = args.at(0).toString(exec);
    77     int k = 0;
    80     int k = 0;
    78     int len = str.size();
    81     int len = str.size();
    79     const UChar* d = str.data();
    82     const UChar* d = str.data();
    80     UChar u = 0;
    83     UChar u = 0;
   104                         const int character = decodeUTF8Sequence(sequence);
   107                         const int character = decodeUTF8Sequence(sequence);
   105                         if (character < 0 || character >= 0x110000)
   108                         if (character < 0 || character >= 0x110000)
   106                             charLen = 0;
   109                             charLen = 0;
   107                         else if (character >= 0x10000) {
   110                         else if (character >= 0x10000) {
   108                             // Convert to surrogate pair.
   111                             // Convert to surrogate pair.
   109                             result.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10)));
   112                             builder.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10)));
   110                             u = static_cast<UChar>(0xDC00 | ((character - 0x10000) & 0x3FF));
   113                             u = static_cast<UChar>(0xDC00 | ((character - 0x10000) & 0x3FF));
   111                         } else
   114                         } else
   112                             u = static_cast<UChar>(character);
   115                             u = static_cast<UChar>(character);
   113                     }
   116                     }
   114                 }
   117                 }
   129                 c = u;
   132                 c = u;
   130                 k += charLen - 1;
   133                 k += charLen - 1;
   131             }
   134             }
   132         }
   135         }
   133         k++;
   136         k++;
   134         result.append(c);
   137         builder.append(c);
   135     }
   138     }
   136     return jsString(exec, result);
   139     return builder.build(exec);
   137 }
   140 }
   138 
   141 
   139 bool isStrWhiteSpace(UChar c)
   142 bool isStrWhiteSpace(UChar c)
   140 {
   143 {
   141     switch (c) {
   144     switch (c) {
   374         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   377         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   375         "abcdefghijklmnopqrstuvwxyz"
   378         "abcdefghijklmnopqrstuvwxyz"
   376         "0123456789"
   379         "0123456789"
   377         "*+-./@_";
   380         "*+-./@_";
   378 
   381 
   379     UString result = "";
   382     JSStringBuilder builder;
   380     UString s;
       
   381     UString str = args.at(0).toString(exec);
   383     UString str = args.at(0).toString(exec);
   382     const UChar* c = str.data();
   384     const UChar* c = str.data();
   383     for (int k = 0; k < str.size(); k++, c++) {
   385     for (unsigned k = 0; k < str.size(); k++, c++) {
   384         int u = c[0];
   386         int u = c[0];
   385         if (u > 255) {
   387         if (u > 255) {
   386             char tmp[7];
   388             char tmp[7];
   387             sprintf(tmp, "%%u%04X", u);
   389             sprintf(tmp, "%%u%04X", u);
   388             s = UString(tmp);
   390             builder.append(tmp);
   389         } else if (u != 0 && strchr(do_not_escape, static_cast<char>(u)))
   391         } else if (u != 0 && strchr(do_not_escape, static_cast<char>(u)))
   390             s = UString(c, 1);
   392             builder.append(c, 1);
   391         else {
   393         else {
   392             char tmp[4];
   394             char tmp[4];
   393             sprintf(tmp, "%%%02X", u);
   395             sprintf(tmp, "%%%02X", u);
   394             s = UString(tmp);
   396             builder.append(tmp);
   395         }
   397         }
   396         result += s;
   398     }
   397     }
   399 
   398 
   400     return builder.build(exec);
   399     return jsString(exec, result);
       
   400 }
   401 }
   401 
   402 
   402 JSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec, JSObject*, JSValue, const ArgList& args)
   403 JSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec, JSObject*, JSValue, const ArgList& args)
   403 {
   404 {
   404     UString result = "";
   405     StringBuilder builder;
   405     UString str = args.at(0).toString(exec);
   406     UString str = args.at(0).toString(exec);
   406     int k = 0;
   407     int k = 0;
   407     int len = str.size();
   408     int len = str.size();
   408     while (k < len) {
   409     while (k < len) {
   409         const UChar* c = str.data() + k;
   410         const UChar* c = str.data() + k;
   418             u = UChar(Lexer::convertHex(c[1], c[2]));
   419             u = UChar(Lexer::convertHex(c[1], c[2]));
   419             c = &u;
   420             c = &u;
   420             k += 2;
   421             k += 2;
   421         }
   422         }
   422         k++;
   423         k++;
   423         result.append(*c);
   424         builder.append(*c);
   424     }
   425     }
   425 
   426 
   426     return jsString(exec, result);
   427     return jsString(exec, builder.build());
   427 }
   428 }
   428 
   429 
   429 #ifndef NDEBUG
   430 #ifndef NDEBUG
   430 JSValue JSC_HOST_CALL globalFuncJSCPrint(ExecState* exec, JSObject*, JSValue, const ArgList& args)
   431 JSValue JSC_HOST_CALL globalFuncJSCPrint(ExecState* exec, JSObject*, JSValue, const ArgList& args)
   431 {
   432 {