I moved from C++ to C and I don't miss std::string. Quite the opposite, I don't feel
comfortable using std::string/std::wstring anymore even in C++.
One problem of std::string tries to do two different jobs at the same time
using the same object. One job is "string builder" and a the other "string holder"
Using std::string as string holder is a waste of memory. For instance:
std::map<std::string, something>
Map doesn't want build string, it just need to hold a string.
Another bad thing about std::string is to have to use .c_str() to make
it compatible with C strings. Clearly std::string is not a built-in
language string. "literals" are not std::string.
void F1(std::string s);
void F2(const std::string& s);
F("this is very bad because heap is used");
F("this is also very bad heap is used");
I have being using std::wstring for a long time. When moving to C
everything in my code is char* and it means UTF8. Life is so much
easier with much less conversions. It is amazing how few function we
need to work with UTF8. The same existing function can be used.
For instance, strcat is fine, strlen (if you need byte len), strdup etc.. everything just works.
u8"utf encoded" also completes the task.
What is missing. Because I create windows programs I miss the open_memstream.
This is for the job of "build strings" and to use the same FILE* functions.
So in C:
const char * or char * to represent and hold strings.
snprintf and open_memstream to build (and format) strings.
Dangerous parts...
strncpy because may not add NULL. ( I have heart of strlcpy )
replacement .
object->text = strdup("new text"); //this may cause a leak
especially for this replacement problem I think C++ has an advantage.
Something I use this that free the previous string.
replace_string(&object->text, strdup("new text"));