1.1 --- a/templates/native/str.c Mon Dec 12 01:46:28 2016 +0100
1.2 +++ b/templates/native/str.c Mon Dec 12 17:05:59 2016 +0100
1.3 @@ -113,18 +113,33 @@
1.4 {
1.5 __attr * const _data = &__args[1];
1.6 __attr * const start = &__args[2];
1.7 - __attr * const size = &__args[3];
1.8 + __attr * const end = &__args[3];
1.9 + __attr * const step = &__args[4];
1.10 /* _data interpreted as string */
1.11 char *s = _data->strvalue, *sub;
1.12 /* start.__data__ interpreted as int */
1.13 - int i = __load_via_object(start->value, __pos___data__).intvalue;
1.14 - /* size.__data__ interpreted as int */
1.15 - int l = __load_via_object(size->value, __pos___data__).intvalue;
1.16 + int istart = __load_via_object(start->value, __pos___data__).intvalue;
1.17 + /* end.__data__ interpreted as int */
1.18 + int iend = __load_via_object(end->value, __pos___data__).intvalue;
1.19 + /* step.__data__ interpreted as int */
1.20 + int istep = __load_via_object(step->value, __pos___data__).intvalue;
1.21 +
1.22 + /* Calculate the size of the substring. */
1.23 + size_t resultsize = ((iend - istart - 1) / istep) + 1;
1.24 + int to, from;
1.25
1.26 /* Reserve space for a new string. */
1.27 - sub = (char *) __ALLOCATE(l + 1, sizeof(char));
1.28 - memcpy(sub, s + i, l); /* does not null terminate but final byte should be zero */
1.29 - return __new_str(sub, l);
1.30 + sub = (char *) __ALLOCATE(resultsize + 1, sizeof(char));
1.31 +
1.32 + /* Does not null terminate but final byte should be zero. */
1.33 + if (istep > 0)
1.34 + for (from = istart, to = 0; from < iend; from += istep, to++)
1.35 + sub[to] = s[from];
1.36 + else if (istep < 0)
1.37 + for (from = istart, to = 0; from > iend; from += istep, to++)
1.38 + sub[to] = s[from];
1.39 +
1.40 + return __new_str(sub, resultsize);
1.41 }
1.42
1.43 /* Module initialisation. */