r/C_Programming • u/Hoxitron • Oct 21 '26
A string library
Some time ago I was inspired by the string view library to focus on simpler things and do some recreational programming of my own. I had written very little C before, but spent a lot of time reading it, mostly the miniaudio source code, which I think still helped a lot.
This is a relatively small one, mostly practice for the next library in this repo. But I would really appreciate some feedback or review on my code.
There are more functions available compared to sv, plus a string type that owns it and can modify it (for now just appending), plus iterators over words, lines, or a delimiter. I tried to make everything as ergonomic as I could.
At the core of the library is also the two way string matching (Crochemore-Perrin) algorithm taken from the rust std library. I did include an explanation into how it works, but mine just follows a practical example. I found theoretical explanations to be too confusing (at least for my brain).
I don't think I am done with the algorithms. I think it may be useful to use different ones depending on the size of the haystack or needle. Plus, it seems like a good place to mess around with SIMD. However, I have no clue how to benchmark in C.
The library is C89 compatible for no other reason that I find it fun and challenging.
•
u/github-guard Oct 21 '26
⚠️ GitHub Guard: Trust Warning
One or more repositories scored below the threshold (3), but the post was not removed.
Repository: tsoding/sv
Score: 4/6
Repository: someotherself/cvr_libs
Score: 1/6
1
u/AutoModerator Oct 21 '26
Hi /u/Hoxitron,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
You can read more at https://www.reddit.com/r/C_Programming/wiki/index/posting-projects
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Salat_Leaf Oct 22 '26
Stringzilla is also a good reference for string functions implementations, so you might wanna check it out
2
u/rubidus-api Oct 22 '26
Nice work. I like that you pair the view type with an owning string you can modify. I've been working on something similar. This sub is strict about posts with links, so I'll leave mine out.
I think one of the root causes behind the need for things like string_view is C's traditional NUL-terminated string.
strtok() shows the limits of that design well:
By today's standards it's an awkward design.
Buffer overruns are partly related too. A NUL-terminated string doesn't store its length, so finding the length means scanning the whole string, every time. When copying or modifying, you have to account for the terminator; forget it or miscompute a buffer size and you have a memory-safety bug. The terminator isn't the only cause, of course, but APIs that don't track length and capacity explicitly clearly make things riskier.
The alternative I favor is to use (pointer, length) slices wherever possible. A string_view-style type can refer to part of a string without copying it, cuts allocations while parsing, and never needs the length recomputed. That's a real win for both memory management and performance.
The idea isn't new. Pascal dialects like UCSD and Turbo Pascal stored the length with the string, and non-owning references followed in Google's StringPiece, LLVM's StringRef and C++17's std::string_view. I think the C ecosystem should move further in this direction.
Of course, adding one more string type doesn't solve it:
To try this out myself, I built a base library with AI assistance, starting from allocators and covering much of what the C library provides. It's designed around pointer and length for strings and buffers, and I use it in several of my own projects, which I try to write in plain C on top of it with as few outside dependencies as possible.
In practice it shines for parsing: fewer copies and allocations, and you can work with pieces of the input without turning each one into its own string.
At the boundary with existing libraries and OS APIs, though, you still have to produce NUL-terminated strings, and on Windows the OS boundary is UTF-16, which adds another conversion. In fact, my more Win32-heavy projects tend to use the view type less. Those boundaries cost copies and extra work, and you have to manage the mismatch between the two styles. So improving one library isn't enough; what matters is for length-based strings to flow naturally across the whole ecosystem.