pike.git / src / builtin_functions.c

version» Context lines:

pike.git/src/builtin_functions.c:2187:    *! @seealso    *! @[Charset.encoder()], @[string_to_unicode()], @[string_to_utf8()],    *! @[unicode_to_string()]    */   PMOD_EXPORT void f_utf8_to_string(INT32 args)   {    struct pike_string *in;    struct pike_string *out;    ptrdiff_t len = 0;    int shift = 0; -  ptrdiff_t i,j=0; +     INT_TYPE extended = 0;    INT32 min, max;       get_all_args("utf8_to_string", args, "%S.%i", &in, &extended);       check_string_range(in, 1, &min, &max);       if (min >= 0 && max <= 0x7f) {    /* 7bit string -- already valid utf8. */    pop_n_elems(args - 1);
pike.git/src/builtin_functions.c:2214:    /* 7bit in == 7bit out */    pop_n_elems(args-1);    return;    }       out = begin_wide_shared_string(len, shift);       switch (shift) {    case 0: {    p_wchar0 *out_str = STR0 (out); -  for(i=0; i < in->len;) { -  unsigned int c = STR0(in)[i++]; +  const p_wchar0 *in_str = STR0(in); +  ptrdiff_t len = out->len; +  +  for(ptrdiff_t j=0; j < len; j++) { +  unsigned int c = *(in_str++);    /* NOTE: No tests here since we've already tested the string above. */    if (c & 0x80) {    /* 11bit */ -  unsigned int c2 = STR0(in)[i++] & 0x3f; +  unsigned int c2 = *(in_str++) & 0x3f;    c &= 0x1f;    c = (c << 6) | c2;    } -  out_str[j++] = c; +  out_str[j] = c;    }    break;    }       case 1: {    p_wchar1 *out_str = STR1 (out); -  for(i=0; i < in->len;) { -  unsigned int c = STR0(in)[i++]; +  const p_wchar0 *in_str = STR0(in); +  ptrdiff_t len = out->len; +  +  for(ptrdiff_t j=0; j < len; j++) { +  unsigned int c = *(in_str++);    /* NOTE: No tests here since we've already tested the string above. */    if (c & 0x80) {    if ((c & 0xe0) == 0xc0) {    /* 11bit */ -  unsigned int c2 = STR0(in)[i++] & 0x3f; +  unsigned int c2 = *(in_str++) & 0x3f;    c &= 0x1f;    c = (c << 6) | c2;    } else {    /* 16bit */ -  unsigned int c2 = STR0(in)[i++] & 0x3f; -  unsigned int c3 = STR0(in)[i++] & 0x3f; +  unsigned int c2 = *(in_str++) & 0x3f; +  unsigned int c3 = *(in_str++) & 0x3f;    c &= 0x0f;    c = (c << 12) | (c2 << 6) | c3;    }    } -  out_str[j++] = c; +  out_str[j] = c;    }    break;    }       case 2: {    p_wchar2 *out_str = STR2 (out); -  for(i=0; i < in->len;) { -  unsigned int c = STR0(in)[i++]; +  const p_wchar0 *in_str = STR0(in); +  ptrdiff_t len = out->len; +  +  for(ptrdiff_t j=0; j < len; j++) { +  unsigned int c = *(in_str++);    /* NOTE: No tests here since we've already tested the string above. */    if (c & 0x80) {    int cont = 0;    if ((c & 0xe0) == 0xc0) {    /* 11bit */    cont = 1;    c &= 0x1f;    } else if ((c & 0xf0) == 0xe0) {    /* 16bit */    cont = 2;
pike.git/src/builtin_functions.c:2285:    } else if ((c & 0xfe) == 0xfc) {    /* 31bit */    cont = 5;    c &= 0x01;    } else {    /* 36bit */    cont = 6;    c = 0;    }    while(cont--) { -  unsigned int c2 = STR0(in)[i++] & 0x3f; +  unsigned int c2 = *(in_str++) & 0x3f;    c = (c << 6) | c2;    }    if ((extended & 2) && (c & 0xfc00) == 0xdc00) {    /* Low surrogate */    c &= 0x3ff;    c |= ((out_str[--j] & 0x3ff)<<10) + 0x10000;    }    } -  out_str[j++] = c; +  out_str[j] = c;    }    break;    }    }      #ifdef PIKE_DEBUG    if (j != len) {    Pike_fatal("Calculated and actual lengths differ: "    "%"PRINTPTRDIFFT"d != %"PRINTPTRDIFFT"d\n",    len, j);