Re:C Strings and my slow descent to madness
by stephc_int13 on Thursday April 06, 2023 @02:21PM
(#35472302)
IMHO that does not solve the main problem, that is individual lifetime management.
I've seen many libs using this style of strings, not convinced by the practicality.
Re:C Strings and my slow descent to madness
by nickff on Thursday April 06, 2023 @02:35PM
(#35472483)
What is "individual lifetime management"?
Re:C Strings and my slow descent to madness
by klodolph on Thursday April 06, 2023 @03:04PM
(#35472895)
It sounds like you’re rephrasing part of my comment back to me, or maybe I’m misinterpreting what you’re saying.
If you’re not convinced of the practicality, it sounds like you are simply not convinced of the practicality of doing string processing in C at all, which is a fair view point. String processing in C is somewhat a minefield. Libraries like Git’s strbuf are very effective relative to other solutions in C, but lack safety relative to other languages.
Re:C Strings and my slow descent to madness
by stephc_int13 on Thursday April 06, 2023 @03:29PM
(#35473226)
No, I simply am using a different approach, still in C, where strings are simple char*, null-terminated, nothing hidden with magic fields above the base address of the string.
The trick is to pass an allocator (or container) to string handling functions.
If/when I want to get rid of all the garbage I reset the container/allocator.
You often end up with some kind of structure, or variations of structures, for strings:
Those are just examples. The tricky part is figuring out the different ownership use cases you want to solve. Because C gives you so much freedom and very little in the standard library, you end up with a lot of variations. You might use reference-counted strings, owned buffers, or string slices, etc. You might want certain types to be distinguished at compile-time and other types to be distinguished at run-time.An example can be found in the Git source code.
https://github.com/git/git/blob/master/strbuf.h
The history of changes to this file is interesting as well. This is a relatively nice general-purpose string type—you can easily append to it or truncate it.