paul@354 | 1 | /* Native functions for string operations. |
paul@354 | 2 | |
paul@531 | 3 | Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@354 | 4 | |
paul@354 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@354 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@354 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@354 | 8 | version. |
paul@354 | 9 | |
paul@354 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@354 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@354 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@354 | 13 | details. |
paul@354 | 14 | |
paul@354 | 15 | You should have received a copy of the GNU General Public License along with |
paul@354 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@354 | 17 | */ |
paul@354 | 18 | |
paul@378 | 19 | #include <string.h> /* strcmp, memcpy */ |
paul@354 | 20 | #include "native/common.h" |
paul@354 | 21 | #include "types.h" |
paul@354 | 22 | #include "exceptions.h" |
paul@354 | 23 | #include "ops.h" |
paul@354 | 24 | #include "progconsts.h" |
paul@354 | 25 | #include "progops.h" |
paul@354 | 26 | #include "progtypes.h" |
paul@354 | 27 | #include "main.h" |
paul@354 | 28 | |
paul@354 | 29 | /* String operations. */ |
paul@354 | 30 | |
paul@354 | 31 | __attr __fn_native_str_str_add(__attr __args[]) |
paul@354 | 32 | { |
paul@354 | 33 | __attr * const _data = &__args[1]; |
paul@354 | 34 | __attr * const other = &__args[2]; |
paul@354 | 35 | /* _data, other interpreted as string */ |
paul@354 | 36 | char *s = _data->strvalue; |
paul@354 | 37 | char *o = other->strvalue; |
paul@378 | 38 | int n = _data->size + other->size; |
paul@378 | 39 | char *r = (char *) __ALLOCATE(n + 1, sizeof(char)); |
paul@354 | 40 | |
paul@378 | 41 | memcpy(r, s, _data->size); |
paul@378 | 42 | memcpy(r + _data->size, o, other->size); |
paul@354 | 43 | |
paul@354 | 44 | /* Return a new string. */ |
paul@378 | 45 | return __new_str(r, n); |
paul@354 | 46 | } |
paul@354 | 47 | |
paul@531 | 48 | __attr __fn_native_str_str_chr(__attr __args[]) |
paul@531 | 49 | { |
paul@531 | 50 | __attr * const _data = &__args[1]; |
paul@531 | 51 | /* _data interpreted as int */ |
paul@531 | 52 | int n = _data->intvalue; |
paul@531 | 53 | char *s = (char *) __ALLOCATE(2, sizeof(char)); |
paul@531 | 54 | |
paul@531 | 55 | s[0] = (char) n; |
paul@531 | 56 | return __new_str(s, 1); |
paul@531 | 57 | } |
paul@531 | 58 | |
paul@354 | 59 | __attr __fn_native_str_str_lt(__attr __args[]) |
paul@354 | 60 | { |
paul@354 | 61 | __attr * const _data = &__args[1]; |
paul@354 | 62 | __attr * const other = &__args[2]; |
paul@354 | 63 | /* _data, other interpreted as string */ |
paul@354 | 64 | char *s = _data->strvalue; |
paul@354 | 65 | char *o = other->strvalue; |
paul@354 | 66 | |
paul@354 | 67 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 68 | return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 69 | } |
paul@354 | 70 | |
paul@354 | 71 | __attr __fn_native_str_str_gt(__attr __args[]) |
paul@354 | 72 | { |
paul@354 | 73 | __attr * const _data = &__args[1]; |
paul@354 | 74 | __attr * const other = &__args[2]; |
paul@354 | 75 | /* _data, other interpreted as string */ |
paul@354 | 76 | char *s = _data->strvalue; |
paul@354 | 77 | char *o = other->strvalue; |
paul@354 | 78 | |
paul@354 | 79 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 80 | return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 81 | } |
paul@354 | 82 | |
paul@354 | 83 | __attr __fn_native_str_str_eq(__attr __args[]) |
paul@354 | 84 | { |
paul@354 | 85 | __attr * const _data = &__args[1]; |
paul@354 | 86 | __attr * const other = &__args[2]; |
paul@354 | 87 | /* _data, other interpreted as string */ |
paul@354 | 88 | char *s = _data->strvalue; |
paul@354 | 89 | char *o = other->strvalue; |
paul@354 | 90 | |
paul@354 | 91 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 92 | return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 93 | } |
paul@354 | 94 | |
paul@354 | 95 | __attr __fn_native_str_str_len(__attr __args[]) |
paul@354 | 96 | { |
paul@354 | 97 | __attr * const _data = &__args[1]; |
paul@354 | 98 | |
paul@354 | 99 | /* Return the new integer. */ |
paul@378 | 100 | return __new_int(_data->size); |
paul@354 | 101 | } |
paul@354 | 102 | |
paul@354 | 103 | __attr __fn_native_str_str_nonempty(__attr __args[]) |
paul@354 | 104 | { |
paul@354 | 105 | __attr * const _data = &__args[1]; |
paul@354 | 106 | |
paul@378 | 107 | return _data->size ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 108 | } |
paul@354 | 109 | |
paul@354 | 110 | __attr __fn_native_str_str_ord(__attr __args[]) |
paul@354 | 111 | { |
paul@354 | 112 | __attr * const _data = &__args[1]; |
paul@354 | 113 | /* _data interpreted as string */ |
paul@354 | 114 | char *s = _data->strvalue; |
paul@354 | 115 | |
paul@354 | 116 | return __new_int((unsigned int) s[0]); |
paul@354 | 117 | } |
paul@354 | 118 | |
paul@354 | 119 | __attr __fn_native_str_str_substr(__attr __args[]) |
paul@354 | 120 | { |
paul@354 | 121 | __attr * const _data = &__args[1]; |
paul@354 | 122 | __attr * const start = &__args[2]; |
paul@384 | 123 | __attr * const end = &__args[3]; |
paul@384 | 124 | __attr * const step = &__args[4]; |
paul@354 | 125 | /* _data interpreted as string */ |
paul@354 | 126 | char *s = _data->strvalue, *sub; |
paul@354 | 127 | /* start.__data__ interpreted as int */ |
paul@384 | 128 | int istart = __load_via_object(start->value, __pos___data__).intvalue; |
paul@384 | 129 | /* end.__data__ interpreted as int */ |
paul@384 | 130 | int iend = __load_via_object(end->value, __pos___data__).intvalue; |
paul@384 | 131 | /* step.__data__ interpreted as int */ |
paul@384 | 132 | int istep = __load_via_object(step->value, __pos___data__).intvalue; |
paul@384 | 133 | |
paul@384 | 134 | /* Calculate the size of the substring. */ |
paul@388 | 135 | size_t resultsize = ((iend - istart - (istep > 0 ? 1 : -1)) / istep) + 1; |
paul@384 | 136 | int to, from; |
paul@354 | 137 | |
paul@354 | 138 | /* Reserve space for a new string. */ |
paul@384 | 139 | sub = (char *) __ALLOCATE(resultsize + 1, sizeof(char)); |
paul@384 | 140 | |
paul@384 | 141 | /* Does not null terminate but final byte should be zero. */ |
paul@384 | 142 | if (istep > 0) |
paul@384 | 143 | for (from = istart, to = 0; from < iend; from += istep, to++) |
paul@384 | 144 | sub[to] = s[from]; |
paul@384 | 145 | else if (istep < 0) |
paul@384 | 146 | for (from = istart, to = 0; from > iend; from += istep, to++) |
paul@384 | 147 | sub[to] = s[from]; |
paul@384 | 148 | |
paul@384 | 149 | return __new_str(sub, resultsize); |
paul@354 | 150 | } |
paul@354 | 151 | |
paul@354 | 152 | /* Module initialisation. */ |
paul@354 | 153 | |
paul@354 | 154 | void __main_native_str() |
paul@354 | 155 | { |
paul@354 | 156 | } |