Small refactoring in parse_utf16 (+ tests) and typo fix in json_array_remove.

pull/85/head
Krzysztof Gabis 7 years ago
parent 578b25e590
commit e1292a0e3c
  1. 27
      parson.c
  2. 6
      tests.c

@ -527,14 +527,16 @@ static int parse_utf16(const char **unprocessed, char **processed) {
return JSONFailure;
}
if (cp < 0x80) {
*processed_ptr = (char)cp; /* 0xxxxxxx */
processed_ptr[0] = (char)cp; /* 0xxxxxxx */
} else if (cp < 0x800) {
*processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
*processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
processed_ptr[0] = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
processed_ptr[1] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
processed_ptr += 1;
} else if (cp < 0xD800 || cp > 0xDFFF) {
*processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
*processed_ptr++ = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
*processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */
processed_ptr[0] = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
processed_ptr[1] = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */
processed_ptr[2] = ((cp) & 0x3F) | 0x80; /* 10xxxxxx */
processed_ptr += 2;
} else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
lead = cp;
unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
@ -545,11 +547,12 @@ static int parse_utf16(const char **unprocessed, char **processed) {
if (!parse_succeeded || trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
return JSONFailure;
}
cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000;
*processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
*processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
*processed_ptr++ = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
*processed_ptr = (((cp ) & 0x3F) | 0x80); /* 10xxxxxx */
cp = ((((lead - 0xD800) & 0x3FF) << 10) | ((trail - 0xDC00) & 0x3FF)) + 0x010000;
processed_ptr[0] = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
processed_ptr[1] = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
processed_ptr[2] = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */
processed_ptr[3] = (((cp) & 0x3F) | 0x80); /* 10xxxxxx */
processed_ptr += 3;
} else { /* trail surrogate before lead surrogate */
return JSONFailure;
}
@ -1552,7 +1555,7 @@ JSON_Status json_array_remove(JSON_Array *array, size_t ix) {
return JSONFailure;
}
json_value_free(json_array_get_value(array, ix));
to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value**);
to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value*);
memmove(array->items + ix, array->items + ix + 1, to_move_bytes);
array->count -= 1;
return JSONSuccess;

@ -247,6 +247,12 @@ void test_suite_3(void) {
TEST(json_parse_string("\"string\"") != NULL);
TEST(json_parse_string("123") != NULL);
puts("Test UTF-16 parsing:");
TEST(STREQ(json_string(json_parse_string("\"\\u0024x\"")), "$x"));
TEST(STREQ(json_string(json_parse_string("\"\\u00A2x\"")), "¢x"));
TEST(STREQ(json_string(json_parse_string("\"\\u20ACx\"")), "€x"));
TEST(STREQ(json_string(json_parse_string("\"\\uD801\\uDC37x\"")), "𐐷x"));
puts("Testing invalid strings:");
malloc_count = 0;
TEST(json_parse_string(NULL) == NULL);

Loading…
Cancel
Save