From 35213dede29a6e17aed50e9f38025eaa90487365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Schr=C3=B6der?= Date: Sat, 17 Oct 2026 20:53:52 +0200 Subject: [PATCH] Add a stringview README --- stringview/README.md | 57 ++++++++++++++++++++++ stringview/include/stringview/stringview.h | 2 + stringview/library.mk | 2 + stringview/src/stringview.c | 2 + stringview/tests/test_stringview.c | 2 + 5 files changed, 65 insertions(+) create mode 100644 stringview/README.md diff --git a/stringview/README.md b/stringview/README.md new file mode 100644 index 0000000..363c5a4 --- /dev/null +++ b/stringview/README.md @@ -0,0 +1,57 @@ +# stringview + +A small, allocation-free library for non-owning views over contiguous byte sequences. +Supports read-only and mutable views, slicing, substring search, comparison, prefix and suffix checks, ASCII whitespace trimming, and stream output without requiring null-terminated strings. + +## Build + +From the repository root: +- release mode: `make stringview` +- debug mode: `make stringview BUILD=debug` +- unit tests: `make test-stringview BUILD=debug` + +The packaged archive and headers are in `dist//stringview/`. + +## Example + +```c +#include "stringview/stringview.h" + +#include + +int main(void) { + char text[] = " hello, world! "; + MutableStringView line = msv_trim(msv_from_cstr(text)); + size_t separator = msv_find(line, sv_from_cstr(", ")); + + if (separator == SV_NPOS) { + fputs("separator not found\n", stderr); + return 1; + } + + MutableStringView greeting = msv_slice(line, 0, separator); + MutableStringView subject = msv_substr(line, separator + 2, SIZE_MAX); + subject.data[0] = 'W'; + + msv_write(stdout, greeting); + fputs(", ", stdout); + msv_write(stdout, subject); + fputc('\n', stdout); + return 0; +} +``` + +Save the above code as `example.c` in the repository root and run: + +```sh +cc -std=c23 -Idist/release/stringview/include example.c \ + dist/release/stringview/lib/libstringview.a -o example +./example +``` + +The program prints `hello, World!`. + +> [!IMPORTANT] +> String views do not own, copy, resize, or free their data. +> The underlying buffer must remain valid for the lifetime of every view, and mutable views require writable storage. +> Lengths and indices are measured in bytes, and views are not necessarily null-terminated; use `sv_write` / `msv_write` rather than passing a view directly to APIs that expect a C string. diff --git a/stringview/include/stringview/stringview.h b/stringview/include/stringview/stringview.h index 02230a8..63fb044 100644 --- a/stringview/include/stringview/stringview.h +++ b/stringview/include/stringview/stringview.h @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Unlicense + /** * @file stringview.h * @brief Non-owning views over contiguous byte sequences. diff --git a/stringview/library.mk b/stringview/library.mk index 9172583..5387f1d 100644 --- a/stringview/library.mk +++ b/stringview/library.mk @@ -1 +1,3 @@ +# SPDX-License-Identifier: Unlicense + LIB_stringview_DEPS := diff --git a/stringview/src/stringview.c b/stringview/src/stringview.c index 2d09254..23b9713 100644 --- a/stringview/src/stringview.c +++ b/stringview/src/stringview.c @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Unlicense + #include #include diff --git a/stringview/tests/test_stringview.c b/stringview/tests/test_stringview.c index 31f0241..842d81e 100644 --- a/stringview/tests/test_stringview.c +++ b/stringview/tests/test_stringview.c @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Unlicense + #include "stringview/stringview.h" #include "test.h"