fixing printing of invalid UTF-8
This commit is contained in:
		
							parent
							
								
									e4488bb065
								
							
						
					
					
						commit
						909b91ffcc
					
				| 
						 | 
					@ -1358,23 +1358,11 @@ static void print_function(value_t v, ios_t *f, int princ)
 | 
				
			||||||
    function_t *fn = value2c(function_t*,v);
 | 
					    function_t *fn = value2c(function_t*,v);
 | 
				
			||||||
    outs("#function(", f);
 | 
					    outs("#function(", f);
 | 
				
			||||||
    char *data = cvalue_data(fn->bcode);
 | 
					    char *data = cvalue_data(fn->bcode);
 | 
				
			||||||
    size_t sz = cvalue_len(fn->bcode);
 | 
					    size_t i, sz = cvalue_len(fn->bcode);
 | 
				
			||||||
    outc('"', f);
 | 
					    for(i=0; i < sz; i++) data[i] += 48;
 | 
				
			||||||
    size_t i; uint8_t c;
 | 
					    fl_print_child(f, fn->bcode, 0);
 | 
				
			||||||
    for(i=0; i < sz; i++) {
 | 
					    for(i=0; i < sz; i++) data[i] -= 48;
 | 
				
			||||||
        c = data[i]+48;
 | 
					    outc(' ', f);
 | 
				
			||||||
        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);
 | 
					 | 
				
			||||||
    fl_print_child(f, fn->vals, 0);
 | 
					    fl_print_child(f, fn->vals, 0);
 | 
				
			||||||
    if (fn->env != NIL) {
 | 
					    if (fn->env != NIL) {
 | 
				
			||||||
        outc(' ', f);
 | 
					        outc(' ', f);
 | 
				
			||||||
| 
						 | 
					@ -1404,6 +1392,8 @@ static value_t fl_function(value_t *args, uint32_t nargs)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (nargs != 3)
 | 
					    if (nargs != 3)
 | 
				
			||||||
        argcount("function", nargs, 2);
 | 
					        argcount("function", nargs, 2);
 | 
				
			||||||
 | 
					    if (!isstring(args[0]))
 | 
				
			||||||
 | 
					        type_error("function", "string", args[0]);
 | 
				
			||||||
    if (!isvector(args[1]))
 | 
					    if (!isvector(args[1]))
 | 
				
			||||||
        type_error("function", "vector", args[1]);
 | 
					        type_error("function", "vector", args[1]);
 | 
				
			||||||
    cvalue_t *arr = (cvalue_t*)ptr(args[0]);
 | 
					    cvalue_t *arr = (cvalue_t*)ptr(args[0]);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -436,12 +436,29 @@ static void print_string(ios_t *f, char *str, size_t sz)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    char buf[512];
 | 
					    char buf[512];
 | 
				
			||||||
    size_t i = 0;
 | 
					    size_t i = 0;
 | 
				
			||||||
 | 
					    uint8_t c;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    outc('"', f);
 | 
					    outc('"', f);
 | 
				
			||||||
 | 
					    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) {
 | 
					        while (i < sz) {
 | 
				
			||||||
            size_t n = u8_escape(buf, sizeof(buf), str, &i, sz, 1, 0);
 | 
					            size_t n = u8_escape(buf, sizeof(buf), str, &i, sz, 1, 0);
 | 
				
			||||||
            outsn(buf, f, n-1);
 | 
					            outsn(buf, f, n-1);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    outc('"', f);
 | 
					    outc('"', f);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1027,6 +1027,7 @@ new evaluator todo:
 | 
				
			||||||
- maxstack calculation, replace Stack with C stack, alloca
 | 
					- maxstack calculation, replace Stack with C stack, alloca
 | 
				
			||||||
  - stack traces and better debugging support
 | 
					  - stack traces and better debugging support
 | 
				
			||||||
- lambda lifting
 | 
					- lambda lifting
 | 
				
			||||||
- let optimization
 | 
					* let optimization
 | 
				
			||||||
- have macroexpand use its own global syntax table
 | 
					* have macroexpand use its own global syntax table
 | 
				
			||||||
- be able to create/load an image file
 | 
					* be able to create/load an image file
 | 
				
			||||||
 | 
					- opcodes NEG, ADD2
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue