Files
fx/test/core/ropes.c

85 lines
1.9 KiB
C
Raw Normal View History

2026-03-16 10:35:43 +00:00
#include <fx/core/rope.h>
#include <inttypes.h>
2025-04-11 14:01:47 +01:00
#include <stdio.h>
2026-03-16 10:35:43 +00:00
static void print_rope(const struct fx_rope *rope, int depth)
{
for (int i = 0; i < depth; i++) {
printf(" ");
}
printf("[%x:", rope->r_flags);
2026-03-16 10:35:43 +00:00
(FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CHAR) && printf(" CHAR");
(FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CSTR) && printf(" CSTR");
(FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CSTR_BORROWED)
&& printf(" CSTR_BORROWED");
2026-03-16 10:35:43 +00:00
(FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_CSTR_STATIC)
&& printf(" CSTR_STATIC");
2026-03-16 10:35:43 +00:00
(FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_INT) && printf(" INT");
(FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_UINT) && printf(" UINT");
(FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_COMPOSITE)
&& printf(" COMPOSITE");
2026-03-16 10:35:43 +00:00
(rope->r_flags & FX_ROPE_F_MALLOC) && printf(" MALLOC");
printf("] ");
2026-03-16 10:35:43 +00:00
switch (FX_ROPE_TYPE(rope->r_flags)) {
case FX_ROPE_F_CHAR:
printf("%c", rope->r_v.v_char);
break;
2026-03-16 10:35:43 +00:00
case FX_ROPE_F_CSTR:
case FX_ROPE_F_CSTR_BORROWED:
case FX_ROPE_F_CSTR_STATIC:
printf("%s", rope->r_v.v_cstr.s);
break;
2026-03-16 10:35:43 +00:00
case FX_ROPE_F_INT:
printf("%" PRIdPTR, rope->r_v.v_int);
break;
2026-03-16 10:35:43 +00:00
case FX_ROPE_F_UINT:
printf("%" PRIuPTR, rope->r_v.v_uint);
break;
default:
break;
}
printf("\n");
2026-03-16 10:35:43 +00:00
if (FX_ROPE_TYPE(rope->r_flags) == FX_ROPE_F_COMPOSITE) {
if (rope->r_v.v_composite.r_left) {
print_rope(rope->r_v.v_composite.r_left, depth + 1);
}
if (rope->r_v.v_composite.r_right) {
print_rope(rope->r_v.v_composite.r_right, depth + 1);
}
}
}
2025-04-11 14:01:47 +01:00
int main(void)
{
2026-03-16 10:35:43 +00:00
fx_rope a = FX_ROPE_CHAR('a');
fx_rope b = FX_ROPE_CSTR_STATIC("Hello, world!");
fx_rope c = FX_ROPE_INT(-4096);
fx_rope d = FX_ROPE_UINT(4096);
2025-04-11 14:01:47 +01:00
2026-03-16 10:35:43 +00:00
fx_rope str;
2025-04-11 14:01:47 +01:00
2026-03-16 10:35:43 +00:00
const fx_rope *ropes[] = {
2025-04-11 14:01:47 +01:00
&a,
&b,
&c,
&d,
};
2026-03-16 10:35:43 +00:00
fx_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]);
2025-04-11 14:01:47 +01:00
print_rope(&str, 0);
2025-04-11 14:01:47 +01:00
char cstr[1024];
2026-03-16 10:35:43 +00:00
fx_rope_to_cstr(&str, cstr, sizeof cstr);
fx_rope_destroy(&str);
2025-04-11 14:01:47 +01:00
printf("%s\n", cstr);
return 0;
}