1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/templates/native/str.c Fri Dec 09 16:22:37 2016 +0100
1.3 @@ -0,0 +1,134 @@
1.4 +/* Native functions for string operations.
1.5 +
1.6 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
1.7 +
1.8 +This program is free software; you can redistribute it and/or modify it under
1.9 +the terms of the GNU General Public License as published by the Free Software
1.10 +Foundation; either version 3 of the License, or (at your option) any later
1.11 +version.
1.12 +
1.13 +This program is distributed in the hope that it will be useful, but WITHOUT
1.14 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.15 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.16 +details.
1.17 +
1.18 +You should have received a copy of the GNU General Public License along with
1.19 +this program. If not, see <http://www.gnu.org/licenses/>.
1.20 +*/
1.21 +
1.22 +#include <string.h> /* strcmp, strncpy, strlen */
1.23 +#include "native/common.h"
1.24 +#include "types.h"
1.25 +#include "exceptions.h"
1.26 +#include "ops.h"
1.27 +#include "progconsts.h"
1.28 +#include "progops.h"
1.29 +#include "progtypes.h"
1.30 +#include "main.h"
1.31 +
1.32 +/* String operations. */
1.33 +
1.34 +__attr __fn_native_str_str_add(__attr __args[])
1.35 +{
1.36 + __attr * const _data = &__args[1];
1.37 + __attr * const other = &__args[2];
1.38 + /* _data, other interpreted as string */
1.39 + char *s = _data->strvalue;
1.40 + char *o = other->strvalue;
1.41 + int n = strlen(s) + strlen(o) + 1;
1.42 + char *r = (char *) __ALLOCATE(n, sizeof(char));
1.43 +
1.44 + strncpy(r, s, n);
1.45 + strncpy(r + strlen(s), o, n - strlen(s)); /* should null terminate */
1.46 +
1.47 + /* Return a new string. */
1.48 + return __new_str(r);
1.49 +}
1.50 +
1.51 +__attr __fn_native_str_str_lt(__attr __args[])
1.52 +{
1.53 + __attr * const _data = &__args[1];
1.54 + __attr * const other = &__args[2];
1.55 + /* _data, other interpreted as string */
1.56 + char *s = _data->strvalue;
1.57 + char *o = other->strvalue;
1.58 +
1.59 + /* NOTE: Using simple byte-level string operations. */
1.60 + return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False;
1.61 +}
1.62 +
1.63 +__attr __fn_native_str_str_gt(__attr __args[])
1.64 +{
1.65 + __attr * const _data = &__args[1];
1.66 + __attr * const other = &__args[2];
1.67 + /* _data, other interpreted as string */
1.68 + char *s = _data->strvalue;
1.69 + char *o = other->strvalue;
1.70 +
1.71 + /* NOTE: Using simple byte-level string operations. */
1.72 + return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False;
1.73 +}
1.74 +
1.75 +__attr __fn_native_str_str_eq(__attr __args[])
1.76 +{
1.77 + __attr * const _data = &__args[1];
1.78 + __attr * const other = &__args[2];
1.79 + /* _data, other interpreted as string */
1.80 + char *s = _data->strvalue;
1.81 + char *o = other->strvalue;
1.82 +
1.83 + /* NOTE: Using simple byte-level string operations. */
1.84 + return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False;
1.85 +}
1.86 +
1.87 +__attr __fn_native_str_str_len(__attr __args[])
1.88 +{
1.89 + __attr * const _data = &__args[1];
1.90 + /* _data interpreted as string */
1.91 + char *s = _data->strvalue;
1.92 +
1.93 + /* Return the new integer. */
1.94 + return __new_int(strlen(s));
1.95 +}
1.96 +
1.97 +__attr __fn_native_str_str_nonempty(__attr __args[])
1.98 +{
1.99 + __attr * const _data = &__args[1];
1.100 + /* _data interpreted as string */
1.101 + char *s = _data->strvalue;
1.102 +
1.103 + return strlen(s) ? __builtins___boolean_True : __builtins___boolean_False;
1.104 +}
1.105 +
1.106 +__attr __fn_native_str_str_ord(__attr __args[])
1.107 +{
1.108 + __attr * const _data = &__args[1];
1.109 + /* _data interpreted as string */
1.110 + char *s = _data->strvalue;
1.111 +
1.112 + return __new_int((unsigned int) s[0]);
1.113 +}
1.114 +
1.115 +__attr __fn_native_str_str_substr(__attr __args[])
1.116 +{
1.117 + __attr * const _data = &__args[1];
1.118 + __attr * const start = &__args[2];
1.119 + __attr * const size = &__args[3];
1.120 + /* _data interpreted as string */
1.121 + char *s = _data->strvalue, *sub;
1.122 + /* start.__data__ interpreted as int */
1.123 + int i = __load_via_object(start->value, __pos___data__).intvalue;
1.124 + /* size.__data__ interpreted as int */
1.125 + int l = __load_via_object(size->value, __pos___data__).intvalue;
1.126 +
1.127 + /* Reserve space for a new string. */
1.128 + sub = (char *) __ALLOCATE(l + 1, sizeof(char));
1.129 + strncpy(sub, s + i, l); /* does not null terminate but final byte should be zero */
1.130 + return __new_str(sub);
1.131 +}
1.132 +
1.133 +/* Module initialisation. */
1.134 +
1.135 +void __main_native_str()
1.136 +{
1.137 +}