1.1 --- a/templates/native/str.c Mon Dec 12 00:33:33 2016 +0100
1.2 +++ b/templates/native/str.c Mon Dec 12 00:37:28 2016 +0100
1.3 @@ -16,7 +16,7 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 */
1.6
1.7 -#include <string.h> /* strcmp, strncpy, strlen */
1.8 +#include <string.h> /* strcmp, memcpy */
1.9 #include "native/common.h"
1.10 #include "types.h"
1.11 #include "exceptions.h"
1.12 @@ -35,14 +35,14 @@
1.13 /* _data, other interpreted as string */
1.14 char *s = _data->strvalue;
1.15 char *o = other->strvalue;
1.16 - int n = strlen(s) + strlen(o) + 1;
1.17 - char *r = (char *) __ALLOCATE(n, sizeof(char));
1.18 + int n = _data->size + other->size;
1.19 + char *r = (char *) __ALLOCATE(n + 1, sizeof(char));
1.20
1.21 - strncpy(r, s, n);
1.22 - strncpy(r + strlen(s), o, n - strlen(s)); /* should null terminate */
1.23 + memcpy(r, s, _data->size);
1.24 + memcpy(r + _data->size, o, other->size);
1.25
1.26 /* Return a new string. */
1.27 - return __new_str(r);
1.28 + return __new_str(r, n);
1.29 }
1.30
1.31 __attr __fn_native_str_str_lt(__attr __args[])
1.32 @@ -88,7 +88,7 @@
1.33 char *s = _data->strvalue;
1.34
1.35 /* Return the new integer. */
1.36 - return __new_int(strlen(s));
1.37 + return __new_int(_data->size);
1.38 }
1.39
1.40 __attr __fn_native_str_str_nonempty(__attr __args[])
1.41 @@ -97,7 +97,7 @@
1.42 /* _data interpreted as string */
1.43 char *s = _data->strvalue;
1.44
1.45 - return strlen(s) ? __builtins___boolean_True : __builtins___boolean_False;
1.46 + return _data->size ? __builtins___boolean_True : __builtins___boolean_False;
1.47 }
1.48
1.49 __attr __fn_native_str_str_ord(__attr __args[])
1.50 @@ -123,8 +123,8 @@
1.51
1.52 /* Reserve space for a new string. */
1.53 sub = (char *) __ALLOCATE(l + 1, sizeof(char));
1.54 - strncpy(sub, s + i, l); /* does not null terminate but final byte should be zero */
1.55 - return __new_str(sub);
1.56 + memcpy(sub, s + i, l); /* does not null terminate but final byte should be zero */
1.57 + return __new_str(sub, l);
1.58 }
1.59
1.60 /* Module initialisation. */