Show
Ignore:
Timestamp:
09/04/08 11:39:51 (6 years ago)
Author:
mattn
Message:

output utf-8 string.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/cplusplus/fcgi-v8/trunk/core.cc

    r18794 r18804  
    22#include <cstdio> 
    33#include <cstdlib> 
     4#include <memory.h> 
    45#include "v8-util.h" 
    56 
     
    78extern char ** __argv; 
    89 
     10int utf_char2bytes(int c, unsigned char* buf) { 
     11    if (c < 0x80) { 
     12        buf[0] = c; 
     13        return 1; 
     14    } 
     15    if (c < 0x800) { 
     16        buf[0] = 0xc0 + ((unsigned)c >> 6); 
     17        buf[1] = 0x80 + (c & 0x3f); 
     18        return 2; 
     19    } 
     20    if (c < 0x10000) { 
     21        buf[0] = 0xe0 + ((unsigned)c >> 12); 
     22        buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f); 
     23        buf[2] = 0x80 + (c & 0x3f); 
     24        return 3; 
     25    } 
     26    if (c < 0x200000) { 
     27        buf[0] = 0xf0 + ((unsigned)c >> 18); 
     28        buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f); 
     29        buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f); 
     30        buf[3] = 0x80 + (c & 0x3f); 
     31        return 4; 
     32    } 
     33    if (c < 0x4000000) { 
     34        buf[0] = 0xf8 + ((unsigned)c >> 24); 
     35        buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f); 
     36        buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f); 
     37        buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f); 
     38        buf[4] = 0x80 + (c & 0x3f); 
     39        return 5; 
     40    } 
     41    /* 31 bits */ 
     42    buf[0] = 0xfc + ((unsigned)c >> 30); 
     43    buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f); 
     44    buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f); 
     45    buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f); 
     46    buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f); 
     47    buf[5] = 0x80 + (c & 0x3f); 
     48    return 6; 
     49} 
     50 
     51unsigned char* ucs2utf_alloc(uint16_t *instr) { 
     52    uint16_t *p = instr; 
     53    int inlen = 0; 
     54 
     55    while(*p++) inlen++; 
     56    unsigned char* outstr = new unsigned char[inlen * 8]; 
     57    unsigned char* outp = outstr; 
     58    memset(outstr, 0, inlen * 8); 
     59 
     60    p = instr; 
     61    while (*p) outstr += utf_char2bytes(*p++, outstr); 
     62    return outp; 
     63} 
     64 
    965v8::Handle<v8::Value> P(const v8::Arguments& args) { 
    1066    for (int i = 0; i < args.Length(); i++) { 
    1167        v8::HandleScope handle_scope; 
    12         v8::String::AsciiValue str(args[i]); 
    13         printf("%s", *str); 
     68        v8::String::Value val(args[i]); 
     69        unsigned char* buf = ucs2utf_alloc(*val); 
     70        printf("%s", buf); 
     71        delete[] buf; 
    1472    } 
    1573    printf("\n"); 
     
    2078    for (int i = 0; i < args.Length(); i++) { 
    2179        v8::HandleScope handle_scope; 
    22         v8::String::AsciiValue str(args[i]); 
    23         printf("%s", *str); 
     80        v8::String::Value val(args[i]); 
     81        unsigned char* buf = ucs2utf_alloc(*val); 
     82        printf("%s", buf); 
     83        delete[] buf; 
    2484    } 
    2585    printf("\n"); 
     
    3090    for (int i = 0; i < args.Length(); i++) { 
    3191        v8::HandleScope handle_scope; 
    32         v8::String::AsciiValue str(args[i]); 
    33         printf("%s\n", *str); 
     92        v8::String::Value val(args[i]); 
     93        unsigned char* buf = ucs2utf_alloc(*val); 
     94        printf("%s", buf); 
     95        delete[] buf; 
    3496    } 
    3597    return v8::Undefined();