2022-06-02 22:23:01 +03:00
|
|
|
/* This software is licensed by the MIT License, see LICENSE file */
|
|
|
|
|
/* Copyright © 2022 Gregory Lirent */
|
|
|
|
|
|
|
|
|
|
#include "plug.h"
|
|
|
|
|
|
2022-06-03 19:29:46 +03:00
|
|
|
|
|
|
|
|
static vtype_string string_random(unsigned int n) {
|
|
|
|
|
vtype_string x;
|
|
|
|
|
|
|
|
|
|
if (random_boolean()) {
|
|
|
|
|
x.buffer = random_utf8_cstring(n);
|
|
|
|
|
} else x.buffer = random_ascii_cstring(n);
|
|
|
|
|
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void string_concat_random(vtype_string* x, unsigned int n) {
|
|
|
|
|
char* v;
|
|
|
|
|
|
|
|
|
|
if (random_boolean()) {
|
|
|
|
|
v = random_utf8_cstring(n);
|
|
|
|
|
} else v = random_ascii_cstring(n);
|
|
|
|
|
|
|
|
|
|
string_concat(x, v);
|
|
|
|
|
free(v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void string_info(vtype_string* x) {
|
|
|
|
|
printf("\e[36mString consists of \e[m\e[32m%lu\e[m\e[36m utf8 chars (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", string_size(x), string_nmemb(x));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void string_print(const vtype_string* x, const char* prefix) {
|
|
|
|
|
if (!prefix) puts("\e[36mString content:\e[m\n");
|
|
|
|
|
else printf("\e[36mString %s content:\e[m\n\n", prefix);
|
|
|
|
|
printf("\e[33m%s\e[m\n", x->buffer);
|
|
|
|
|
put_separator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-06-02 22:23:01 +03:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
test_init(argc, argv);
|
2022-06-03 19:29:46 +03:00
|
|
|
vtype_string x;
|
|
|
|
|
|
|
|
|
|
x = string_random(31);
|
|
|
|
|
string_print(&x, "(part 1)");
|
|
|
|
|
|
|
|
|
|
string_concat(&x, '\n');
|
|
|
|
|
string_concat_random(&x, 31);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
void* hack = string_at(&x, 32);
|
|
|
|
|
string_print((void*)&hack, "(part 2)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string_info(&x);
|
|
|
|
|
string_print(&x, "concatenated");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string_free(&x);
|
2022-06-02 22:23:01 +03:00
|
|
|
}
|