string-builder: Ajoute un paquet pour construire des char*

This commit is contained in:
2022-11-28 11:40:21 +01:00
parent cb6d978cad
commit 27eafc7b9c
5 changed files with 137 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "builder.h"
int main(void) {
char *result = tmp_end();
assert(*result == '\0');
tmp_append_char('a');
assert(strcmp(result, "a") == 0);
for (char c = 'b'; c <= 'z'; ++c) {
tmp_append_char(c);
}
char *end = tmp_append_char('\0');
assert(*end == '\0');
assert(strcmp(result, "abcdefghijklmnopqrstuvwxyz") == 0);
tmp_clean();
result = tmp_end();
tmp_append_cstr("Hello,");
char *comma = tmp_append_cstr(" world!");
tmp_append_char('\0');
assert(strcmp(comma, " world!") == 0);
assert(strcmp(result, "Hello, world!") == 0);
tmp_rewind(comma);
tmp_append_cstr(" the world!");
tmp_append_char('\0');
assert(strcmp(result, "Hello, the world!") == 0);
printf("Success!\n");
return 0;
}