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