From 909b91ffccf636de9cd6f8d65544bd0268c97a92 Mon Sep 17 00:00:00 2001 From: JeffBezanson Date: Thu, 23 Apr 2009 00:22:03 +0000 Subject: [PATCH] fixing printing of invalid UTF-8 --- femtolisp/flisp.c | 24 +++++++----------------- femtolisp/print.c | 23 ++++++++++++++++++++--- femtolisp/todo | 7 ++++--- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/femtolisp/flisp.c b/femtolisp/flisp.c index 140b618..b313989 100644 --- a/femtolisp/flisp.c +++ b/femtolisp/flisp.c @@ -1358,23 +1358,11 @@ static void print_function(value_t v, ios_t *f, int princ) function_t *fn = value2c(function_t*,v); outs("#function(", f); char *data = cvalue_data(fn->bcode); - size_t sz = cvalue_len(fn->bcode); - outc('"', f); - size_t i; uint8_t c; - for(i=0; i < sz; i++) { - c = data[i]+48; - if (c == '\\') - outsn("\\\\", f, 2); - else if (c == '"') - outsn("\\\"", f, 2); - else if (c >= 32 && c < 0x7f) - outc(c, f); - else - ios_printf(f, "\\x%02x", c); - } - outsn("\" ", f, 2); - //fl_print_child(f, fn->bcode, 0); - //outc(' ', f); + size_t i, sz = cvalue_len(fn->bcode); + for(i=0; i < sz; i++) data[i] += 48; + fl_print_child(f, fn->bcode, 0); + for(i=0; i < sz; i++) data[i] -= 48; + outc(' ', f); fl_print_child(f, fn->vals, 0); if (fn->env != NIL) { outc(' ', f); @@ -1404,6 +1392,8 @@ static value_t fl_function(value_t *args, uint32_t nargs) { if (nargs != 3) argcount("function", nargs, 2); + if (!isstring(args[0])) + type_error("function", "string", args[0]); if (!isvector(args[1])) type_error("function", "vector", args[1]); cvalue_t *arr = (cvalue_t*)ptr(args[0]); diff --git a/femtolisp/print.c b/femtolisp/print.c index eb8fa81..700465a 100644 --- a/femtolisp/print.c +++ b/femtolisp/print.c @@ -436,11 +436,28 @@ static void print_string(ios_t *f, char *str, size_t sz) { char buf[512]; size_t i = 0; + uint8_t c; outc('"', f); - while (i < sz) { - size_t n = u8_escape(buf, sizeof(buf), str, &i, sz, 1, 0); - outsn(buf, f, n-1); + if (!u8_isvalid(str, sz)) { + // alternate print algorithm that preserves data if it's not UTF-8 + for(i=0; i < sz; i++) { + c = str[i]; + if (c == '\\') + outsn("\\\\", f, 2); + else if (c == '"') + outsn("\\\"", f, 2); + else if (c >= 32 && c < 0x7f) + outc(c, f); + else + HPOS += ios_printf(f, "\\x%02x", c); + } + } + else { + while (i < sz) { + size_t n = u8_escape(buf, sizeof(buf), str, &i, sz, 1, 0); + outsn(buf, f, n-1); + } } outc('"', f); } diff --git a/femtolisp/todo b/femtolisp/todo index 08a05f8..5aafc5b 100644 --- a/femtolisp/todo +++ b/femtolisp/todo @@ -1027,6 +1027,7 @@ new evaluator todo: - maxstack calculation, replace Stack with C stack, alloca - stack traces and better debugging support - lambda lifting -- let optimization -- have macroexpand use its own global syntax table -- be able to create/load an image file +* let optimization +* have macroexpand use its own global syntax table +* be able to create/load an image file +- opcodes NEG, ADD2