pike.git / src / stralloc.c

version» Context lines:

pike.git/src/stralloc.c:1645:    return a->len - b->len;    }    }   }      struct pike_string *realloc_unlinked_string(struct pike_string *a,    ptrdiff_t size)   {    struct pike_string *r = NULL;    -  if (a->flags & STRING_IS_SHORT ) -  { -  if (size <= SHORT_STRING_THRESHOLD/(1<<a->size_shift)) { -  /* There's already space enough. */ -  a->len = size; -  low_set_index(a, size, 0); -  return a; -  } -  } else { +  if (a->flags & STRING_IS_SHORT ) { +  if (size <= SHORT_STRING_THRESHOLD/(1<<a->size_shift)) +  r = a; /* There's already space enough. */ +  } else    r=realloc(a, sizeof(struct pike_string_hdr)+((size+1)<<a->size_shift)); -  } +     -  if(!r) -  { +  if(!r) {    r=begin_wide_shared_string(size, a->size_shift);    r->flags |= a->flags & ~15;    r->min = a->min;    r->max = a->max; -  if (a->len <= size) { -  memcpy(r->str, a->str, a->len<<a->size_shift); -  } else { -  memcpy(r->str, a->str, size<<a->size_shift); -  } +  memcpy(r->str, a->str, (a->len <= size ? a->len : size)<<a->size_shift);    free_string(a);    }       r->len=size; -  low_set_index(r,size,0); +  low_set_index(r,size,0); /* Enforce \0 termination */    return r;   }      /* Returns an unlinked string ready for end_shared_string */   static struct pike_string *realloc_shared_string(struct pike_string *a,    ptrdiff_t size)   {    struct pike_string *r;    if(a->refs==1)    {
pike.git/src/stralloc.c:2379:   {    ptrdiff_t i;    int mag = min_magnitude(ch);       string_build_mkspace(s, 1, mag);    if (mag > s->known_shift) {    s->known_shift = mag;    }    i = s->s->len++;    low_set_index(s->s,i,ch); -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }      PMOD_EXPORT void string_builder_putchars(struct string_builder *s, int ch,    ptrdiff_t count)   {    ptrdiff_t len = s->s->len;    int mag = min_magnitude(ch);       /* This is not really expected to happen. But since we are doing    * memset here, a negative argument should be avoided. */
pike.git/src/stralloc.c:2419:    }    case 2: {    int i;    for (i = 0; i < count; i++)    (STR2 (s->s) + s->s->len)[i] = ch;    break;    }    }       s->s->len += count; -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }         PMOD_EXPORT void string_builder_binary_strcat0(struct string_builder *s,    const p_wchar0 *str, ptrdiff_t len)   {    string_build_mkspace(s,len,0);    switch(s->s->size_shift)    {    case 0: convert_0_to_0(STR0(s->s)+s->s->len,str,len); break;    case 1: convert_0_to_1(STR1(s->s)+s->s->len,str,len); break;    case 2: convert_0_to_2(STR2(s->s)+s->s->len,str,len); break;   #ifdef PIKE_DEBUG    default:    Pike_fatal ("Illegal magnitude! (%d)\n", s->s->size_shift);   #endif    }    s->s->len+=len; -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }      PMOD_EXPORT void string_builder_binary_strcat1(struct string_builder *s,    const p_wchar1 *str, ptrdiff_t len)   {    if (s->s->size_shift == 0) {    if (find_magnitude1 (str, len) == 0) {    string_build_mkspace (s, len, 0);    convert_1_to_0 (STR0(s->s) + s->s->len, str, len);    s->s->len += len; -  /* Ensure NUL-termination */ -  s->s->str[s->s->len] = 0; +     return;    }    s->known_shift = 1;    }       string_build_mkspace (s, len, 1);    if (s->s->size_shift == 1)    convert_1_to_1 (STR1(s->s)+s->s->len, str, len);    else {   #ifdef PIKE_DEBUG    if (s->s->size_shift != 2)    Pike_fatal ("I aint got no clue 'bout nothing, dude. (%d)\n",    s->s->size_shift);   #endif    convert_1_to_2 (STR2(s->s)+s->s->len, str, len);    }    s->s->len += len; -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }      PMOD_EXPORT void string_builder_binary_strcat2(struct string_builder *s,    const p_wchar2 *str, ptrdiff_t len)   {    if (s->s->size_shift < 2) {    int shift = find_magnitude2 (str, len);       if (shift > s->s->size_shift) {    string_build_mkspace (s, len, shift);
pike.git/src/stralloc.c:2513:    }    }    }       else {    string_build_mkspace (s, len, 2);    convert_2_to_2 (STR2(s->s) + s->s->len, str, len);    }       s->s->len += len; -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }      PMOD_EXPORT void string_builder_append(struct string_builder *s,    PCHARP from,    ptrdiff_t len)   {    int shift = from.shift;    if (shift > s->s->size_shift) {    if (shift == 1) {    shift = find_magnitude1((p_wchar1 *)from.ptr, len);    } else {    shift = find_magnitude2((p_wchar2 *)from.ptr, len);    }    if (shift > s->known_shift)    s->known_shift = shift;    }    string_build_mkspace(s, len, shift);    generic_memcpy(MKPCHARP_STR_OFF(s->s,s->s->len), from, len);    s->s->len+=len; -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }      PMOD_EXPORT void string_builder_fill(struct string_builder *s,    ptrdiff_t howmany,    PCHARP from,    ptrdiff_t len,    ptrdiff_t offset)   {    ptrdiff_t tmp;    int shift;
pike.git/src/stralloc.c:2560:   #endif    if(howmany<=0) return;       if(!s->s->size_shift &&    len == 1 &&    (!from.shift || !min_magnitude(EXTRACT_PCHARP(from))))    {    memset(string_builder_allocate(s,howmany,0),    EXTRACT_PCHARP(from),    howmany); -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +     return;    }       if ((shift = from.shift) > s->s->size_shift) {    /* Check if we really need the extra magnitude. */    /* FIXME: What about offset? */    if (shift == 1) {    shift = find_magnitude1((p_wchar1 *)from.ptr, len);    } else {    shift = find_magnitude2((p_wchar2 *)from.ptr, len);
pike.git/src/stralloc.c:2603:    {    tmp = MINIMUM(len, howmany);    memcpy(s->s->str + (s->s->len << s->s->size_shift),    to.ptr,    tmp << s->s->size_shift);    len+=tmp;    howmany-=tmp;    s->s->len+=tmp;    }    } -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }      /* Append a NUL-terminated UTF16 string possibly containing surrogates. */   PMOD_EXPORT void string_builder_utf16_strcat(struct string_builder *s,    const p_wchar1 *utf16str)   {    p_wchar1 uc;    while ((uc = *(utf16str++))) {    if ((uc & 0xf800) == 0xd800) {    /* Surrogate. */
pike.git/src/stralloc.c:2647:    string_builder_binary_strcat(s,str,strlen(str));   }      PMOD_EXPORT void string_builder_shared_strcat(struct string_builder *s, struct pike_string *str)   {    string_build_mkspace(s,str->len,str->size_shift);       pike_string_cpy(MKPCHARP_STR_OFF(s->s,s->s->len), str);    s->known_shift=MAXIMUM(s->known_shift,str->size_shift);    s->s->len+=str->len; -  /* Ensure NUL-termination */ -  s->s->str[s->s->len << s->s->size_shift] = 0; +    }      PMOD_EXPORT ptrdiff_t string_builder_quote_string(struct string_builder *buf,    struct pike_string *str,    ptrdiff_t i,    ptrdiff_t max_len,    int flags)   {    ptrdiff_t old_len = buf->s->len;   
pike.git/src/stralloc.c:3157:    va_start(args, fmt);    string_builder_vsprintf(s, fmt, args);    va_end(args);   }         PMOD_EXPORT void reset_string_builder(struct string_builder *s)   {    s->known_shift=0;    s->s->len=0; -  /* Ensure NUL-termination */ -  low_set_index (s->s, 0, 0); +    }      PMOD_EXPORT void free_string_builder(struct string_builder *s)   {    s->s->len = s->malloced;    free_string(s->s);   }      PMOD_EXPORT struct pike_string *finish_string_builder(struct string_builder *s)   {    ptrdiff_t len = s->s->len;    if (len != s->malloced)    s->s = realloc_unlinked_string(s->s, s->malloced = len); -  else -  /* Ensure NUL-termination */ -  low_set_index(s->s,s->s->len,0); +     if(s->known_shift == s->s->size_shift)    return low_end_shared_string(s->s);    return end_shared_string(s->s);   }      PMOD_EXPORT PCHARP MEMCHR_PCHARP(PCHARP ptr, int chr, ptrdiff_t len)   {    switch(ptr.shift)    {    case 0: return MKPCHARP(memchr(ptr.ptr,chr,len),0);