From 5118881e6c5c543eeb856adbcc77394c97170a43 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Wed, 1 Feb 2023 17:25:54 -0800 Subject: [PATCH] Handle stretches of blanks. Don't create zero-length strings. --- implementations/C/parser.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/implementations/C/parser.c b/implementations/C/parser.c index f03996f..9285741 100644 --- a/implementations/C/parser.c +++ b/implementations/C/parser.c @@ -5,7 +5,7 @@ const char *BLANKS = " \t"; -const char *TEXT = " hi there fr[ie]nd"; +const char *TEXT = " hi there fr [ ie]nd"; /* 01234567890123456789 ^ ^ */ @@ -32,18 +32,23 @@ main(void) while (NULL != rest) { /* How many chars have we got? */ diff = rest - text; - printf("%ld\n", diff); + /*printf("%ld\n", diff);*/ - /* Allocate and copy out the substring. */ - snip = (char *)GC_malloc(diff + 1); - strncat(snip, text, diff); - printf("%s\n", snip); + if (diff) { + /* Allocate and copy out the substring. */ + snip = (char *)GC_malloc(diff + 1); + strncat(snip, text, diff); + printf("%s\n", snip); + } /* The next char is a space or '[' or ']'. */ - printf("<%c>\n", rest[0]); + if ('[' == rest[0] || ']' == rest[0]) { + printf("%c\n", rest[0]); + } /* */ text = trim_leading_blanks(++rest); + /*printf(">>>%s\n\n", text);*/ rest = strpbrk(text, " []"); } printf("%s\n", text);