Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Apr 2009 12:05:58 -0500 (CDT)
From:      Mark Tinguely <tinguely@casselton.net>
To:        channa.kad@gmail.com, imp@bsdimp.com
Cc:        freebsd-arm@freebsd.org
Subject:   Re: strncmp issue
Message-ID:  <200904301705.n3UH5wg2057498@casselton.net>
In-Reply-To: <20090430.000846.1484329326.imp@bsdimp.com>

next in thread | previous in thread | raw e-mail | index | archive | help

>  Yes.  We should get the following results:
>
>	strncmp("a", "b", 0);	  0
>	strncmp("a", "b", *);	  < 0
>
>  where * is any other number :)
>
>  Warner

ENTRY(strncmp)
/* if (len == 0) return 0 */
	cmp	r2, #0
	moveq	r0, #0
	RETeq

/* ip == last src address to compare */
	add	ip, r0, r2
1:
	ldrb	r2, [r0], #1
	ldrb	r3, [r1], #1
	cmp	ip, r0
-	cmpcs	r2, #1
+	beq	2f
+	cmp	r2, #1
	cmpcs	r2, r3
	beq	1b
+2:
	sub	r0, r2, r3
	RET

also ip < r0 if r2 = (unsigned int) -1 using 32 bit math. so original
loop will terminate and compare the first characters. For example
strncmp("ab", "aa", -1) will result is 0.

I sent Olivier a couple patches, both wrong. Again, sorry for all the noise.

The above code, though, should work in all cases.

	strncmp("b", "a", 0)  result is 0
	strncmp("abcdef", "abcdef", n)  result is 0
	strncmp("abcde", "abcdef", n)   0 if 0 <= n < 6  neg if n < 0 or n > 5
	strncmp("abcdef", "abcde", n)   0 if 0 <= n < 6  pos if n < 0 or n > 5


The "beq" will break out of the loop if we give a count <= the string
length the first arguement.

The next cmp looks for the NULL byte, and the last cmp checks the characters
in the strings.

--Mark.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200904301705.n3UH5wg2057498>