core: Add string-ref.

* src/strings.c (string_ref): New function.
* mes/module/mes/scm.mes (string-ref): Remove.
This commit is contained in:
Jan Nieuwenhuizen 2018-11-16 00:15:50 +01:00
parent 6af0b49f09
commit 1ab054002c
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
3 changed files with 17 additions and 3 deletions

View File

@ -194,9 +194,6 @@
(define (make-string n . fill)
(list->string (apply make-list n fill)))
(define (string-ref s k)
(list-ref (string->list s) k))
(define (string-set! s k v)
(list->string (list-set! (string->list s) k v)))

View File

@ -1258,6 +1258,8 @@ eval_apply ()
goto begin;
}
}
// write_error_ (CAR (r1));
// eputs ("\n");
push_cc (CAR (r1), r1, r0, cell_vm_apply2);
goto eval;
apply2:

View File

@ -18,6 +18,8 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#define MAX_STRING 4096
char const*
@ -267,3 +269,16 @@ string_length (SCM string)
assert (TYPE (string) == TSTRING);
return MAKE_NUMBER (LENGTH (string));
}
SCM
string_ref (SCM str, SCM k)
{
assert (TYPE (str) == TSTRING);
assert (TYPE (k) == TNUMBER);
size_t size = LENGTH (str);
size_t i = VALUE (k);
if (i >= size)
error (cell_symbol_system_error, cons (MAKE_STRING0 ("value out of range"), k));
char const *p = CSTRING (str);
return MAKE_CHAR (p[i]);
}