paul@354 | 1 | /* Native functions for string operations. |
paul@354 | 2 | |
paul@941 | 3 | Copyright (C) 2016, 2017, 2021 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@664 | 31 | __attr __fn_native_str_str_add(__attr __self, __attr _data, __attr other, __attr _size, __attr othersize) |
paul@354 | 32 | { |
paul@664 | 33 | /* _data, other interpreted as string.__data__ */ |
paul@664 | 34 | char *s = _data.strvalue; |
paul@664 | 35 | char *o = other.strvalue; |
paul@763 | 36 | /* _size, othersize interpreted as int */ |
paul@941 | 37 | __int ss = __TOINT(_size), os = __TOINT(othersize); |
paul@941 | 38 | __int n = ss + os; |
paul@378 | 39 | char *r = (char *) __ALLOCATE(n + 1, sizeof(char)); |
paul@354 | 40 | |
paul@569 | 41 | memcpy(r, s, ss); |
paul@569 | 42 | memcpy(r + ss, o, os); |
paul@354 | 43 | |
paul@354 | 44 | /* Return a new string. */ |
paul@583 | 45 | return __new_str(r, n); |
paul@354 | 46 | } |
paul@354 | 47 | |
paul@664 | 48 | __attr __fn_native_str_str_chr(__attr __self, __attr _data) |
paul@531 | 49 | { |
paul@758 | 50 | /* data interpreted as int */ |
paul@763 | 51 | int n = __TOINT(_data); |
paul@531 | 52 | char *s = (char *) __ALLOCATE(2, sizeof(char)); |
paul@531 | 53 | |
paul@531 | 54 | s[0] = (char) n; |
paul@583 | 55 | return __new_str(s, 1); |
paul@531 | 56 | } |
paul@531 | 57 | |
paul@664 | 58 | __attr __fn_native_str_str_lt(__attr __self, __attr _data, __attr other) |
paul@354 | 59 | { |
paul@664 | 60 | /* _data, other interpreted as string.__data__ */ |
paul@664 | 61 | char *s = _data.strvalue; |
paul@664 | 62 | char *o = other.strvalue; |
paul@354 | 63 | |
paul@354 | 64 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 65 | return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 66 | } |
paul@354 | 67 | |
paul@664 | 68 | __attr __fn_native_str_str_gt(__attr __self, __attr _data, __attr other) |
paul@354 | 69 | { |
paul@664 | 70 | /* _data, other interpreted as string.__data__ */ |
paul@664 | 71 | char *s = _data.strvalue; |
paul@664 | 72 | char *o = other.strvalue; |
paul@354 | 73 | |
paul@354 | 74 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 75 | return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 76 | } |
paul@354 | 77 | |
paul@664 | 78 | __attr __fn_native_str_str_eq(__attr __self, __attr _data, __attr other) |
paul@354 | 79 | { |
paul@664 | 80 | /* _data, other interpreted as string.__data__ */ |
paul@664 | 81 | char *s = _data.strvalue; |
paul@664 | 82 | char *o = other.strvalue; |
paul@354 | 83 | |
paul@354 | 84 | /* NOTE: Using simple byte-level string operations. */ |
paul@354 | 85 | return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 86 | } |
paul@354 | 87 | |
paul@664 | 88 | __attr __fn_native_str_str_ord(__attr __self, __attr _data) |
paul@354 | 89 | { |
paul@664 | 90 | /* _data interpreted as string.__data__ */ |
paul@664 | 91 | char *s = _data.strvalue; |
paul@354 | 92 | |
paul@941 | 93 | return __new_int((__int) s[0]); |
paul@354 | 94 | } |
paul@354 | 95 | |
paul@664 | 96 | __attr __fn_native_str_str_substr(__attr __self, __attr _data, __attr start, __attr end, __attr step) |
paul@354 | 97 | { |
paul@664 | 98 | /* _data interpreted as string.__data__ */ |
paul@664 | 99 | char *s = _data.strvalue, *sub; |
paul@758 | 100 | /* start interpreted as int */ |
paul@941 | 101 | __int istart = __TOINT(start); |
paul@758 | 102 | /* end interpreted as int */ |
paul@941 | 103 | __int iend = __TOINT(end); |
paul@758 | 104 | /* step interpreted as int */ |
paul@941 | 105 | __int istep = __TOINT(step); |
paul@384 | 106 | |
paul@384 | 107 | /* Calculate the size of the substring. */ |
paul@388 | 108 | size_t resultsize = ((iend - istart - (istep > 0 ? 1 : -1)) / istep) + 1; |
paul@941 | 109 | size_t to, from; |
paul@354 | 110 | |
paul@354 | 111 | /* Reserve space for a new string. */ |
paul@384 | 112 | sub = (char *) __ALLOCATE(resultsize + 1, sizeof(char)); |
paul@384 | 113 | |
paul@384 | 114 | /* Does not null terminate but final byte should be zero. */ |
paul@384 | 115 | if (istep > 0) |
paul@384 | 116 | for (from = istart, to = 0; from < iend; from += istep, to++) |
paul@384 | 117 | sub[to] = s[from]; |
paul@384 | 118 | else if (istep < 0) |
paul@384 | 119 | for (from = istart, to = 0; from > iend; from += istep, to++) |
paul@384 | 120 | sub[to] = s[from]; |
paul@384 | 121 | |
paul@583 | 122 | return __new_str(sub, resultsize); |
paul@354 | 123 | } |
paul@354 | 124 | |
paul@354 | 125 | /* Module initialisation. */ |
paul@354 | 126 | |
paul@354 | 127 | void __main_native_str() |
paul@354 | 128 | { |
paul@354 | 129 | } |