jcorporation / sds

Simple Dynamic Strings library for C
BSD 2-Clause "Simplified" License
40 stars 13 forks source link

C90 Compliance #38

Open kilianmh opened 1 month ago

kilianmh commented 1 month ago

There was an issue in the original library for C90 compliance here: https://github.com/antirez/sds/issues/74

For a utility library like sds it would be nice to be compliant even with older C standards and it looks like there are not that many changes needed.

What do you think? @jcorporation @dagostinelli If you like the idea, I can submit a PR.

jcorporation commented 1 month ago

I am not very interested for C90 compatibility, but I will merge a PR if it it has no disadvantages.

sds.c:640:52: warning: ISO C90 does not support ‘long long’ [-Wlong-long]

How would you workaround this?

kilianmh commented 1 month ago

How would you workaround this?

Most issues can be solved with conditional compilation such as:

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
    long long num;
#else
    long num;
#endif

This will have no runtime disadvantage, but the code will be bloated somewhat.

The only issue where it's not clear what to do is that C90 does not support flexible array members. We could use a pointer to the array:

struct __attribute__ ((__packed__)) sdshdr5 {
    unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
    #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
        char buf[];
    #else
        char *buf;
    #endif

};

But then we have to manually allocate and deallocate the memory.