12 points · by alcover · 4y ago · 12 comments
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.
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)
- 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
union {
struct {
char* data;
size_t len;
size_t off:8*sizeof(size_t)-TAGBITS, tag:TAGBITS;
} ptr;
struct {
char data[sizeof(BuffetPtr)-2];
uint8_t refcnt;
uint8_t len:8-TAGBITS, tag:TAGBITS;
} sso;
}
where the tag falls into place. I tried an older version on 32 bit and it worked ok. > anyone else already tried it?
That's what I wonder too... working on this without any return other than lots of github stars is a bit discouraging.