Skip to content

Commit

Permalink
Add option to escape characters in entity path
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Oct 31, 2024
1 parent 6a76b64 commit 3b9278f
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 76 deletions.
88 changes: 53 additions & 35 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9466,13 +9466,13 @@ void ecs_id_str_buf(
}

ecs_strbuf_appendch(buf, '(');
ecs_get_path_w_sep_buf(world, 0, rel, NULL, NULL, buf);
ecs_get_path_w_sep_buf(world, 0, rel, NULL, NULL, buf, false);
ecs_strbuf_appendch(buf, ',');
ecs_get_path_w_sep_buf(world, 0, obj, NULL, NULL, buf);
ecs_get_path_w_sep_buf(world, 0, obj, NULL, NULL, buf, false);
ecs_strbuf_appendch(buf, ')');
} else {
ecs_entity_t e = id & ECS_COMPONENT_MASK;
ecs_get_path_w_sep_buf(world, 0, e, NULL, NULL, buf);
ecs_get_path_w_sep_buf(world, 0, e, NULL, NULL, buf, false);
}

error:
Expand Down Expand Up @@ -9544,7 +9544,7 @@ char* ecs_entity_str(
ecs_strbuf_t buf = ECS_STRBUF_INIT;
ecs_check(ecs_is_alive(world, entity), ECS_INVALID_PARAMETER, NULL);

ecs_get_path_w_sep_buf(world, 0, entity, 0, "", &buf);
ecs_get_path_w_sep_buf(world, 0, entity, 0, "", &buf, false);

ecs_strbuf_appendlit(&buf, " [");
const ecs_type_t *type = ecs_get_type(world, entity);
Expand Down Expand Up @@ -10297,7 +10297,8 @@ bool flecs_path_append(
ecs_entity_t child,
const char *sep,
const char *prefix,
ecs_strbuf_t *buf)
ecs_strbuf_t *buf,
bool escape)
{
flecs_poly_assert(world, ecs_world_t);
ecs_assert(sep[0] != 0, ECS_INVALID_PARAMETER, NULL);
Expand All @@ -10318,7 +10319,7 @@ bool flecs_path_append(
if (cur) {
ecs_assert(cur != child, ECS_CYCLE_DETECTED, NULL);
if (cur != parent && (cur != EcsFlecsCore || prefix != NULL)) {
flecs_path_append(world, parent, cur, sep, prefix, buf);
flecs_path_append(world, parent, cur, sep, prefix, buf, escape);
if (!sep[1]) {
ecs_strbuf_appendch(buf, sep[0]);
} else {
Expand Down Expand Up @@ -10349,19 +10350,35 @@ bool flecs_path_append(
sep_in_name = strchr(name, sep[0]);
}

if (sep_in_name) {
const char *name_ptr = name;
while (sep_in_name) {
ecs_size_t len = flecs_ito(int32_t, sep_in_name - name_ptr);
ecs_strbuf_appendstrn(buf, name_ptr, len);
ecs_strbuf_appendch(buf, '\\');
ecs_strbuf_appendch(buf, sep[0]);

name_ptr = sep_in_name + 1;
sep_in_name = strchr(name_ptr, sep[0]);
if (sep_in_name || escape) {
const char *name_ptr;
char ch;
for (name_ptr = name; (ch = name_ptr[0]); name_ptr ++) {
char esc[3];
if (ch != sep[0]) {
if (escape) {
flecs_chresc(esc, ch, '\"');
ecs_strbuf_appendch(buf, esc[0]);
if (esc[1]) {
ecs_strbuf_appendch(buf, esc[1]);
}
} else {
ecs_strbuf_appendch(buf, ch);
}
} else {
if (!escape) {
ecs_strbuf_appendch(buf, '\\');
ecs_strbuf_appendch(buf, sep[0]);
} else {
ecs_strbuf_appendlit(buf, "\\\\");
flecs_chresc(esc, ch, '\"');
ecs_strbuf_appendch(buf, esc[0]);
if (esc[1]) {
ecs_strbuf_appendch(buf, esc[1]);
}
}
}
}

ecs_strbuf_appendstr(buf, name_ptr);
} else {
ecs_strbuf_appendstrn(buf, name, name_len);
}
Expand Down Expand Up @@ -10593,7 +10610,8 @@ void ecs_get_path_w_sep_buf(
ecs_entity_t child,
const char *sep,
const char *prefix,
ecs_strbuf_t *buf)
ecs_strbuf_t *buf,
bool escape)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(buf != NULL, ECS_INVALID_PARAMETER, NULL);
Expand All @@ -10614,7 +10632,7 @@ void ecs_get_path_w_sep_buf(
}

if (!child || parent != child) {
flecs_path_append(world, parent, child, sep, prefix, buf);
flecs_path_append(world, parent, child, sep, prefix, buf, escape);
} else {
ecs_strbuf_appendstrn(buf, "", 0);
}
Expand All @@ -10631,7 +10649,7 @@ char* ecs_get_path_w_sep(
const char *prefix)
{
ecs_strbuf_t buf = ECS_STRBUF_INIT;
ecs_get_path_w_sep_buf(world, parent, child, sep, prefix, &buf);
ecs_get_path_w_sep_buf(world, parent, child, sep, prefix, &buf, false);
return ecs_strbuf_get(&buf);
}

Expand Down Expand Up @@ -26188,7 +26206,7 @@ void flecs_system_stats_to_json(
{
ecs_strbuf_list_push(reply, "{", ",");
ecs_strbuf_list_appendlit(reply, "\"name\":\"");
ecs_get_path_w_sep_buf(world, 0, system, ".", NULL, reply);
ecs_get_path_w_sep_buf(world, 0, system, ".", NULL, reply, true);
ecs_strbuf_appendch(reply, '"');

bool disabled = ecs_has_id(world, system, EcsDisabled);
Expand Down Expand Up @@ -33139,8 +33157,8 @@ void flecs_term_to_buf(

if (term->second.id & EcsIsEntity) {
if (term->second.id != 0) {
ecs_get_path_w_sep_buf(
world, 0, ECS_TERM_REF_ID(&term->second), ".", NULL, buf);
ecs_get_path_w_sep_buf(world, 0, ECS_TERM_REF_ID(&term->second),
".", NULL, buf, false);
}
} else {
if (term->second.id & EcsIsVariable) {
Expand Down Expand Up @@ -42171,7 +42189,7 @@ void flecs_json_path(
ecs_entity_t e)
{
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, e, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, e, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
}

Expand Down Expand Up @@ -42254,15 +42272,15 @@ void flecs_json_id(
ecs_entity_t first = ecs_pair_first(world, id);
ecs_entity_t second = ecs_pair_second(world, id);
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
ecs_strbuf_appendch(buf, ',');
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
} else {
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
}

Expand All @@ -42279,12 +42297,12 @@ void flecs_json_id_member_fullpath(
ecs_strbuf_appendch(buf, '(');
ecs_entity_t first = ecs_pair_first(world, id);
ecs_entity_t second = ecs_pair_second(world, id);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf, true);
ecs_strbuf_appendch(buf, ',');
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf, true);
ecs_strbuf_appendch(buf, ')');
} else {
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf, true);
}
}

Expand Down Expand Up @@ -42599,13 +42617,13 @@ void flecs_json_serialize_id_str(
ecs_entity_t first = ecs_pair_first(world, id);
ecs_entity_t second = ecs_pair_first(world, id);
ecs_strbuf_appendch(buf, '(');
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf, true);
ecs_strbuf_appendch(buf, ',');
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf, true);
ecs_strbuf_appendch(buf, ')');
} else {
ecs_get_path_w_sep_buf(
world, 0, id & ECS_COMPONENT_MASK, ".", "", buf);
world, 0, id & ECS_COMPONENT_MASK, ".", "", buf, true);
}
ecs_strbuf_appendch(buf, '"');
}
Expand Down Expand Up @@ -45741,7 +45759,7 @@ int flecs_expr_ser_primitive(
if (!e) {
ecs_strbuf_appendlit(str, "#0");
} else {
ecs_get_path_w_sep_buf(world, 0, e, ".", NULL, str);
ecs_get_path_w_sep_buf(world, 0, e, ".", NULL, str, false);
}
break;
}
Expand Down
5 changes: 3 additions & 2 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -7198,7 +7198,8 @@ void ecs_get_path_w_sep_buf(
ecs_entity_t child,
const char *sep,
const char *prefix,
ecs_strbuf_t *buf);
ecs_strbuf_t *buf,
bool escape);

/** Find or create entity from path.
* This operation will find or create an entity from a path, and will create any
Expand Down Expand Up @@ -9999,7 +10000,7 @@ int ecs_value_move_ctor(
ecs_get_path_w_sep(world, 0, child, ".", NULL)

#define ecs_get_path_buf(world, child, buf)\
ecs_get_path_w_sep_buf(world, 0, child, ".", NULL, buf)
ecs_get_path_w_sep_buf(world, 0, child, ".", NULL, buf, false)

#define ecs_new_from_path(world, parent, path)\
ecs_new_from_path_w_sep(world, parent, path, ".", NULL)
Expand Down
3 changes: 2 additions & 1 deletion include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3981,7 +3981,8 @@ void ecs_get_path_w_sep_buf(
ecs_entity_t child,
const char *sep,
const char *prefix,
ecs_strbuf_t *buf);
ecs_strbuf_t *buf,
bool escape);

/** Find or create entity from path.
* This operation will find or create an entity from a path, and will create any
Expand Down
2 changes: 1 addition & 1 deletion include/flecs/addons/flecs_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@
ecs_get_path_w_sep(world, 0, child, ".", NULL)

#define ecs_get_path_buf(world, child, buf)\
ecs_get_path_w_sep_buf(world, 0, child, ".", NULL, buf)
ecs_get_path_w_sep_buf(world, 0, child, ".", NULL, buf, false)

#define ecs_new_from_path(world, parent, path)\
ecs_new_from_path_w_sep(world, parent, path, ".", NULL)
Expand Down
14 changes: 7 additions & 7 deletions src/addons/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ void flecs_json_path(
ecs_entity_t e)
{
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, e, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, e, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
}

Expand Down Expand Up @@ -611,15 +611,15 @@ void flecs_json_id(
ecs_entity_t first = ecs_pair_first(world, id);
ecs_entity_t second = ecs_pair_second(world, id);
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
ecs_strbuf_appendch(buf, ',');
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
} else {
ecs_strbuf_appendch(buf, '"');
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf, true);
ecs_strbuf_appendch(buf, '"');
}

Expand All @@ -636,12 +636,12 @@ void flecs_json_id_member_fullpath(
ecs_strbuf_appendch(buf, '(');
ecs_entity_t first = ecs_pair_first(world, id);
ecs_entity_t second = ecs_pair_second(world, id);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf, true);
ecs_strbuf_appendch(buf, ',');
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf, true);
ecs_strbuf_appendch(buf, ')');
} else {
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, id & ECS_COMPONENT_MASK, ".", "", buf, true);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/addons/json/serialize_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ void flecs_json_serialize_id_str(
ecs_entity_t first = ecs_pair_first(world, id);
ecs_entity_t second = ecs_pair_first(world, id);
ecs_strbuf_appendch(buf, '(');
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, first, ".", "", buf, true);
ecs_strbuf_appendch(buf, ',');
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf);
ecs_get_path_w_sep_buf(world, 0, second, ".", "", buf, true);
ecs_strbuf_appendch(buf, ')');
} else {
ecs_get_path_w_sep_buf(
world, 0, id & ECS_COMPONENT_MASK, ".", "", buf);
world, 0, id & ECS_COMPONENT_MASK, ".", "", buf, true);
}
ecs_strbuf_appendch(buf, '"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/addons/meta/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ int flecs_expr_ser_primitive(
if (!e) {
ecs_strbuf_appendlit(str, "#0");
} else {
ecs_get_path_w_sep_buf(world, 0, e, ".", NULL, str);
ecs_get_path_w_sep_buf(world, 0, e, ".", NULL, str, false);
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/addons/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ void flecs_system_stats_to_json(
{
ecs_strbuf_list_push(reply, "{", ",");
ecs_strbuf_list_appendlit(reply, "\"name\":\"");
ecs_get_path_w_sep_buf(world, 0, system, ".", NULL, reply);
ecs_get_path_w_sep_buf(world, 0, system, ".", NULL, reply, true);
ecs_strbuf_appendch(reply, '"');

bool disabled = ecs_has_id(world, system, EcsDisabled);
Expand Down
8 changes: 4 additions & 4 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -4477,13 +4477,13 @@ void ecs_id_str_buf(
}

ecs_strbuf_appendch(buf, '(');
ecs_get_path_w_sep_buf(world, 0, rel, NULL, NULL, buf);
ecs_get_path_w_sep_buf(world, 0, rel, NULL, NULL, buf, false);
ecs_strbuf_appendch(buf, ',');
ecs_get_path_w_sep_buf(world, 0, obj, NULL, NULL, buf);
ecs_get_path_w_sep_buf(world, 0, obj, NULL, NULL, buf, false);
ecs_strbuf_appendch(buf, ')');
} else {
ecs_entity_t e = id & ECS_COMPONENT_MASK;
ecs_get_path_w_sep_buf(world, 0, e, NULL, NULL, buf);
ecs_get_path_w_sep_buf(world, 0, e, NULL, NULL, buf, false);
}

error:
Expand Down Expand Up @@ -4555,7 +4555,7 @@ char* ecs_entity_str(
ecs_strbuf_t buf = ECS_STRBUF_INIT;
ecs_check(ecs_is_alive(world, entity), ECS_INVALID_PARAMETER, NULL);

ecs_get_path_w_sep_buf(world, 0, entity, 0, "", &buf);
ecs_get_path_w_sep_buf(world, 0, entity, 0, "", &buf, false);

ecs_strbuf_appendlit(&buf, " [");
const ecs_type_t *type = ecs_get_type(world, entity);
Expand Down
Loading

0 comments on commit 3b9278f

Please sign in to comment.