Fri, 17 Jan 2025 17:41:29 +0100
fix wrong accidental unicode escaping
src/json.c | file | annotate | diff | comparison | revisions | |
tests/test_json.c | file | annotate | diff | comparison | revisions |
--- a/src/json.c Thu Jan 16 18:56:44 2025 +0100 +++ b/src/json.c Fri Jan 17 17:41:29 2025 +0100 @@ -393,10 +393,9 @@ bool all_printable = true; for (size_t i = 0; i < str.length; i++) { - bool escape = !isprint(str.ptr[i]) - || str.ptr[i] == '\\' - || str.ptr[i] == '"' - || (escape_slash && str.ptr[i] == '/'); + unsigned char c = str.ptr[i]; + bool escape = c < 0x20 || c == '\\' || c == '"' + || (escape_slash && c == '/'); if (all_printable && escape) { size_t capa = str.length + 32; @@ -408,30 +407,29 @@ } if (escape) { cxBufferPut(&buf, '\\'); - if (str.ptr[i] == '\"') { + if (c == '\"') { cxBufferPut(&buf, '\"'); - } else if (str.ptr[i] == '\n') { + } else if (c == '\n') { cxBufferPut(&buf, 'n'); - } else if (str.ptr[i] == '\t') { + } else if (c == '\t') { cxBufferPut(&buf, 't'); - } else if (str.ptr[i] == '\r') { + } else if (c == '\r') { cxBufferPut(&buf, 'r'); - } else if (str.ptr[i] == '\\') { + } else if (c == '\\') { cxBufferPut(&buf, '\\'); - } else if (str.ptr[i] == '/') { + } else if (c == '/') { cxBufferPut(&buf, '/'); - } else if (str.ptr[i] == '\f') { + } else if (c == '\f') { cxBufferPut(&buf, 'f'); - } else if (str.ptr[i] == '\b') { + } else if (c == '\b') { cxBufferPut(&buf, 'b'); } else { char code[6]; - snprintf(code, sizeof(code), "u%04x", - (unsigned int)(0xff & str.ptr[i])); + snprintf(code, sizeof(code), "u%04x", (unsigned int) c); cxBufferPutString(&buf, code); } } else if (!all_printable) { - cxBufferPut(&buf, str.ptr[i]); + cxBufferPut(&buf, c); } } if (!all_printable) {
--- a/tests/test_json.c Thu Jan 16 18:56:44 2025 +0100 +++ b/tests/test_json.c Fri Jan 17 17:41:29 2025 +0100 @@ -961,7 +961,7 @@ CX_TEST_DO { cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer); CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), - CX_STR("\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\""))); + CX_STR("\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\""))); } cxBufferDestroy(&buf); cxJsonValueFree(str); @@ -977,7 +977,7 @@ CX_TEST_DO { cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer); CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), - CX_STR("{\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}"))); + CX_STR("{\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}"))); } cxBufferDestroy(&buf); cxJsonValueFree(obj);