开发者

Suggestion on equalsIgnoreCase in Java to C implementation

All,

In a bid to improve my C skills, I decided to start implementing various Java libraries/library functions to C code. This would ensure that everyone knows the functionality of my implementation at least. Here is the link to the C source code that simulates the equalsIgnoreCase() of String class in Java : C source code. I have tested the code and 开发者_C百科it looks fine as per my testing skills are concerned. My aim was to use as much basic operations and datatypes as possible. Though, it would be great if the gurus here can:

1 > Give me any suggestion to improve the code quality

2 > Enlighten me with any missing coding standard/practices

3 > Locate bugs in my logic.


100 lines of code is not too long to post here.

You calculate the string length twice. In C, the procedure to calculate the string length starts at the beginning of the string and runs along all of it (not necessarily in steps of 1 byte) until it finds the terminating null byte. If your strings are 2Mbyte long, you "walk" along 4Mbyte unnecessarily.

in <ctype.h> there are the two functions tolower() and toupper() declared. You can use one of them (tolower) instead of extractFirstCharacterASCIIVal(). The advantage of using the library function is that it is not locked in to ASCII and may even work with foreign characters when you go 'international'.

You use awkward (very long) names for your variables (and functions too). eg: ch1 and ch2 do very well for characters in file 1 and file 2 respectively :-)

return 1; at the end of main usually means something went wrong with the program. return 0; is idiomatic for successful termination.


Edit: for comparison with tcrosley version

#include <ctype.h>
int cmpnocase(const char *s1, const char *s2) {
    while (*s1 && *s2) {
        if (tolower((unsigned char)*s1) != tolower((unsigned char)*s2)) break;
        s1++;
        s2++;
    }
    return (*s1 != *s2);
}


With C++, you could replace performComparison(char* string1, char * string2) with stricmp. However stricmp is not part of the standard C library. Here is a version adapted to your example. Note you don't need the extractFirstCharacterASCIIVal function, use tolower instead. Also note there is no need to explicitly calculate the string length ahead of time, as strings in C are terminated by the NULL character '\0'.

int performComparison(char* string1, char * string2)
{
    char c1, c2;
    int  v;

    do {
        c1 = *string1++;
        c2 = *string2++;
        v = (UINT) tolower(c1) - (UINT) tolower(c2);
    } while ((v == 0) && (c1 != '\0') && (c2 != '\0') );

    return v != 0;
}

If you do want to use your own extractFirstCharacterASCIIVal function instead of the tolower macro, to make the code more transparent then you should code it like so:

   if ((str >= 'a') && (str <= 'z'))
   {
      returnVal = str - ('a' - 'A');
   }
   else
   {
      returnVal = str;
   }

to make it more obvious what you are doing. Also you should include a comment that this assumes the characters a..z and A..Z are contiguous. (They are in ASCII, but not always in other encodings.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜