Author Topic: patch for uStrcmp  (Read 7744 times)

Offline schveiguy

  • Newbie
  • *
  • Posts: 19
    • View Profile
patch for uStrcmp
« on: September 14, 2010, 01:57:49 PM »
While building a binary search on strings, I found that uStrcmp was inadequate.  It only tests equality, where I need it to test order as well.

Here is a patch if anyone is interested, it should be just as fast as the original, since the test for order doesn't occur until a difference is found.

-Steve

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: patch for uStrcmp
« Reply #1 on: September 18, 2010, 03:03:37 PM »
Hi Steve

uStrcmp() is a library replacement for strcmp(). It is therefore compatible with strcmp().
The idea behind the library replacement routines is simply to avoid libraries in small project. More info here: http://www.utasker.com/docs/CodeDoc/lib_intro.htm

Having said that, the uStrcpy() is not fully compatible with strcpy() since it returns not a pointer to the original string but to the end of the copied string since this is rather mode useful (I don't expect anyone to otherwise use the return pointer to the same string that was passed...).

Therefore an extension to inform whether the differing character in the string has a lower ASCII value does seem to be of use in some cases (when sorting strings in alphabetic order) so I will look at integrating this. The criteria is still that uStrcmp() should essentially be compatible with strcmp() but the check !uStrcmp() would still be fine, just additional checking of < 0 would give further information about the string's alphabetic order.

Thanks.

Regards

Mark

Offline schveiguy

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: patch for uStrcmp
« Reply #2 on: September 18, 2010, 09:58:04 PM »
Hi Steve

uStrcmp() is a library replacement for strcmp(). It is therefore compatible with strcmp().


According to Linux and msdn, that is incorrect.  strcmp is supposed to return <0, 0, or >0 depending on the lexicographical order:

http://msdn.microsoft.com/en-us/library/e0z9k731.aspx
http://linux.die.net/man/3/strcmp

And according to that linux man page, that implementation of strcmp is conforming to C89 and C99 standards.  So it's been like that for a while.

-Steve

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: patch for uStrcmp
« Reply #3 on: September 19, 2010, 01:46:03 AM »
Hi Steve

I didn't realise that - maybe I made a mistake when first writing the uStrcmp(). That means that your improvement must be used ;-)

Regards

Mark