21 ms·
If you are using C and do some non-trivial work with strings you should either use a good library to handle strings or build your own. It is not that difficult
by stephc_int13 4y ago
If you are using C and do some non-trivial work with strings you should either use a good library to handle strings or build your own.
It is not that difficult in practice.
The old C std lib is, in my opinion, outdated, obsolete and a very bad fit for complex string handling, especially on the memory management side.
In my own framework, the string management module is using a dedicated memory allocator and a "high level" string API with full UTF8 support from the start.
As a general rule, I think that the C std lib is the weakest part of the C language and it should only be used as a fallback.
- tyingq 4y agoFair, though there's always the crossover point where you need to interact with the OS, 3rd party libraries, protocols, etc. It's not difficult to miss a spot where your utf8 string gets mangled, truncated, etc.
- stephc_int13 4y agoYes, do not trust the OS, use the minimal API surface, and build your own toolkit, you won't have to do it often.
- maxloh 4y agoIs your framework open sourced?
- stephc_int13 4y agoNot 100% decided yet, but it is very probable that I will open source it.
- attractivechaos 4y ago> especially on the memory management side. Libc string functions don't manage memory. They can be used no matter where your strings are stored. It is more of a choice between generality vs convenience in common cases.
- nicoburns 4y agoA lot of them require the string to be null terminated rather than taking a length.
- stephc_int13 4y agoI think this is the main culprit of the libc string functions, you have to provide buffers to store results, and the responsibility of managing those individually can be annoying, and bug prone, resulting in vulnerabilities. Passing an allocator (like Zig) or a container (like in my framework) to anything that needs to allocate some memory to store a result is both explicit, low overhead and quite convenient in practice.
- bandrami 4y agoNote that they're annoying, bug prone, and vulnerable in 3rd-party memory managed libraries, too, but you can just say it's someone else's problem then.
- skitter 4y agoYou cannot e.g. store a string as a slice of another string (unless the slice reaches the end).
- marssaxman 4y ago> the C std lib is the weakest part of the C language and it should only be used as a fallback. I've been musing for a while now: what would it look like if we were to discard the C library and design a new one, leaving the language itself intact?
- stephc_int13 4y agoI think it could be very nice. C is not perfect, there are some parts of the syntax that I strongly dislike, like casting or function pointers declaration... But it is overall a good enough syntax, much simpler than C++.
- marssaxman 4y agoAmending the syntax is fun but rapidly becomes a slippery slope; soon enough you find yourself designing a new successor language, as has been done many times before. Simply scrapping the mostly-unhelpful C stdlib and inventing new, modern abstractions for allocation, IO, text, threading, etc seems like a more tractable problem.
- stephc_int13 4y agoI fully agree.
- int_19h 4y agoIt has the same fundamental problem, though: you have to rewrite most existing code, which hinders adoption. In this case, it might actually hinder it more than also improving the language itself, since people would be more willing to take that leap if there are more benefits to be had from it.
- up2isomorphism 4y agoStdlib is probably the most successful library in the history, not sure how it is “unhelpful”.
- klodolph 4y ago
- m463 4y ago> The old C std lib is, in my opinion, outdated, obsolete ...and has been since most of us ever used C. I think one of the major failings of C was the lack of a good standard library that updated with the times. Actually, I believe a rich standard toolbox was one of the best features of python, and helped with its success.
- pjmlp 4y agoWhich is why most applications ping back into POSIX when available, not that fixes the security issues with the standard library.
- ninkendo 4y agoWhich parts of posix? What can I #include in a posix environment to get better string handling in C? (Or maybe I’m misinterpreting your comment?)
- pjmlp 4y agoYou are misinterpreting my comment. First part of my comment relates to C library in general, second part of my comment refers to strings and arrays, even if not explicitly.
- 1vuio0pswjnm7 4y ago"If you are using C and do some non-trivial work with strings you should either use a good library to handle strings or build your own." unsigned int str_len(const char *s) { register const char *t; t = s; for (;;) { if (!*t) return t - s; ++t; if (!*t) return t - s; ++t; if (!*t) return t - s; ++t; if (!*t) return t - s; ++t; } } I still use this instead of stdlib strlen. Of course I also use software everyday that I know uses stdlib strlen. For most C programs dealing with strings I just use flex and yyleng, which in turn uses the stdlib strlen. Using flex for small jobs is overkill but it's quick and convenient. I am a hobbyist programmer; I write so-called "trivial" programs. That said, this exact function is used in some "non-trivial" software written by someone else and that person is IMHO a better C programmer than any HN commenter I have seen, most of whom do not let the public see the code they write anyway. Go figure. NB. I am not the author; this is in the public domain. The author is djb.
- stephc_int13 4y agoI think this naming style should be considered obsolete. - This function will return the number of bytes, not of characters or codepoints. - str and len are both abbreviations, we should use full words when possible - We can also be more explicit about what the function does, it does not simply returns the string length, it counts characters (or bytes in this case) Here is how I would name it: u32 CountBytesInString(char* string); u32 CountCharactersInString(char* string); And on the implementation side, this work can be done with SIMD instructions, and be really freaking fast, but still, it should be explicit for the user that the work is O(n) complexity, not exactly free.
- 1vuio0pswjnm7 4y ago"- str and len are both abbreviations, we should use full words when possible -" u32 and char are abbreviations.
- stephc_int13 4y agoYes, this is true. And this is a tradeoff, I think that basic types are so widely used that we can use this style of abbreviation without much ambiguity. strlen is also pretty unambiguous, but I still have to check what strstr means.
- faxmeyourcode 4y agoDo you have any recommendations for good open source C standard library replacements instead of rolling your own string manipulation functions?
- gaws 4y ago> use a good library Such as?