A C library that provides a better string that resizes its size automatically.
Simply download better_string.h and put it in your source code.
The project is licensed under the MIT license.
Or, to quickly test it out, we have a simple test program:
gcc test.c
./a.outMake sure you have included the downloaded header!
To create a new string:
string* my_str = str_init();
if (my_str == NULL) puts("alloc failed");
str_add(my_str, "Hello World!");You will often need to turn a string into a C string.
For this case, you can use the c_str() function:
puts(c_str(my_str));A string is simply a struct with an integer holding its size and a C string with the raw data.
After you are finished working with your string, you MUST use the
str_free() function to free allocated memory:
str_free(my_str);This automatically frees my_str->data and my_str.
This is what str_add uses under the hood. This function simply
resizes your string with realloc, moves the null terminator and
puts the character before the null terminator.
str_addc(my_string, 'h');Turns a C string into a string. (why are we calling these "the" strings, again?)
const char *path = getenv("PATH");
string* new_string = c_str_to_str(path);Turns a character into a string.
char ch = (char)getchar();
string* str_from_char = char_to_str(ch);Just runs strlen() from <string.h> on string->data.