Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strncmp
04-22-2018, 05:51 PM
Post: #1
strncmp
I am quite confused whether our lib supports strcmp or strncmp. There is strncmp in string.h, but the comment shows it may not work all the time.

We need this function. Pls improve it.
Find all posts by this user
Quote this message in a reply
04-22-2018, 10:31 PM
Post: #2
RE: strncmp
The strncmp's n must be a multiply of 8 in current implementation.

strcmp and strncmp can be implemented using strlen and memcmp now as memcmp is ready.

I will take a look at this.
Quote this message in a reply
04-23-2018, 01:31 PM
Post: #3
RE: strncmp
The current strncmp is quite naive as it was implemented earlier than memcmp. Now with the good performing memcmp, it can be implemented as follows.

strncmp(char *s1, char *s2, long n)

Code:
len1 = strnlen(s1, n)
len2 = strnlen(s2, n)
len = min(len1, len2)
cmp = memcmp(s1, s2, len)
// s1[0:len] == s2[0:len] does not mean s1[0:n] == s2[0:n]
if (cmp == 0):
  if len < n:
    cmp = compare char s1[len+1] and s2[len+1]
return cmp

long strcmp(char *s1, char *s2)

Code:
len1 = strlen(s1)
len2 = strlen(s2)
len = min(len1, len2)
cmp = memcmp(s1, s2, len)
// s1[0:len] == s2[0:len] does not mean s1[0:] == s2[0:]
if (cmp == 0):
  if len < len1 or len < len2:
    cmp = compare char s1[len+1] and s2[len+1]
return cmp

RR @Xinjie



Added to cod://dt ee23f3af258b1a0b4a0bb978eaf72c2d064d97f3 with tests.
Quote this message in a reply
04-24-2018, 04:30 PM
Post: #4
RE: strncmp
(04-23-2018 01:31 PM)zma Wrote:  The current strncmp is quite naive as it was implemented earlier than memcmp. Now with the good performing memcmp, it can be implemented as follows.

strncmp(char *s1, char *s2, long n)

Code:
len1 = strnlen(s1, n)
len2 = strnlen(s2, n)
len = min(len1, len2)
cmp = memcmp(s1, s2, len)
// s1[0:len] == s2[0:len] does not mean s1[0:n] == s2[0:n]
if (cmp == 0):
  if len < n:
    cmp = compare char s1[len+1] and s2[len+1]
return cmp

long strcmp(char *s1, char *s2)

Code:
len1 = strlen(s1)
len2 = strlen(s2)
len = min(len1, len2)
cmp = memcmp(s1, s2, len)
// s1[0:len] == s2[0:len] does not mean s1[0:] == s2[0:]
if (cmp == 0):
  if len < len1 or len < len2:
    cmp = compare char s1[len+1] and s2[len+1]
return cmp

RR @Xinjie



Added to cod://dt ee23f3af258b1a0b4a0bb978eaf72c2d064d97f3 with tests.

Today's limbo1 seems fine with the change too for all modules tested.

Xinjie please review and if good, please merge to the main post.
Quote this message in a reply
Post Reply 


Forum Jump: