From octave-sources-request at bevo dot che dot wisc dot edu Tue Jul 11 19:25:52 2000 Subject: strncmp, strcmpi, and strncmpi From: Bill Lash To: octave-sources at bevo dot che dot wisc dot edu, pkienzle@kienzle.powernet.co.uk Date: Tue, 11 Jul 2000 19:24:45 -0500 This is a multi-part message in MIME format. --------------5AB842A0C1413A916504695B Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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 --------------5AB842A0C1413A916504695B 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 = 1; # will always say they are equal (or should this # be an error?) elseif ((n <= c1) && (n <= c2)) status = strcmp(s1(:,1:n),s2(:,1:n)); elseif ((n > c1) && (n > c2)) status = strcmp(s1,s2); endif endif endfunction --------------5AB842A0C1413A916504695B Content-Type: text/plain; charset=us-ascii; name="strncmpi.m" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="strncmpi.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: strncmpi (s1, s2, n) ## ## Compare the first n characters of two strings, ignoring case, ## returning 1 if they are the same, and 0 otherwise. ## ## Note: For compatibility with Matlab, Octave's strncmpi 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 = strncmpi(s1, s2, n) if (nargin != 3) usage ("strncmpi (s, t, n)"); endif status = 0; # Assume strings are different if (isstr (s1) && isstr(s2)) status = strncmp(upper(s1),upper(s2),n); endif endfunction --------------5AB842A0C1413A916504695B Content-Type: text/plain; charset=us-ascii; name="strcmpi.m" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="strcmpi.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: strcmpi (s1, s2) ## ## Compare two strings, ignoring case, returning 1 if ## they are the same, and 0 otherwise. ## ## Note: For compatibility with Matlab, Octave's strcmpi 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 = strcmpi(s1, s2) if (nargin != 2) usage ("strcmpi (s, t)"); endif status = 0; # Assume strings are different if (isstr (s1) && isstr(s2)) status = strcmp(upper(s1),upper(s2)); endif endfunction --------------5AB842A0C1413A916504695B-- ----------------------------------------------------------------------- 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 -----------------------------------------------------------------------