4 ms·
Sigh, another string library. And this is the reason why I prefer C++ over C. You don't end up writing another string library for the 300th time. There aren't t
by FooBarWidget 13y ago
Sigh, another string library. And this is the reason why I prefer C++ over C. You don't end up writing another string library for the 300th time. There aren't that many differences between string libraries anyway. Most of them are just structs with a pointer to a memory block, plus a length field.
- antirez 13y agoYep, but this one has not the usual layout of struct+pointer (that's the whole point), and I'm not sure it qualifies as "yet another" since it was written in 2006.
- FooBarWidget 13y agoYou're right, sorry. I only scanned the document, saw the struct, and closed the tab. The way you use a header before the data is similar to how Ruby represents its strings internally.
- pacaro 13y agoIt appears to me to be "yet another" because the length before the string approach is usually referred to as a b-string. windows (for example) has had a comprehensive b-string library (type is called BSTR) for about 20 years - due to it's age and provenance it has the downside of thinking a character is a 16-bit value...
- shadowfox 13y agoJust to add a reference to the parent: http://www.johndcook.com/cplusplus_strings.html http://www.johndcook.com/cplusplus_strings.html (The article is mildly interesting by itself)
- pekk 13y agoYes, many people who have written much C have written some version of this library.
- jhallenworld 13y agoI'm not so happy with C++ string either. Main complaint is that I like the use of C strings as "semi-predicates"- you can test for NULL to indicate a failure. I wrote my own String class at one point to provide this feature: // These provide test if assigned/not assigned inline operator void*() const { return (void *)s; } inline bool operator!() const { if(s) return 0; else return 1; }
- dllthomas 13y agoBetter not to pun, and use an actual optional where you want it...
- jhallenworld 13y agoEh, I like my C idioms. With C++ it's easy to make it safe so that accessing an unassigned string doesn't cause a crash (make NULL equivalent to empty strings except for tests). I have the same gripe with the way the STL is designed. Too tedious to test for empty first before reading an item.
- deleted 13y ago[deleted]
- pjmlp 13y agoNot only that. You also have x vector libraries for helping with bound checking in secure sensitive code and macro based generic data structures. No thanks.
- munificent 13y ago> And this is the reason why I prefer C++ over C. You don't end up writing another string library for the 300th time. Ironically, in almost all of my uses of C++, I did end up writing or using a custom string library there too. (I was doing mostly console games or language interpreters.)
- geminitojanus 13y ago> Sigh, another string library. And this is the reason why I prefer C++ over C. You don't end up writing another string library for the 300th time. You say that, and yet every C++ project I've ever touched in my life has had its own string class with various levels of horror attached. My favorite was the one that stored everything internally as 32-bit characters to be Unicode safe, and was never used in a codebase that had to deal with Unicode.