paul@354 | 1 | /* Native functions for string operations. |
paul@354 | 2 | |
paul@354 | 3 | Copyright (C) 2016 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@354 | 48 | __attr __fn_native_str_str_lt(__attr __args[]) |
paul@354 | 49 | { |
paul@354 | 50 | __attr * const _data = &__args[1]; |
paul@354 | 51 | __attr * const other = &__args[2]; |
paul@354 | 52 | /* _data, other interpreted as string */ |
paul@354 | 53 | char *s = _data->strvalue; |
paul@354 | 54 | char *o = other->strvalue; |
paul@354 | 55 | |
paul@354 | 56 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 57 | return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 58 | } |
paul@354 | 59 | |
paul@354 | 60 | __attr __fn_native_str_str_gt(__attr __args[]) |
paul@354 | 61 | { |
paul@354 | 62 | __attr * const _data = &__args[1]; |
paul@354 | 63 | __attr * const other = &__args[2]; |
paul@354 | 64 | /* _data, other interpreted as string */ |
paul@354 | 65 | char *s = _data->strvalue; |
paul@354 | 66 | char *o = other->strvalue; |
paul@354 | 67 | |
paul@354 | 68 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 69 | return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 70 | } |
paul@354 | 71 | |
paul@354 | 72 | __attr __fn_native_str_str_eq(__attr __args[]) |
paul@354 | 73 | { |
paul@354 | 74 | __attr * const _data = &__args[1]; |
paul@354 | 75 | __attr * const other = &__args[2]; |
paul@354 | 76 | /* _data, other interpreted as string */ |
paul@354 | 77 | char *s = _data->strvalue; |
paul@354 | 78 | char *o = other->strvalue; |
paul@354 | 79 | |
paul@354 | 80 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 81 | return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 82 | } |
paul@354 | 83 | |
paul@354 | 84 | __attr __fn_native_str_str_len(__attr __args[]) |
paul@354 | 85 | { |
paul@354 | 86 | __attr * const _data = &__args[1]; |
paul@354 | 87 | /* _data interpreted as string */ |
paul@354 | 88 | char *s = _data->strvalue; |
paul@354 | 89 | |
paul@354 | 90 | /* Return the new integer. */ |
paul@378 | 91 | return __new_int(_data->size); |
paul@354 | 92 | } |
paul@354 | 93 | |
paul@354 | 94 | __attr __fn_native_str_str_nonempty(__attr __args[]) |
paul@354 | 95 | { |
paul@354 | 96 | __attr * const _data = &__args[1]; |
paul@354 | 97 | /* _data interpreted as string */ |
paul@354 | 98 | char *s = _data->strvalue; |
paul@354 | 99 | |
paul@378 | 100 | return _data->size ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 101 | } |
paul@354 | 102 | |
paul@354 | 103 | __attr __fn_native_str_str_ord(__attr __args[]) |
paul@354 | 104 | { |
paul@354 | 105 | __attr * const _data = &__args[1]; |
paul@354 | 106 | /* _data interpreted as string */ |
paul@354 | 107 | char *s = _data->strvalue; |
paul@354 | 108 | |
paul@354 | 109 | return __new_int((unsigned int) s[0]); |
paul@354 | 110 | } |
paul@354 | 111 | |
paul@354 | 112 | __attr __fn_native_str_str_substr(__attr __args[]) |
paul@354 | 113 | { |
paul@354 | 114 | __attr * const _data = &__args[1]; |
paul@354 | 115 | __attr * const start = &__args[2]; |
paul@354 | 116 | __attr * const size = &__args[3]; |
paul@354 | 117 | /* _data interpreted as string */ |
paul@354 | 118 | char *s = _data->strvalue, *sub; |
paul@354 | 119 | /* start.__data__ interpreted as int */ |
paul@354 | 120 | int i = __load_via_object(start->value, __pos___data__).intvalue; |
paul@354 | 121 | /* size.__data__ interpreted as int */ |
paul@354 | 122 | int l = __load_via_object(size->value, __pos___data__).intvalue; |
paul@354 | 123 | |
paul@354 | 124 | /* Reserve space for a new string. */ |
paul@354 | 125 | sub = (char *) __ALLOCATE(l + 1, sizeof(char)); |
paul@378 | 126 | memcpy(sub, s + i, l); /* does not null terminate but final byte should be zero */ |
paul@378 | 127 | return __new_str(sub, l); |
paul@354 | 128 | } |
paul@354 | 129 | |
paul@354 | 130 | /* Module initialisation. */ |
paul@354 | 131 | |
paul@354 | 132 | void __main_native_str() |
paul@354 | 133 | { |
paul@354 | 134 | } |