cstring
Small standalone C library that provides extensible C-style strings and extensible arrays of those strings, for Unix and Windows.
Table of Contents
Introduction
cstring provides one resizeable string, cstring_t, and a vector of those strings, cstring_vector_t. A cstring_t is always a length, a pointer, a capacity, and flags. The flags select a memory contract. An owned growable string is the default. Fixed, borrowed, auto-buffer, and readonly are the other contracts. Which heap owns the memory is a separate choice: realloc by default, and the Windows heaps where those flags exist.
The C API has no non-standard dependencies. Building tests requires STLSoft and xTests (and may optionally recognise shwild).
Usage modes
Ordinary code uses the default contract and never sets a flag. The other contracts exist so a caller can cap growth, write into a buffer they already have, or select a Windows heap, without a second string type.
Default use
#include <cstring/cstring.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
cstring_t cs;
CSTRING_RC rc = cstring_create(&cs, "Hello");
if (CSTRING_RC_SUCCESS != rc)
{
return EXIT_FAILURE;
}
printf("%s\n", cs.ptr);
cstring_destroy(&cs);
return EXIT_SUCCESS;
}
Storage contract
Pass memory flags to cstring_createEx() or cstring_createLenEx(). For a borrowed buffer, arena is that buffer and capacity is its size. With no memory flags, those parameters are ignored and the string is an owned heap allocation.
| Mode |
Flags |
Familiar form |
If it cannot grow |
| Owned, growable |
(none) |
A heap std::string, or Rust String |
CSTRING_RC_OUTOFMEMORY |
| Owned, fixed |
CSTRING_F_MEMORY_IS_FIXED |
That same owned string, with a hard ceiling |
CSTRING_RC_EXCEEDFIXEDCAPACITY |
| Borrowed |
CSTRING_F_MEMORY_IS_BORROWED (implies fixed) |
A caller-owned char buf[N], writable up to N |
CSTRING_RC_EXCEEDBORROWEDCAPACITY |
| Auto-buffer |
CSTRING_F_MEMORY_IS_BORROWED | CSTRING_F_MEMORY_CAN_GROW_TO_HEAP |
stlsoft::auto_buffer / llvm::SmallString |
Spills to the heap, then stays there |
| Readonly |
CSTRING_F_MEMORY_IS_READONLY |
A frozen instance; with borrowed, std::string_view or Rust &str |
CSTRING_RC_READONLY |
std::string SSO keeps a small buffer inside the object; cstring's auto-buffer uses a buffer you supply, of a size you choose, and cstring_t stays four fields; after a spill the instance stays on the heap (Rust's standard String has no SSO).
Allocators
The arena flags apply to memory the library owns: the default heap, a fixed owned buffer, and the heap side of an auto-buffer.
| Arena |
Flag |
Where |
realloc |
CSTRING_F_USE_REALLOC (the default) |
Unix and Windows |
| Win32 global memory |
CSTRING_F_USE_WINDOWS_GLOBAL_MEMORY |
Windows |
| Process heap |
CSTRING_F_USE_WINDOWS_PROCESSHEAP_MEMORY |
Windows |
| COM task allocator |
CSTRING_F_USE_WINDOWS_COM_TASK_MEMORY |
Windows |
A few further rules:
cstring_init() stores cstring_t_DEFAULT. That instance does not need cstring_destroy(). Every cstring_create* does;
cstring_yield2() hands back an owned payload. Borrowed and readonly instances refuse it. A Windows DLL built on realloc returns CSTRING_RC_CANNOTYIELDFROMSO;
- The character type is
char unless CSTRING_USE_WIDE_STRINGS is set (normally both UNICODE and _UNICODE on Windows). CSTRING_NO_USE_WIDE_STRINGS forces char. That choice is made at compile time. prepare_cmake.sh --wide-strings sets the CMake option CSTRING_USE_WIDE_STRINGS, which defines the macro on the library and everything that links it. Examples and tests include cstring.helpers.h for CSTRING_T_(), CSTRING_STRCMP_(), and CSTRING_STRNCMP_(). That header is not part of the library contract. Print a payload with cstring_write() / cstring_writeline();
- Custom arenas (
CSTRING_F_USE_CUSTOMARENAFUNCTIONS) are declared and return CSTRING_RC_CUSTOMARENANOTSUPPORTED. CSTRING_F_MEMORY_IS_OFFSET is set by the implementation and is not a client mode.
Installation
Detailed instructions - via CMake, via bundling - are provided in the accompanying INSTALL.md file.
Components
Types
The C API is based around two structures:
cstring_t, which represents a resizeable string instance; andstruct cstring_t
{
size_t len; /*!< Number of characters. */
cstring_char_t* ptr; /*!< Pointer to the string. If capacity is 0, the value
of this member is undetermined. */
size_t capacity; /*!< Number of bytes available. */
cstring_flags_t flags; /*!< Flags. This field belongs to the implementation,
and must not be modified by any application code. */
};
cstring_vector_t, which represents a sequence of cstring_t instances;struct cstring_vector_t
{
size_t len; /*!< Number of strings. */
cstring_t* ptr; /*!< Pointer to the first string. If capacity is 0, the
value of this member is undetermined. */
size_t capacity; /*!< Number of instances available. */
cstring_flags_t flags; /*!< Flags. This field belongs to the implementation,
and must not be modified by any application code. */
};
Supporting scalar typedefs:
cstring_char_t — character type (char, or wchar_t when CSTRING_USE_WIDE_STRINGS is defined);
cstring_flags_t — bit flags controlling allocation and capacity semantics;
cstring_hash_t — 64-bit unsigned integer type (uint64_t) representing hash values;
Constants
CSTRING_VER — the composite library version;
cstring_t_DEFAULT — { 0, NULL, 0, 0 }, an uninitialised cstring_t. cstring_init() assigns this;
cstring_vector_t_DEFAULT — the same shape for a cstring_vector_t;
cstring_vector_DEFAULT_CAPACITY — sentinel (~(size_t)0) passed to creators so the implementation chooses the capacity;
CSTRING_FROM_END(x) — reverse index for cstring_insert(), cstring_insertLen(), cstring_replace(), and cstring_replaceLen();
CSTRING_HASH_DJB2_SEED — djb2 initial seed (5381); also the hash of an empty input;
CSTRING_HASH_FNV1A_OFFSET — FNV-1a 64-bit offset basis (0xcbf29ce484222325ULL); also the hash of an empty input;
CSTRING_HASH_FNV1A_PRIME — FNV-1a 64-bit prime (0x100000001b3ULL);
CSTRING_HASH_SDBM_MULTIPLIER — sdbm multiplier (65599);
CSTRING_HASH_SDBM_SEED — sdbm initial seed (0); also the hash of an empty input;
String API
Defined in cstring/cstring.h:
Status and capacity
cstring_getStatusCodeString() — returns a NUL-terminated description of a CSTRING_RC code;
cstring_getStatusCodeStringLength() — returns the length of that description, or 0 if the code is not recognised;
cstring_setCapacity() — adjusts capacity (subject to fixed / borrowed / readonly rules);
cstring_yield2() — yields ownership of the payload (and raw buffer) to the caller;
Creation/destruction functions
cstring_init() — initialises an instance to default values (and does not require a following call to cstring_destroy());
cstring_create() — creates an instance from a C-style string;
cstring_createLen() — creates an instance from a (portion of a) C-style string;
cstring_createN() — creates an instance from a number of repetitions of a character value;
cstring_createEx() — creates an instance with special characteristics (borrowed buffer, allocator flags, …);
cstring_createLenEx() — as cstring_createEx(), from a fixed number of characters;
cstring_destroy() — releases resources and resets the instance;
Modification functions
cstring_assign() — assigns a C-style string (may reallocate);
cstring_assignLen() — assigns a fixed character count (embedded NULs allowed);
cstring_copy() — copies one cstring_t into another;
cstring_append() — appends a C-style string;
cstring_appendLen() — appends a fixed character count;
cstring_insert() — inserts a C-style string at an index (CSTRING_FROM_END supported);
cstring_insertLen() — inserts a fixed character count at an index;
cstring_replace() — replaces a section at an index with a C-style string;
cstring_replaceLen() — replaces a section at an index with a fixed character count;
cstring_replaceAll() — replaces all occurrences of one substring with another;
cstring_truncate() — shortens the logical length (capacity unchanged);
cstring_swap() — swaps the contents of two instances;
Comparison functions
cstring_equal() — non-zero when two strings hold the same code units for len. A NULL pointer and a zero length are empty. capacity and flags are ignored;
cstring_compare() — negative, zero, or positive order of those same code units. Test equality with cstring_equal();
- C++
operator== and operator!= call cstring_equal(); operator< calls cstring_compare();
File functions
cstring_readline() — reads a line of text from the given text stream into the instance;
cstring_write() — writes the string to the given text stream;
cstring_writeline() — writes the string followed by a newline to the given text stream;
Hashing functions
Declared in cstring/hash.h, which cstring.h includes. The three algorithms return cstring_hash_t (uint64_t). Multibyte and wide entry points exist in every build, whatever ambient cstring_char_t is. A multibyte code unit contributes its one octet. A wide code unit contributes every octet of the wchar_t, low byte first, so "a" and L"a" differ. The wide value depends on sizeof(wchar_t): two octets on Windows, four on Unix. Big-endian and little-endian hosts of the same width agree. A NULL pointer, or a zero length, yields that algorithm's empty-input value and does not read the pointer. The numeric tables below are the multibyte results.
| Suffix |
Input |
What is hashed |
| (none) |
cstring_t const* |
ptr for len code units, including embedded NULs |
_mbs |
char const* |
stops at the first NUL |