From octave-sources-request at bevo dot che dot wisc dot edu Wed Jul 12 20:16:26 2000 Subject: RE: strncmp, strcmpi, and strncmpi From: "Julian A. de Marchi, Ph.D." To: "Bill Lash" , , Date: Wed, 12 Jul 2000 21:14:39 -0400 I did those tests for you in Matlab. Warning: your implementations are _not_ compatible in all cases: "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. "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. "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. - julian at matlinks dot net : -----Original Message----- : From: octave-sources-request at bevo dot che dot wisc dot edu : [mailto:octave-sources-request at bevo dot che dot wisc dot edu]On Behalf Of Bill Lash : Sent: Tuesday, July 11, 2000 20:25 : To: octave-sources at bevo dot che dot wisc dot edu; pkienzle@kienzle.powernet.co.uk : Subject: strncmp, strcmpi, and strncmpi : : : I noticed that strncmp, strcmpi, and strncmpi were listed as Matlab : functions not available for Octave in the matcompat package (thanks : for organizing this Paul). These seemed pretty easy, so I wrote : them. I don't have Matlab to check to see if they behave the same : way as they do there, but I took my best shot. : : If anybody compares them to Matlab, I would be interested in knowing : how they compare in the following instances: : : For strncmp and strncmpi, if n<1 I am returning 1 (indicating the : strings : are equal) as long as s1 and s2 are strings. : : 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). : : For strncmp and strncmpi, if columns(s2) < n <= columns(s1) I am : returning : 0 (again not padding the short string). : : Most of this is just what I feel is a logical extension of what the : current : strcmp() implementation does. One thing that seems questionable to me is : that : currently, strcmp() will give an erro if the row dimension of 2 strings : does : not match, e.g. : : octave:1> a="this" : a = this : octave:2> b="that" : b = that : octave:3> c=[a;b] : c = : : this : that : : octave:4> strcmp(a,c) : error: operator ==: nonconformant arguments (op1 is 1x4, op2 is 2x4) : error: evaluating binary operator `==' near line 48, column 31 : error: evaluating argument list element number 1 : error: evaluating index expression near line 48, column 23 : error: evaluating argument list element number 1 : error: evaluating index expression near line 48, column 18 : error: evaluating assignment expression near line 48, column 16 : error: evaluating if command near line 45, column 7 : error: evaluating if command near line 44, column 5 : error: evaluating if command near line 41, column 3 : error: called from `strcmp' in file : `/home/tlabat-4/lash/progs/share/octave/2.1.14/m/strings/strcmp.m' : : It seems to me that the current strcmp.m file might be better if it : had the following patch applied: : : --- /home/tlabat-4/lash/progs/share/octave/2.1.14/m/strings/strcmp.m : Tue Jun 22 16:11:59 1999 : +++ strcmp.m Tue Jul 11 19:20:39 2000 : at @ -39,9 +39,9 @@ : : status = 0; : if (isstr (s1) && isstr(s2)) : - c1 = columns (s1); : - c2 = columns (s2); : - if (c1 == c2) : + [c1,r1] = size (s1); : + [c2,r2] = size (s2); : + if ((c1 == c2) && (r1 == r2)) : if (c1 == 0) : status = 1; : else : : That way it would just have the two strings not being equal. : I guess I would be interested in what Matlab does in this : case as well. : : Anyway, any feedback (good or bad) is appreciated. : : Bill Lash : lash at tellabs dot com ----------------------------------------------------------------------- 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 -----------------------------------------------------------------------