From ec8553dc4f46c54abe7a0bb6abf3485eab1f6e4c Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sat, 13 Aug 2016 18:42:11 +0200 Subject: [PATCH] mes.c: implement string->list. --- mes.c | 5 +++-- scm.mes | 6 ++++++ test/scm.test | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mes.c b/mes.c index a227f6a0..f225ba2f 100644 --- a/mes.c +++ b/mes.c @@ -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; diff --git a/scm.mes b/scm.mes index 5c17a406..19d79f4b 100755 --- a/scm.mes +++ b/scm.mes @@ -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)))) diff --git a/test/scm.test b/test/scm.test index f2511546..4bd39563 100644 --- a/test/scm.test +++ b/test/scm.test @@ -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))