Branch: Tag:

2011-01-17

2011-01-17 20:25:42 by Henrik Grubbström (Grubba) <grubba@grubba.org>

String.Buffer: Added String.Buffer()->addat().

2340:       /*! @decl int add(string ... data)    *! -  *! Adds @[data] to the buffer. Returns the size of the buffer. +  *! Adds @[data] to the buffer.    *! -  +  *! @returns +  *! Returns the size of the buffer. +  *! +  *! @seealso +  *! @[addat()]    */    PIKEFUN int add( string ... arg1 )    {
2389:    }    }    +  /*! @decl int addat(int(0..) pos, string ... data) +  *! +  *! Adds @[data] to the buffer, starting at position @[pos]. +  *! +  *! @returns +  *! Returns the size of the buffer. +  *! +  *! @note +  *! If the buffer isn't of the required size, it is padded +  *! with NUL-characters. +  *! +  *! @seealso +  *! @[add()] +  */ +  PIKEFUN int addat(int(0..) pos, string ... arg1 ) +  { +  struct Buffer_struct *str = THIS; +  +  if (pos < 0) +  SIMPLE_BAD_ARG_ERROR("addat", 1, "int(0..)"); +  +  if (args) { +  int init_from_arg0 = 0, j; +  ptrdiff_t sum = 0; +  int shift = 0; +  for (j=1; j < args; j++) { +  struct pike_string *a = Pike_sp[j-args].u.string; +  sum += a->len; +  shift |= a->size_shift; +  } +  +  if (!str->str.s) { +  if ((sum + pos) <= str->initial) { +  sum = str->initial; +  } else { +  sum <<= 1; +  sum += pos; +  } +  shift = shift & ~(shift >> 1); +  +  init_string_builder_alloc(&str->str, sum, shift); +  } else { +  sum += pos; +  shift |= str->str.known_shift; +  shift = shift & ~(shift >> 1); +  if (sum > str->str.s->len) { +  string_build_mkspace(&str->str, sum - str->str.s->len, shift); +  } else if (shift != str->str.known_shift) { +  string_build_mkspace(&str->str, 0, shift); +  } +  } +  /* We know it will be a string that really is this wide. */ +  str->str.known_shift = shift; +  +  if (str->str.s->len < pos) { +  /* Clear the padding. */ +  MEMSET(str->str.s->str + (str->str.s->len << str->str.s->size_shift), +  0, (pos - str->str.s->len) << str->str.s->size_shift); +  } +  +  for(j = 1; j<args; j++) { +  struct pike_string *a = Pike_sp[j-args].u.string; +  pike_string_cpy(MKPCHARP_STR_OFF(str->str.s, pos), a); +  pos += a->len; +  } +  +  if (str->str.s->len < pos) { +  str->str.s->len = pos; +  /* Ensure NUL-termination */ +  str->str.s->str[str->str.s->len << str->str.s->size_shift] = 0; +  } +  } +  +  if (str->str.s) { +  RETURN str->str.s->len; +  } else { +  RETURN 0; +  } +  } +     /*! @decl void putchar(int c)    *! Appends the character @[c] at the end of the string.    */