1.1 --- a/lib/__builtins__/buffer.py Tue Nov 22 17:36:39 2016 +0100
1.2 +++ b/lib/__builtins__/buffer.py Tue Nov 22 22:44:30 2016 +0100
1.3 @@ -25,11 +25,18 @@
1.4
1.5 "A buffer, used to build strings."
1.6
1.7 - def __init__(self, args=None, size=None):
1.8 + def __init__(self, args=None, size=0):
1.9
1.10 "Initialise a buffer from the given 'args' or the given 'size'."
1.11
1.12 - self.__data__ = _list_init(len(args) or size or 0)
1.13 + if args is not None:
1.14 + n = len(args)
1.15 + elif isinstance(size, int):
1.16 + n = size
1.17 + else:
1.18 + raise TypeError(size)
1.19 +
1.20 + self.__data__ = _list_init(n)
1.21
1.22 # Append all arguments in string form to the buffer.
1.23
2.1 --- a/templates/native.c Tue Nov 22 17:36:39 2016 +0100
2.2 +++ b/templates/native.c Tue Nov 22 22:44:30 2016 +0100
2.3 @@ -1,7 +1,7 @@
2.4 #include <stdlib.h> /* calloc, exit, realloc */
2.5 #include <unistd.h> /* read, write */
2.6 #include <math.h> /* ceil, log10, pow */
2.7 -#include <string.h> /* strcmp, strlen */
2.8 +#include <string.h> /* strcmp, strncpy, strlen */
2.9 #include <stdio.h> /* snprintf */
2.10 #include "types.h"
2.11 #include "exceptions.h"
2.12 @@ -362,7 +362,7 @@
2.13 if (size >= capacity)
2.14 {
2.15 /* NOTE: Consider various restrictions on capacity increases. */
2.16 - n = data->capacity * 2;
2.17 + n = capacity ? capacity * 2 : 1;
2.18 data = realloc(data, __FRAGMENT_SIZE(n));
2.19 data->capacity = n;
2.20 }
2.21 @@ -441,8 +441,8 @@
2.22 __attr * const self = &__args[1];
2.23 /* self.__data__ interpreted as buffer */
2.24 __fragment *data = __load_via_object(self->value, __pos___data__).data;
2.25 - unsigned int size = 0, i, j;
2.26 - char *s;
2.27 + unsigned int size = 0, i, j, n;
2.28 + char *s, *o;
2.29
2.30 /* Calculate the size of the string. */
2.31 for (i = 0; i < data->size; i++)
2.32 @@ -452,8 +452,13 @@
2.33 s = calloc(size + 1, sizeof(char));
2.34
2.35 /* Build a single string from the buffer contents. */
2.36 - for (i = 0, j = 0; i < data->size; j += strlen(data->attrs[i].strvalue), i++)
2.37 - strcpy(s + j, data->attrs[i].strvalue);
2.38 + for (i = 0, j = 0; i < data->size; i++)
2.39 + {
2.40 + o = __load_via_object(data->attrs[i].value, __pos___data__).strvalue;
2.41 + n = strlen(o);
2.42 + strncpy(s + j, o, n);
2.43 + j += n;
2.44 + }
2.45
2.46 /* Return a new string. */
2.47 return __new_str(s);