mes.c: implement string->list.

This commit is contained in:
Jan Nieuwenhuizen 2016-08-13 18:42:11 +02:00
parent 00e000306d
commit ec8553dc4f
3 changed files with 10 additions and 2 deletions

5
mes.c
View File

@ -1160,9 +1160,10 @@ readstring ()
int c = getchar ();
while (true) {
if (c == '"') break;
*p++ = c;
if (c == '\\' && peek_char () == '"') *p++ = getchar ();
if (c == EOF) assert (!"EOF in string");
else if (c == '\\' && peek_char () == 'n') {getchar (); *p++ = '\n';}
else if (c == EOF) assert (!"EOF in string");
else *p++ = c;
c = getchar ();
}
*p = 0;

View File

@ -58,6 +58,12 @@
(if (= 0 n) '()
(cons fill (loop (- n 1)))))))
(define (string->list s)
(let ((n (string-length s)))
(let loop ((i 0))
(if (= i n) '()
(cons (string-ref s i) (loop (+ i 1)))))))
(define (vector . rest) (list->vector rest))
(define (make-vector n . x)
(list->vector (apply make-list (cons n x))))

View File

@ -64,6 +64,7 @@
(pass-if "string-ref" (seq? (string-ref "hello world" 4) #\o))
(pass-if "eq?" (not (eq? (string-append "a" "b" "c") "abc")))
(pass-if "string-length" (seq? (string-length (string-append "a" "b" "c")) 3))
(pass-if "string->list" (sequal? (string->list "abc\n") '(#\a #\b #\c #\newline)))
(pass-if "char" (seq? (char->integer #\A) 65))
(pass-if "char 2" (seq? (char->integer #\101) (char->integer #\A)))
(pass-if "char 3" (seq? (integer->char 10) #\newline))