fixing printing of invalid UTF-8

This commit is contained in:
JeffBezanson 2009-04-23 00:22:03 +00:00
parent e4488bb065
commit 909b91ffcc
3 changed files with 31 additions and 23 deletions

View File

@ -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]);

View File

@ -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);
}

View File

@ -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