5 ms·
I don't write much C, mostly C++. But one of the things I swear about most when using C is the totally primitive and error-prone string handling. (I realize tha
by lutorm 16y ago
I don't write much C, mostly C++. But one of the things I swear about most when using C is the totally primitive and error-prone string handling. (I realize that C is supposed to be "primitive", but this really is a domain where the pain goes up drastically.)
- pyre 16y agoWhy is C 'supposed to be primitive?' If C was supposed to be so primitive, there wouldn't even be 'str' functions, it would just be memcpy functions and you would be responsible yourself for keeping track of null terminators. C just needs a bolstered set of string-handling functions. Much like C++ has the Boost libraries.
- AndrewMoffat 16y agoGLib supposedly has some pretty solid C base libraries for string handling and other common tasks. http://library.gnome.org/devel/glib/2.28/glib-String-Utility-Functions.html http://library.gnome.org/devel/glib/2.28/glib-String-Utility...
- lutorm 16y agoYeah, by "primitive", I meant "low level". (The strxxx functions aren't part of the language, after all. Like you say, one could write a better, more "object oriented" string library for C.)
- prewett 16y agoC++ has pretty lousy string handling, too. std::string is a step up from char*, but why can't I do std::string(10)? Or str = nErrors + std::string(" errors"); ? Why do I have to go through the hassle of a std::stringstream just because I want to have a number in my string? Parsing stinks, where's the std::string::split() function? Boost has a bunch of string algorithms, but that's design overkill; I don't really need to have the algorithms work on C strings because I won't be using C strings if there were an simple, useful string class like Java's, or Qt's.
- lutorm 16y agoI agree. Maybe I should have added that whenever I get back to C++ from Python, I also swear about the string handling... ;-)
- bioh42_2 16y agoAs yet another C++ developer I have to ask, why is anyone doing a lot of string handling in C++ ?
- slug 16y agoWhy using boost is overkill? You use std::XXX and the STL, do you consider these too? For the two cases you mentioned, boost comes with nice functions to solve them (lexical_cast, which uses stringstream and the boost tokenizer). tokenizer<> tok(s); for(tokenizer<>::iterator beg=tok.begin();beg!=tok.end();++beg){ cout << *beg << "\n"; } It surely beats the C approach of handling pointers my hand, or the ugly interface of strtok / strtoX (X=l,d,etc). At least in C++ you can use the same function for any number type. I see the need of C (or ASM), but with char/string manipulation it loses any battle with any other language, except probably speed.