A simple string building API for C
- Size
- 35K (35,466 bytes)
- Added
- 2026-11-20 02:42
- Downloads
- 2
- Long Description
- About Vstring is a simple string building API for the C programming language. The API does not make any thread-safety guarantees; sharing vstrings between threads requires some form of synchronization. (At some point, I may make a concurrent string API, but that's neither here nor there.) Vstring supports static and dynamic buffers (static buffers are upgraded to dynamic buffers as needed), and can efficiently parse signed and unsigned integers. The API is intended to promote safety in string manipulation as well as to provide a means to avoid hugely expensive printf-family calls for common operations. API A vstring type is defined by the API; this type contains all necessary information / metadata for modifying the underlying buffer. The type is not opaque (because opaque types suck), but it is not recommended to play with the type outside of the API. (If you find the need to do this, fix / extend the API and send a pull request!) Compiling Simply #include <vstring.h>. If you use vs_pushdouble, you may need to link in the system's math library if that is not part of libc. vstring The vstring type is defined as follows: typedef struct vstring { char *contents; uint32_t type; uint32_t flags; uint64_t pointer; uint64_t size; } vstring; - The contents pointer represents the underlying buffer. - The type member is a bitmap containing information about the type of the vstring instance. - The flags member is a bitmap containing metadata about the vstring instance. These flags are intended for API internal use and should not be relied upon by consumers of the API. - The pointer member is used as an offset into the contents buffer. It points to the end of the string + 1 byte. - The size member contains the total capacity of the contents buffer. Types Three sorts of vstrings exist: * Static strings (VS_TYPE_STATIC): these strings are backed by a static buffer and cannot grow. * Growable static strings (VS_TYPE_GROWABLE): these strings are backed by a static buffer, but may be upgraded to dynamic strings if an append operation would cause an overflow. * Dynamic strings (VS_TYPE_DYNAMIC): these strings are backed by a dynamically allocated buffer and may grow if an append option would cause an overflow. Allocation typedef struct vstring_malloc { void *(*vs_malloc)(size_t); void *(*vs_realloc)(void *, size_t); void (*vs_free)(void *); } vstring_malloc; The vstring_malloc type provides a means for using a custom memory allocator that may not be accessible through the malloc API linked into the program. Initialization static inline vstring * vs_init(vstring *vs, vstring_malloc *vm, enum vstring_type type, char *buf, size_t size) A vstring is initialized by calling vs_init with the proper arguments. If the first argument is NULL, the vstring itself will be dynamically allocated. It is legal to pass a pointer to a statically allocated vstring of type VS_TYPE_DYNAMIC. The vstring_malloc argument, if non-NULL, provides pointers to a set of malloc, realloc, and free-compatible functions that are used to allocate the vstring (if needed) and its underlying buffer. If this argument is NULL, the library uses calloc, realloc, and free directly. The buf and size arguments are useful when vs_init is called to initialize a VS_TYPE_STATIC or VS_TYPE_GROWABLE vstring, but also allow passing in an externally allocated buffer with a known size into a vstring of type VS_TYPE_DYNAMIC. Note that such a buffer must have been allocated with the same malloc available to vstring (and must therefore be the same allocator passed through vstring_malloc, if any). Destruction static inline void vs_deinit(vstring