Strlz
Strlz is a library for making strings less annoying in C. I use a library similar to this all the time when I just want to get something done with ease, so I figured I should clean it up and share it.
The string will expand if necessary, but also tries to avoid re-allocating memory if possible. It is designed to be very easy to use but also fast.
Example:
Strlz s = new_strlz("Hello"); // "Hello" strlz_append(&s, " world"); // "Hello world" strlz_prepend(&s, "Well, "); // "Well, Hello world" strlz_add(&s, 4, 123); // "Well123, Hello world" strlz_append_print(&s, " and %i baskets", 8); // "Well123, Hello world and 8 baskets" strlz_remove_from_end(&s, 3); // "Well123, Hello world and 8 bask" strlz_remove_from(&s, 4, 3); // "Well, Hello world and 8 bask" int pos = strlz_find(&s, 4, "ell"); // 7 (found from "Hello", search started from 5th character) Strlz sub = new_strlz_substring(6, 4, &s); // "Hell" printf("String = (%s)\n", sub.data); // String = (Hell) strlz_free(&s); strlz_free(&sub);
Full documentation is in the source file.
This is a "single header library", just include the code and it'll work.
This library is public domain, basically I don't care what you do with it.
WARNING: this isn't thoroughly tested.
Do not use this library for security critical tasks. Strlz is based on my own string library which I use day to day and have fixed any issues I ran into, however this isn't exactly the same code and has some additional features, so some bugs may have slipped in without me noticing. I have tested it somewhat but cannot guarantee that it doesn't have any bugs.
Download
Download source code strlz_lazy_string.c - 23 KB