shithub: femtolisp

Download patch

ref: 909b91ffccf636de9cd6f8d65544bd0268c97a92
parent: e4488bb065f2f4f82f25b6f4d3ab9fb1b6150fa9
author: JeffBezanson <jeff.bezanson@gmail.com>
date: Wed Apr 22 20:22:03 EDT 2009

fixing printing of invalid UTF-8


--- a/femtolisp/flisp.c
+++ b/femtolisp/flisp.c
@@ -1358,23 +1358,11 @@
     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 @@
 {
     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]);
--- a/femtolisp/print.c
+++ b/femtolisp/print.c
@@ -436,11 +436,28 @@
 {
     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);
 }
--- a/femtolisp/todo
+++ b/femtolisp/todo
@@ -1027,6 +1027,7 @@
 - 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