/* Example: char * dynamic strings (gds_t), standard dynamic string type */
/* Part of genvector examples (http://repo.hu/projects/genvector) */
#include <stdio.h>
#include <genvector/gds_char.h>

/* print the string with a prefix */
static void dump(const char *prefix, gds_t *s)
{
	if (s->array == NULL)
		printf("%-16s<NULL>\n", prefix);
	else
		printf("%-16s'%s'\n", prefix, s->array);
}

int main()
{
	int n;
	gds_t s, s2;

	/* Initialize the string (to empty, which means the array is not allocated) */
	gds_init(&s);
	dump("empty: ", &s);


	/* append a..j, char by char */
	for(n = 0; n < 10; n++)
		gds_append(&s, 'a'+n);
	dump("append: ", &s);

	/* append a char * static string */
	gds_append_str(&s, ", hah, ");
	dump("append_str: ", &s);

	/* truncate after 8 characters */
	gds_truncate(&s, 8);
	dump("truncate: ", &s);

	/* Concat two strings: sort of "s += s2" */
	gds_init(&s2);
	gds_append_str(&s2, ", hello");
	gds_concat(&s, &s2);
	dump("concat: ", &s);

	/* Array is still just a char * string */
	printf("strchr:         '%s'\n", strchr(s.array, 'e'));

	/* It can be read or written as a char * array, within bounds */
	s.array[5] = '!';
	dump("array asg: ", &s);

	/* Remove a mid-section of the string, 3 characters starting from [9] */
	gds_remove(&s, 9, 3);
	dump("remove: ", &s);

	/* Free memory */
	gds_uninit(&s);
	gds_uninit(&s2);

	return 0;
}
