From 45e347588c8361355bc89469ff62ab830b6dbeda Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 8 Apr 2018 23:14:45 +0200 Subject: [PATCH] core: Rewrite string reader. * src/reader.c (reader_read_string): Rewrite, create much less garbage. (append_char): Remove. --- src/reader.c | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/reader.c b/src/reader.c index 4bdafaf6..1c0eee3b 100644 --- a/src/reader.c +++ b/src/reader.c @@ -344,30 +344,48 @@ reader_read_hex () return MAKE_NUMBER (s*n); } -SCM -append_char (SCM x, int i) -{ - return append2 (x, cons (MAKE_CHAR (i), cell_nil)); -} - SCM reader_read_string () { - SCM p = cell_nil; + char buf[1024]; + SCM lst = cell_nil; + int i = 0; int c = getchar (); - while (1) { - if (c == '"') break; - if (c == '\\' && peekchar () == '\\') p = append_char (p, getchar ()); - else if (c == '\\' && peekchar () == '"') p = append_char (p, getchar ()); - else if (c == '\\' && peekchar () == 'n') {getchar (); p = append_char (p, '\n');} - else if (c == '\\' && peekchar () == 't') {getchar (); p = append_char (p, '\t');} + while (1) + { + if (c == '"' || i > 1022) + { + buf[i] = 0; + lst = append2 (lst, cstring_to_list (buf)); + i = 0; + if (c == '"') + break; + } + if (c == '\\') + { + int p = peekchar (); + if (p == '\\' || p == '"') + buf[i++] = getchar (); + else if (p == 'n') + { + getchar (); + buf[i++] = '\n'; + } + else if (p == 't') + { + getchar (); + buf[i++] = '\t'; + } + } #if !__MESC__ - else if (c == EOF) assert (!"EOF in string"); + else if (c == EOF) + assert (!"EOF in string"); #endif - else p = append_char (p, c); + else + buf[i++] = c; c = getchar (); } - return MAKE_STRING (p); + return MAKE_STRING (lst); } int g_tiny = 0;