Proposal for a full-fledged C String
Dynamic allocation, SSO, views, refcounting, etc.. are spread on various implementations.
C++ separates string and string_view. And they're not fully compatible. (Ex. you can't concat cross-type !?).
Rust has str, &str, String Box<str>, ... ?
In the spirit of "a slice of a string is still a string", why not pack it all into a single type ? And offer security in the process.
Here's the scheme : https://github.com/alcover/buffet/raw/main/assets/schema.png
Here's the code : https://github.com/alcover/buffet
NB: I'm talking string/byte buffer. Higher string tooling like Utf8 would go a layer above.
13 comments
[ 3.0 ms ] story [ 43.3 ms ] thread- afaik size_t and char* types are implementation dependent, not sure if this could cause problems, maybe you already thought about the implications?
- are you hard set on using GPL?
GPL : Not really. Maybe LGPL would do. I want the lib to promote open source and that's it.
I mean on 32 bit platform the offset to union tag would be different depending on the struct the union holds, unless I'm missing something
Code to modify the string does a copy, decrement the ref count of the original, and then modify of the new string.
Copies of a string are essentially free. You can return them from functions, etc. with no worry about allocation. The run-time handles all of that.
In C, there are mechanisms for running macros when a variable goes out of scope, those could be used to decrement the reference counter to a string used locally inside a function. (Returning the string would need to increase it's ref count first, of course)