News for hackers. Stuff that matters.
Re:Show HN by alcover on Saturday December 03, 2022 @02:38PM (#33846544)
> Come again? Of course it can.

Oh come on.. I'm pretty sure Walter meant taking a view kind of slice. Obviously one can always copy part of a string, but that's not what slice implies I think.

> It can't be done in place, mind you, but that's a pretty bad way to do any string slicing, regardless of implementation, in a manual memory management environment.

It's not bad. It's the best, most efficient way. O(1)-ish.

> Do most programmers expect their slices to result in undefined behavior if they release the larger string they were made from? I doubt it.

That's what copy-on-write is for : release of the parent is blocked until no views are left on it.

I made a C String lib using CoW. It works well:

https://github.com/alcover/buffet

Re:Show HN by WalterBright on Saturday December 03, 2022 @06:41PM (#33848742)
The length-ptr strings are very fast and efficient:

1. don't have to make a copy

2. don't have to scan to find the length

3. don't have to load the string into the cache to find its length

As a bonus, they're memory safe.