Language: English | 简体中文
English version translation was generated by AI.
Provides the strview type for string manipulation.
Supports C99 and C++11 or later.
- Header-only.
- Read-only string view type
strview, does not affect source data. - Does not require
<stdlib.h>or<string.h>, no dynamic memory allocation. - Operates on the provided arguments only; no global state.
- Provides common string operations: creation, slicing, trimming, searching, comparing, counting, etc.
#define LIB_STV_IMPL // Define this macro to enable the implementation code
#include "stv.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
strview myview = stv_new("This is a example string: Hello, world!");
myview = stv_slice(myview, stv_lastChar(myview, 'H', false), stv_end);
printf("myview: " stv_PFFMT "\n", stv_PFARG(myview)); // Hello, world!
return 0;
}Just copy stv.h into your project and include it.
Then define LIB_STV_IMPL before #include in one source file (e.g., stv.c):
// main.c
#include "stv.h" // Declarations only
// stv.c
#define LIB_STV_IMPL // Define the macro before include to get the implementation
#include "stv.h"Or define LIB_STV_STATIC_INLINE_IMPL before each inclusion to make all functions static inline.
Warning
strview is a read-only view, containing only a pointer to the source string and a length, and does not own the data.
This means you must ensure the source string's lifetime exceeds that of the view.
A quick overview by category.
For detailed parameter descriptions, return values and examples, please see the API Reference.
| Category | Key Functions / Macros |
|---|---|
| Creation | stv_new, stv_create, stv_literal, stv_makestv, stv_nullstv |
| Slicing | stv_slice, stv_removeStart, stv_removeEnd, stv_removePrefix, stv_removeSuffix |
| Splitting | stv_split, stv_splitLines, stv_splitWords, stv_beforeFirstDelim, stv_afterLastDelim |
| Trimming | stv_trim, stv_trimStart, stv_trimEnd, stv_whitespace |
| Searching | stv_search, stv_rev_search, stv_firstIndex, stv_lastIndex, stv_firstDiff, stv_lastDiff |
| Comparison | stv_compare, stv_equal, stv_same, stv_startsWith, stv_endsWith, stv_contains, stv_empty |
| Counting / Predicates | stv_count, stv_every, stv_some |
| Utilities | stv_front, stv_back, stv_forEach, stv_swap, stv_hash, stv_PFARG, stv_PFFMT |
| C string conversion | stv_cstr, stv_opt_cstr, stv_opt_join |
| Numeric parsing | stv_parseInum, stv_parseUnum |
Note
Some API are _Generic macros and are only available with C11 or later;
In C99 should be use specific functions instead (e.g., stv_trim -> stv_trimIf, stv_lastIndex -> stv_lastChar).
MIT © 2026 Akarin akarin@icatcp.work
Full license text in the LICENSE file.