r/C_Programming • u/Elifire12 • Sep 07 '26
Article I Made a Simple String Library
I wrote an article about this as well. Here it is.
There I explain why I do not like NUL-terminated strings and how I implemented my own simple string library in C.
If you have some spare time I would really appreciate some feedback on the article and the library.
The code is sitting on a codeberg repository.
Also, tell me what you think about C-style strings. Do you like them? Do you use them, or do you also tend to roll your own pointer + length structs?
8
Upvotes
•
u/AutoModerator Sep 07 '26
Hi /u/Elifire12,
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.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
20
u/Dieriba Sep 08 '26
Even though you hate NULL-terminated, you should have a function that allows to return a pointer to a NULL-terminated strings, as many libc function expect those kind of strings ( if you plan to link your binary with libc which I think we’ll be the case).
Another things is your structure find_out that you use for your find functions. I’d use ˋusizeˋonly as return value instead this structure and have a special value of it meaning not found, like with std::string::npos used as not found value in c++ std::string.
8
u/mjmvideos Sep 08 '26 edited Sep 08 '26
First thing I did was go look at your code base for your header file to see your interfaces. But there isn’t one!? Then I looked at your test routine and found that you are including “string.c”!? Really??
3
u/aalmkainzi Sep 08 '26
Nice. I also have a string library. Probably a lot more complicated than yours, but adds a ton of functionality. I use it in basically all my projects.
1
u/runningOverA Sep 08 '26
You need to extend the standard library more until you can do this ...
string str_replace(string input, string find, string replace);
don't jump to it. it will come naturally. And by that time your library will change a lot as you face problems.
1
u/didntplaymysummercar Sep 08 '26
The first problem section could mention the memory traffic too, to cache or even from swap, when going through a string that might be rarely used.
The bit about
charnot being 8 bits is pointless and pedantic. If your platform doesn't handle strings with char then it's hosed already. It's also in extremely big letters on mobile for some reason.str_owned_freecould be macro to call a helper function with pointer to that struct and set it to null instead of leaving it dangling.LENGTHOFcould be made to reject non literals (by sizes 4 and 8 and those would need another macro or extra argument to confirm it's safe) using a helper function for extra safety, now it accepts any char pointer and does the wrong thing. Or at least recommend-Wsizeof-pointer-divcompiler flag (it's enabled by-Wall) above it to catch that. That macro (andSTR) are also very generic names to use in a library.You're also not using
memcmpin your equality functions, I'd get it if you're avoidinglibcor even juststring.hbut you're usingmemcpyelsewhere already.str_find_substris a very inefficient string search algorithm, plus it doesn't have an early bailout length check for needle bigger then haystack.Using unsigned integers can impede some optimization since compiler can't assume no overflows. Also you use
i32in slices instead ofi64orisizeor something instr_sliceso that code breaks on 64 bit sizes.1
u/ByronScottJones Sep 08 '26
So you've reinvented Pascal strings?
1
u/Last-Employ-3422 Sep 08 '26
Why does the slice have a start and an end?
2
u/[deleted] Sep 08 '26
[removed] — view removed comment