News for hackers. Stuff that matters.
Re:Git's list of banned C functions by user5994461 on Thursday March 04, 2021 @06:43PM (#26350149)
>>> Surely someone could write a better string library in C that hides the complexity.

In short, it's not possible to write a nice string library in C because C simply doesn't support objects, and by extension doesn't support libraries.

Strings are a perfect example of an "object" in what is later known as object oriented programming. C doesn't have objects, it's the last mainstream language that's simply not object oriented at all, and that prevents from making things like a nice string library.

If you're curious, the closest thing you will see in the C world is something like GTK, a massive C library including string and much more (it's more known as a GUI toolkit but there are many lower level building blocks). It's an absolute nightmare to use because everything is some abuse of void pointers and structs.

Re:Git's list of banned C functions by phlakaton on Thursday March 04, 2021 @07:08PM (#26350417)
What rubbish! You do not need objects to make a library. Structs, typedefs, and functions do just fine. There are even techniques in C to define abstract data types if you want!

Take another look at https://developer.gnome.org/glib/stable/glib-Strings.html#g-... . That’s all C, baby, and could be replicated in a completely independent strings-only library built on the standard library if you wished. The reasons no such library exists are ecological, not technical.

Re:Git's list of banned C functions by zlynx on Thursday March 04, 2021 @06:52PM (#26350236)
I think you mean GLib as seen here https://developer.gnome.org/glib/stable/glib-Strings.html

GLib and GTK are closely aligned parts of GNOME so they are easy to get mixed up.

Re:Git's list of banned C functions by user5994461 on Thursday March 04, 2021 @06:57PM (#26350300)
Right. The string library is in glib.

There were a few big libraries in the ecosystem if I remember well, GTK, glib and another two. They're from the same origin and often mixed together.

It's been almost a decade since I dabbled into this stuff day-to-day. I think being forced to use glib is the turning point in a developer's life where you realize you simply have to move on to a more usable language.

Re:Git's list of banned C functions by Tteriffic on Thursday March 04, 2021 @09:59PM (#26352005)
So true
Re:Git's list of banned C functions by c-cube on Thursday March 04, 2021 @06:58PM (#26350304)
Strings have nothing to do with objects. You can write a string library, eg. [sds](https://github.com/antirez/sds). It's just not standard.
Re:Git's list of banned C functions by user5994461 on Thursday March 04, 2021 @07:31PM (#26350671)
The challenge is not to write a string library, but to write a "nice" string library.

Let's say, something that's easier to use and doesn't have all the footguns of the char arrays.

The library you link doesn't come anywhere close to that. It's 99% like the standard library and it has the exact same issues.

Re:Git's list of banned C functions by mariusor on Friday March 05, 2021 @06:12AM (#26355508)
I would love to see what you mean by "exact same issues".

sds strings contain their lengths, so operating on them you don't have to rely on null termination, which (to my knowledge as a lower-midlevel C programmer) is the most prevalent reason why people take issue with C strings.

If you mean that they're not really "strings" but byte arrays I would say that I agree, but to all intents and purposes that's what the C ecosystem considers as strings.

Keeping an API which is very similar to the standard library is also a plus, as it doesn't force developers to change the way they reason about the code.

Re:Git's list of banned C functions by CRConrad on Friday March 05, 2021 @11:24AM (#26358613)
> sds strings contain their lengths, so operating on them you don't have to rely on null termination, which (to my knowledge as a lower-midlevel C programmer) is the most prevalent reason why people take issue with C strings.

Wait, haven't I seen that idea somewhere else...?

> If you mean that they're not really "strings" but byte arrays I would say that I agree, but to all intents and purposes that's what the C ecosystem considers as strings.

Aha, strings as byte arrays but with a built-in length marker.

But yeah, Pascal is sooo outmoded and inferior to C...

Sigh.

Re:Git's list of banned C functions by kergonath on Friday March 05, 2021 @07:19AM (#26356002)
You’re moving goalposts now. Just earlier you wrote that you couldn’t write a library in C because C does not support objects, not that you couldn’t write a nice library (for whatever definition of “nice” you want to use, which will be different from someone else’s).

In fact there are several libraries for string-like objects; the main barrier to use them is that none of them is standard. You can at least acknowledge that before talking about nice-ness, which is a whole other point.