by TimJYoung · 8y ago · 0 comments
So, am I missing something and there is some concrete reason why this can't be implemented ?
Strings-as-char-* are part of the public APIs of damn near every C library, including the standard library, so there's no way to change that idiom without breaking the world or causing a huge migration tax. The kind of people using C are often doing so specifically because they want to avoid that kind of churn and want a maximally-stable platform, even if what it's stabilized to is sub-optimal.
Result:=CreateFile(pChar(FileName),
AccessMode,ShareMode,nil,
OpenFlags,AttrFlags,0);
where FileName is a String and pChar is the more traditional C-style pointer to an array of characters. The compiler will prevent you from passing a Unicode/Wide string to an API call that expects an ANSI string pointer, or vice-versa. So, interop with the system-level APIs of Windows or Linux is seamless and easy.More information:
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Internal_D...
under "Long String Types".
If done from the beginning, slices could have gotten the `T[]` syntax and array-pointer decay could have been replaced by the easier to use and safer array-slice decay. But of course backwards compatibility prevents this now.
I hold out hope it'll happen, but I'm probably in denial. The fact that VLA function parameters were made optional in C11 doesn't bode well, but perhaps that's because it's incomplete--syntax and semantics stops short of what's needed to spur adoption of safer APIs. With a proper slice construct that was easy to use and integrate into idiomatic C code then there'd be more demand for implementing and using the necessary VLA compiler machinery, if not VLAs themselves.
Travel back in time, when they were busy implementing C.
Compare how ESPOL, NEWP, PL/I, PL/S, PL/X, PL.8, BLISS, Algol 68 implemented arrays, strings and unsafe code blocks, and how C decided to go their own way.
BCPL was originally designed as means to Bootstrap CPL, not to be used alone.
See a pattern there?
Then AT&T could not sell UNIX, gave it away at a symbolic price of about $100 and the rest is history.
You're free to roll your own String type that has the length. No clue why people don't just do that though (I assume many do, but obviously a ton do not).
Most somewhat larger C shops will have their own internal utility library or they'll use one of the more popular batteries included packages which typically takes care of memory allocation, string handling, lists and a bunch of other useful stuff.
For certain definitions of "everyone," maybe... I am no longer primarily a C programmer, and haven't been since around the end of 2013, and I'm still firmly in the "C isn't going any where" camp. I just happen to liken this stance to reality.
I really would like that something like SafeC or CheckedC would win the hearts of C devs
One example is the stretchy buffer library that I saw posted recently here.
My point is that implementing a new string type has zero effect upon existing C programs if they don't use the new string type, so I'm confused as to why it hasn't been done. If a C developer doesn't want to use them, then "no harm, no foul".
I see no harm in adding more functions around the string type we already have, but as I mentioned in https://news.ycombinator.com/item?id=17248446, `snprintf` is the mother-of-all-string-functions that does everything you need, so not much else is needed.
Re: snprintf - yes I saw that and it definitely does do most of the heavy lifting, but it still is something that the developer needs to handle manually (I know, I know, not everyone should be using C...).