From octave-sources-request at bevo dot che dot wisc dot edu Thu Jul 13 11:19:07 2000 Subject: Re: strncmp, strcmpi, and strncmpi From: Bill Lash To: julian at matlinks dot net CC: octave-sources at bevo dot che dot wisc dot edu, pkienzle@kienzle.powernet.co.uk Date: Thu, 13 Jul 2000 11:17:59 -0500 This is a multi-part message in MIME format. --------------561B173CB761B793D21576E3 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit julian at matlinks dot net wrote: > > I did those tests for you in Matlab. Warning: your implementations are > _not_ compatible in all cases: Man am I a bad guesser or what?? These can be easily fixed, I think. I am attatching new versions. > > "For strncmp and strncmpi, if n<1 I am returning 1 (indicating the > strings are equal) as long as s1 and s2 are strings." > >> nope... > strncmp returns 0 if n < 1, no matter what the strings (even nul strings) > strncmpi behaves the same way. ok, fixed that one. > > "For strncmp and strncmpi, if n is greater than both string sizes, I am > passing the strings to strcmp without padding to length n. If s1 and s2 > are of different length they will automatically cause 0 to be returned > even if the difference is only trailing blanks (just like strcmp)." > >> nope... > strncmp returns 0 if n > either string length (even if they have the same > length) > strncmpi behaves the same way. ok, that was pretty easy to fix as well. > > "For strncmp and strncmpi, if columns(s2) < n <= columns(s1) I am > returning 0 (again not padding the short string)." > >> nope again... > strncmp returns 1 if the strings differ in length although the compared > substrings match up to n, even with trailing blanks > strncmpi behaves the same way. I think I didn't explain this one very well. Since you say above that 0 is always returned if n is greater than either string length, the situation that I am talking about here would be takne care of by that. I am attatching a new version of strncmp.m which should fix all of the problems that you mention (strncmpi calls strncmp, so there is no need to change strncmpi) Thanks for the input. Bill Lash lash at tellabs dot com --------------561B173CB761B793D21576E3 Content-Type: text/plain; charset=us-ascii; name="strncmp.m" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="strncmp.m" ## Copyright (C) 2000 Bill Lash ## ## This program is free software and may be used for any purpose. This ## copyright notice must be maintained. Bill Lash is not responsible ## for the consequences of using this software. ## usage: strncmp (s1, s2, n) ## ## Compare the first n characters of two strings, returning 1 if ## they are the same, and 0 otherwise. ## ## Note: For compatibility with Matlab, Octave's strncmp function ## returns 1 if the strings are equal, and 0 otherwise. This is ## just the opposite of the corresponding C library function. ## Author: Bill Lash (lash at tellabs dot com) function status = strncmp(s1, s2, n) if (nargin != 3) usage ("strncmp (s, t, n)"); endif status = 0; # Assume strings are different if (isstr (s1) && isstr(s2)) c1 = columns (s1); c2 = columns (s2); if (n < 1) # Comparing less than 1 character of the string status = 0; # will always say they are not equal (for Matlab # compatibility elseif ((n <= c1) && (n <= c2)) status = strcmp(s1(:,1:n),s2(:,1:n)); else status = 0; endif endif endfunction --------------561B173CB761B793D21576E3-- ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------