#include #include #include static void print_rope(const struct b_rope *rope, int depth) { for (int i = 0; i < depth; i++) { printf(" "); } printf("[%x:", rope->r_flags); (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CHAR) && printf(" CHAR"); (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR) && printf(" CSTR"); (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR_BORROWED) && printf(" CSTR_BORROWED"); (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_CSTR_STATIC) && printf(" CSTR_STATIC"); (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_INT) && printf(" INT"); (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_UINT) && printf(" UINT"); (B_ROPE_TYPE(rope->r_flags) == B_ROPE_F_COMPOSITE) && printf(" COMPOSITE"); (rope->r_flags & B_ROPE_F_MALLOC) && printf(" MALLOC"); printf("] "); switch (B_ROPE_TYPE(rope->r_flags)) { case B_ROPE_F_CHAR: printf("%c", rope->r_v.v_char); break; case B_ROPE_F_CSTR: case B_ROPE_F_CSTR_BORROWED: case B_ROPE_F_CSTR_STATIC: printf("%s", rope->r_v.v_cstr.s); break; case B_ROPE_F_INT: printf("%" PRIdPTR, rope->r_v.v_int); break; case B_ROPE_F_UINT: printf("%" PRIuPTR, rope->r_v.v_uint); break; default: break; } printf("\n"); if (B_ROPE_TYPE(rope->r_flags) == B_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); } } } int main(void) { b_rope a = B_ROPE_CHAR('a'); b_rope b = B_ROPE_CSTR_STATIC("Hello, world!"); b_rope c = B_ROPE_INT(-4096); b_rope d = B_ROPE_UINT(4096); b_rope str; const b_rope *ropes[] = { &a, &b, &c, &d, }; b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]); print_rope(&str, 0); char cstr[1024]; b_rope_to_cstr(&str, cstr, sizeof cstr); b_rope_destroy(&str); printf("%s\n", cstr); return 0; }