core: Rewrite string reader.

* src/reader.c (reader_read_string): Rewrite, create much less
  garbage.
  (append_char): Remove.
This commit is contained in:
Jan Nieuwenhuizen 2018-04-08 23:14:45 +02:00
parent 7d32ecd906
commit 45e347588c
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273

View file

@ -344,30 +344,48 @@ reader_read_hex ()
return MAKE_NUMBER (s*n); return MAKE_NUMBER (s*n);
} }
SCM
append_char (SCM x, int i)
{
return append2 (x, cons (MAKE_CHAR (i), cell_nil));
}
SCM SCM
reader_read_string () reader_read_string ()
{ {
SCM p = cell_nil; char buf[1024];
SCM lst = cell_nil;
int i = 0;
int c = getchar (); int c = getchar ();
while (1) { while (1)
if (c == '"') break; {
if (c == '\\' && peekchar () == '\\') p = append_char (p, getchar ()); if (c == '"' || i > 1022)
else if (c == '\\' && peekchar () == '"') p = append_char (p, getchar ()); {
else if (c == '\\' && peekchar () == 'n') {getchar (); p = append_char (p, '\n');} buf[i] = 0;
else if (c == '\\' && peekchar () == 't') {getchar (); p = append_char (p, '\t');} 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__ #if !__MESC__
else if (c == EOF) assert (!"EOF in string"); else if (c == EOF)
assert (!"EOF in string");
#endif #endif
else p = append_char (p, c); else
buf[i++] = c;
c = getchar (); c = getchar ();
} }
return MAKE_STRING (p); return MAKE_STRING (lst);
} }
int g_tiny = 0; int g_tiny = 0;