Page 1 of 1

C string compare function

Posted: Thu Apr 13, 2017 11:43 am
by IanSeyler

Code: Select all

/* string compare
 * returns 0 if equal, 1 if not equal
 */
int str_cmp(unsigned char str1[], unsigned char str2[])
{
        int i = 0;

        if (str1[0] == '\0' && str2[0] == '\0') // Check if both strings are empty
                return 0;

        for (i=0; str1[i] != '\0'; i++) // Compare strings until end of first
        {
                if (str1[i] != str2[i])
                        return 1;
        }

        if (str2[i] != '\0') // Check to make sure str2 was same length
                return 1;

        return 0; // Otherwise they are equal
}
Does anyone see any glaring issues or use cases I might have missed? It works in testing without any problems.

Re: C string compare function

Posted: Thu Apr 13, 2017 12:17 pm
by Icee
Does this have to be so complicated and non-standard?

Code: Select all

int
strcmp(const char *s1, const char *s2)
{
    while ((*s1 == *s2) && *s1) { ++s1; ++s2; }
    return ((int) (unsigned char) *s1) - ((int) (unsigned char) *s2);
}

Re: C string compare function

Posted: Thu Apr 13, 2017 1:24 pm
by IanSeyler
:shock: Wow. Apparently C isn't exactly my forte.

Re: C string compare function

Posted: Mon Apr 24, 2017 3:20 am
by Solar
Icee handled the optimization well. (And yes, if you're doing indexed string access in most of C's string-related functions, you're doing it wrong.) Let's have a look at style.

Code: Select all

int str_cmp(unsigned char str1[], unsigned char str2[])
If you're implementing something that's almost a standard function, make it the standard function. From a maintenance / intuition standpoint, having something that is almost but not quite a strcmp() is a bad idea. Check chapter 7 of the standard document for a very good and, most of all, precise description of the standard functions. (Some of them, like strncpy(), are notoriously badly understood by many.)

The final draft (which is for most intents and purposes identical to the finished standard) is n1570.

Your declaration makes it look as if the function receives two arrays as parameters. But it doesn't, it receives two pointers (because that is what an array "dumbs down to" when passed as a parameter). You will notice the difference if you apply sizeof to the parameters. So don't make your declaration lie about what is happening.

A string is a sequence of char -- not unsigned char. Making your compare function operate on unsigned char, you will get lots of conversion warnings.

It is correct that the actual difference is computed "as if operating on unsigned char" (that's the wording in the standard), and that is the purpose of the casts in Icee's code.

And if you're looking for some no-strings-attached example code for many standard functions, may I shamelessly advertise PDCLib...

Personal code nitpicks: Always use {} even for one-line loop / conditional bodies. Use space padding inside parenthesis and around operators. Use parenthesis around subexpressions in conditionals. I know there are arguments against each of these as well, but after 15+ years of doing (mostly) C++ maintenance coding, I came to appreciate code that focusses on clarity of intent and verbose readability over most other concerns.

Cheers on the comments, they are spot-on, refreshing to see!

Re: C string compare function

Posted: Mon Apr 24, 2017 3:52 am
by Brendan
Hi,

Nobody ever considers doing an initial "if(s1 == s2) return 0; // Both pointers point to the same thing!"... ;)


Cheers,

Brendan

Re: C string compare function

Posted: Mon Apr 24, 2017 6:14 am
by Solar
Adding a condition to every call to optimize for a rather special case...?

I considered but opted against it.

Re: C string compare function

Posted: Mon Apr 24, 2017 9:43 am
by Schol-R-LEA
Don't look at me here. Like captive orcas, I'm leaving C world for good as soon as I can (but probably not very soon at all, and maybe not ever - again, like the whales, who are, after all, hard to move, being almost as heavy as I am).

(Yeah, I know it is rather silly to interject into a thread just to make a joke, but I needed to make light of developments - or lack thereof - in my own projects a bit.)

Re: C string compare function

Posted: Mon Apr 24, 2017 9:47 am
by dozniak
Schol-R-LEA wrote: (Yeah, I know it is rather silly to interject into a thread just to make a joke, but I needed to make light of developments - or lack thereof - in my own projects a bit.)
There's quite a bit of you in the threads lately. Sometimes even 2-3 posts in a row (which is against the forum rules here). Nothing to do?

Re: C string compare function

Posted: Mon Apr 24, 2017 10:23 am
by iansjack
It's good to have a representative of the forums explain the rules to us. However, it would be useful if this restriction actually appeared in the published rules (else we are working in the dark). Can I trust that you will arrange for this explanation of the forum rules?

(Personally, I think that a multitude of interesting and informative posts are of more value than a single one that adds nothing to the topic under discussion. But it's your forum, not mine, so you get to make the rules.)

Re: C string compare function

Posted: Mon Apr 24, 2017 10:25 am
by Schol-R-LEA
dozniak wrote:
Schol-R-LEA wrote: (Yeah, I know it is rather silly to interject into a thread just to make a joke, but I needed to make light of developments - or lack thereof - in my own projects a bit.)
There's quite a bit of you in the threads lately. Sometimes even 2-3 posts in a row (which is against the forum rules here). Nothing to do?
Pretty much. Or rather, no will to do most of the things I should be doing. Posting here is mostly a 'retreating from the world' thing for me lately.

As for the multiple sequential posts, I usually try to edit existing posts instead, but I have been finding myself editing posts days or even weeks later in some cases, meaning that the additions and corrections go unseen.

Also, too often it triggers "TL;DR", especially in the people I am actually trying to get through to.

However, the MSP approach seems to be failing in that regard, too, as they simply aren't reading the posts at all now.

Re: C string compare function

Posted: Mon Apr 24, 2017 12:13 pm
by dozniak
Schol-R-LEA wrote: Also, too often it triggers "TL;DR", especially in the people I am actually trying to get through to.

However, the MSP approach seems to be failing in that regard, too, as they simply aren't reading the posts at all now.
The wall of text posts do have their detracting effect in this ADHD era.