Skip to content

Commit

Permalink
parsers: various bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Nov 12, 2022
1 parent 412fc3f commit 7ca6168
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
18.1.5 - set parent to parsed JSON nodes (fixed)
- fix zpl_json/csv_write_string off-by-one issue
18.1.4 - fix zpl_random_gen_isize/zpl_random_range_isize 32bit overflow
18.1.3 - set parent to parsed JSON nodes
18.1.2 - fix zpl sort procs
Expand Down
36 changes: 18 additions & 18 deletions code/source/parsers/csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ zpl_u8 zpl_csv_parse_delimiter(zpl_csv_object *root, char *text, zpl_allocator a
zpl_adt_make_branch(root, allocator, NULL, has_header ? false : true);
char *p = text, *b = p, *e = p;
zpl_isize colc = 0, total_colc = 0;

do {
char d = 0;
p = cast(char *)zpl_str_trim(p, false);
Expand All @@ -28,7 +28,7 @@ zpl_u8 zpl_csv_parse_delimiter(zpl_csv_object *root, char *text, zpl_allocator a
#ifndef ZPL_PARSER_DISABLE_ANALYSIS
row_item.name_style = ZPL_ADT_NAME_STYLE_NO_QUOTES;
#endif

/* handle string literals */
if (*p == '"') {
p = b = e = p+1;
Expand All @@ -51,7 +51,7 @@ zpl_u8 zpl_csv_parse_delimiter(zpl_csv_object *root, char *text, zpl_allocator a
*e = 0;
p = cast(char *)zpl_str_trim(e+1, true);
d = *p;

/* unescape escaped quotes (so that unescaped text escapes :) */
{
char *ep = b;
Expand Down Expand Up @@ -84,7 +84,7 @@ zpl_u8 zpl_csv_parse_delimiter(zpl_csv_object *root, char *text, zpl_allocator a
d = 0;
p = e;
}

/* check if number and process if so */
zpl_b32 skip_number = false;
char *num_p = b;
Expand All @@ -94,18 +94,18 @@ zpl_u8 zpl_csv_parse_delimiter(zpl_csv_object *root, char *text, zpl_allocator a
break;
}
} while (*num_p++);

if (!skip_number) {
zpl_adt_str_to_number(&row_item);
}
}

if (colc >= zpl_array_count(root->nodes)) {
zpl_adt_append_arr(root, NULL);
}

zpl_array_append(root->nodes[colc].nodes, row_item);

if (d == delim) {
colc++;
p++;
Expand All @@ -122,13 +122,13 @@ zpl_u8 zpl_csv_parse_delimiter(zpl_csv_object *root, char *text, zpl_allocator a
if (d != 0) p++;
}
} while(*p);

if (zpl_array_count(root->nodes) == 0) {
ZPL_CSV_ASSERT("unexpected end of input. stream is empty.");
err = ZPL_CSV_ERROR_UNEXPECTED_END_OF_INPUT;
return err;
}

/* consider first row as a header. */
if (has_header) {
for (zpl_isize i = 0; i < zpl_array_count(root->nodes); i++) {
Expand All @@ -138,7 +138,7 @@ zpl_u8 zpl_csv_parse_delimiter(zpl_csv_object *root, char *text, zpl_allocator a
zpl_array_remove_at(col->nodes, 0);
}
}

return err;
}
void zpl_csv_free(zpl_csv_object *obj) {
Expand All @@ -155,7 +155,7 @@ void zpl__csv_write_record(zpl_file *file, zpl_csv_object *node) {
zpl_adt_print_string(file, node, "\"", "\"");
zpl_fprintf(file, "\"");
} break;

case ZPL_ADT_NAME_STYLE_NO_QUOTES: {
#endif
zpl_fprintf(file, "%s", node->string);
Expand All @@ -164,7 +164,7 @@ void zpl__csv_write_record(zpl_file *file, zpl_csv_object *node) {
}
#endif
} break;

case ZPL_ADT_TYPE_REAL:
case ZPL_ADT_TYPE_INTEGER: {
zpl_adt_print_number(file, node);
Expand All @@ -185,12 +185,12 @@ void zpl_csv_write_delimiter(zpl_file *file, zpl_csv_object *obj, char delimiter
ZPL_ASSERT(obj->nodes);
zpl_isize cols = zpl_array_count(obj->nodes);
if (cols == 0) return;

zpl_isize rows = zpl_array_count(obj->nodes[0].nodes);
if (rows == 0) return;

zpl_b32 has_headers = obj->nodes[0].name != NULL;

if (has_headers) {
for (zpl_isize i = 0; i < cols; i++) {
zpl__csv_write_header(file, &obj->nodes[i]);
Expand All @@ -200,7 +200,7 @@ void zpl_csv_write_delimiter(zpl_file *file, zpl_csv_object *obj, char delimiter
}
zpl_fprintf(file, "\n");
}

for (zpl_isize r = 0; r < rows; r++) {
for (zpl_isize i = 0; i < cols; i++) {
zpl__csv_write_record(file, &obj->nodes[i].nodes[r]);
Expand All @@ -218,7 +218,7 @@ zpl_string zpl_csv_write_string_delimiter(zpl_allocator a, zpl_csv_object *obj,
zpl_csv_write_delimiter(&tmp, obj, delimiter);
zpl_isize fsize;
zpl_u8* buf = zpl_file_stream_buf(&tmp, &fsize);
zpl_string output = zpl_string_make_length(a, (char *)buf, fsize+1);
zpl_string output = zpl_string_make_length(a, (char *)buf, fsize);
zpl_file_close(&tmp);
return output;
}
Expand Down
18 changes: 13 additions & 5 deletions code/source/parsers/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ zpl_string zpl_json_write_string(zpl_allocator a, zpl_adt_node *obj, zpl_isize i
zpl_json_write(&tmp, obj, indent);
zpl_isize fsize;
zpl_u8* buf = zpl_file_stream_buf(&tmp, &fsize);
zpl_string output = zpl_string_make_length(a, (char *)buf, fsize+1);
zpl_string output = zpl_string_make_length(a, (char *)buf, fsize);
zpl_file_close(&tmp);
return output;
}

/* private */

#define zpl__json_append_node(x, item)\
do {\
zpl_array_append(x, item);\
if (item.type == ZPL_ADT_TYPE_OBJECT || item.type == ZPL_ADT_TYPE_ARRAY) {\
for (zpl_isize i = 0; i < zpl_array_count(item.nodes); i++)\
item.nodes[i].parent = zpl_array_end(x);\
}\
} while (0);

static ZPL_ALWAYS_INLINE zpl_b32 zpl__json_is_assign_char(char c) { return !!zpl_strchr(":=|", c); }
static ZPL_ALWAYS_INLINE zpl_b32 zpl__json_is_delim_char(char c) { return !!zpl_strchr(",|\n", c); }
ZPL_DEF_INLINE zpl_b32 zpl__json_validate_name(char const *str, char *err);
Expand Down Expand Up @@ -97,8 +106,7 @@ char *zpl__json_parse_array(zpl_adt_node *obj, char *base, zpl_allocator a, zpl_

if (*err_code != ZPL_JSON_ERROR_NONE) { return NULL; }

zpl_array_append(obj->nodes, elem);
zpl_array_end(obj->nodes)->parent = obj;
zpl__json_append_node(obj->nodes, elem);

p = zpl__json_trim(p, false);

Expand Down Expand Up @@ -219,8 +227,7 @@ char *zpl__json_parse_object(zpl_adt_node *obj, char *base, zpl_allocator a, zpl
p = zpl__json_parse_value(&node, p, a, err_code);
if (err_code && *err_code != ZPL_JSON_ERROR_NONE) { return NULL; }

zpl_array_append(obj->nodes, node);
zpl_array_end(obj->nodes)->parent = obj;
zpl__json_append_node(obj->nodes, node);

char *end_p = p; zpl_unused(end_p);
p = zpl__json_trim(p, true);
Expand Down Expand Up @@ -480,5 +487,6 @@ void zpl__json_write_value(zpl_file *f, zpl_adt_node *o, zpl_adt_node *t, zpl_is
}

#undef zpl___ind
#undef zpl__json_append_node

ZPL_END_C_DECLS

0 comments on commit 7ca6168

Please sign in to comment.