I want to write my own C string library. What should I do?
I want it to be able to do everything the C standard library
So far, I've started with typedef struct gstring { char* data; size_t length; } gstring; // G for george, not the underwear!
Why are you looking to recreate the C string library? I mean, it's pretty battle tested as it is.
Or is this strictly a learning exercise?
It is a rite of passage :-D
The next thing I'd do is add constructors and start overloading operators. Assignment operators, concatentation with +, conversion operator to char*, and stream insertion and extraction operators.
If only C supported operator overloading :( How am I going to print my gstrings though?
I guess I will be using the plain write() functions instead of the printf family :( but how will I do formatted input/output
Ah, C, not C++ - my mistake ;) So, in lieu of operators, you'll need standalone functions for concatenation and similar. For printing, you could just printf("String: %s", myGString.data), or write a new function printfgstring that can take a gstring as its sole argument, or even decomposes a variable argument list and gets the c-string out of your gstring struct.
Join our real-time social learning platform and learn together with your friends!