2 ms·
Length-prefixed strings have another disastrous problem - strings cannot be sliced. A substring would always have to be copied. Arrays decay to pointers when d
by WalterBright 13y ago
Length-prefixed strings have another disastrous problem - strings cannot be sliced. A substring would always have to be copied.
Arrays decay to pointers when dereferenced, but this wouldn't impede array bounds checking. I didn't bother putting in static array bounds checking in Digital Mars C because pretty much all the interesting cases were buffers passed to a function.
I know C doesn't have first class arrays. That's what my enhancement suggestion was for.
- kyllo 13y agoAren't strings immutable in most languages? Like in javascript, String.slice() returns a copy, it does not mutate the original string. I don't disagree with you, but I don't see how it's a disastrous problem, either.
- WalterBright 13y agoIn D, which has ptr/length arrays, strings are immutable and sliceable without copying. This can make them very fast.
- ScottBurson 13y ago> Length-prefixed strings have another disastrous problem - strings cannot be sliced. A substring would always have to be copied. Slicing is one common operation that's O(n) with a simple vector representation. I agree it's nice if it can be made O(1), but concatenation is still O(n) with that representation. This is why I'm a fan of ropes or other more exotic sequence data structures that can do all the common operations in log time or better. > I know C doesn't have first class arrays. That's what my enhancement suggestion was for. Fair enough. I think it's a good suggestion, but I'm skeptical that it would see wide use. Here's why. It has been straightforward for a long time to declare a struct type that holds a base and bounds, pass that struct by value, and then do one's own bounds checking. (The K&R-era compilers didn't have struct passing by value, but it was introduced in Unix v7, as I recall. That was a long time ago.) This isn't quite as convenient as what you're proposing, but it's not hard, and I don't think I've ever seen anyone do it. (Not that I spend my time reading other people's C code.) It certainly hasn't become standard practice. Still and all, if you can persuade the committee to put it in the language, and also define new standard library functions that make use of it, I agree it would be a good thing.