From bug-request at octave dot org Fri May 20 14:46:08 2005 Subject: Re: strcmp doesn't return a bool From: Tom Holroyd To: Tom Holroyd CC: bug at octave dot org, "John W. Eaton" , octave-dev@lists.sourceforge.net Date: Fri, 20 May 2005 15:44:56 -0400 This is a multi-part message in MIME format. --------------070304080508010003080402 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Tom Holroyd wrote: > The strncmp implementation in octave-forge doesn't handle cell arrays at > all. Here's one that does. This is based on JWE's strcmp. I collapsed the case strncmp(cell array, string matrix, n) to strncmp(string matrix, cell array, n) with the arguments reversed; the same thing could be done to strcmp. I don't know if it's slower or faster, but it's easier to maintain, certainly. There's a similar possible collapse in the cell, cell case, but it looks like it might be a bit slower. I didn't time it, it just looks that way. That one's minor anyway. Of course these are both popular functions and as one person has already mentioned today are good candidates for builtins. These guys are both different from C's str*cmp, so at least, the builtin can follow the logic here, which is compatible and correct, as far as I know. -- Dr. Tom Holroyd "A man of genius makes no mistakes. His errors are volitional and are the portals of discovery." -- James Joyce --------------070304080508010003080402 Content-Type: text/x-objcsrc; name="strncmp.m" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="strncmp.m" ## Copyright (C) 1996, 1997 John W. Eaton ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA ## 02110-1301, USA. ## -*- texinfo -*- ## at deftypefn {Function File} {} strncmp (@var{s1}, @var{s2}, @var{n}) ## Compares the first n characters (columns) of two strings, returning 1 if ## they are the same, and 0 otherwise. ## ## at strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp ## function returns 1 if the strings are equal, and 0 otherwise. This is ## just the opposite of the corresponding C library function. ## at end deftypefn ## Author: jwe ## Adapted from strcmp.m by Tom Holroyd function retval = strncmp (s1, s2, n) if (nargin != 3) usage ("strncmp (s, t, n)"); endif retval = 0; if (isstr (s1)) [r1, c1] = size (s1); if (isstr (s2)) [r2, c2] = size (s2); if (r1 == r2 && c1 == c2) if (c1 == 0) retval = 1; else if (c1 > n) t1 = s1(:, 1:n); t2 = s2(:, 1:n); retval = all (all (t1 == t2)); else retval = all (all (s1 == s2)); endif endif elseif (r1 == r2) if (r1 == 0) retval = 1; else l1 = min(n, c1); l2 = min(n, c2); if (l1 == l2) t1 = s1(:, 1:l1); t2 = s2(:, 1:l2); retval = all (all (t1 == t2)); endif endif endif elseif (iscellstr (s2)) [r2, c2] = size (s2); if (r1 == 1) t2 = s2(:); m = length (t2); retval = zeros (m, 1); for i = 1:m retval(i) = strncmp (s1, t2{i}, n); endfor retval = reshape (retval, r2, c2); elseif (r1 > 1) if (r2 == 1 && c2 == 1) t2 = s2{1}; retval = zeros (r1, 1); for i = 1:r1 retval(i) = strncmp (deblank (s1(i,:)), t2, n); endfor else t2 = s2(:); m = length (t2); if (m == r1) retval = zeros (m, 1); for i = 1:m retval(i) = strncmp (deblank (s1(i,:)), t2{i}, n); endfor retval = reshape (retval, r2, c2); else error ("strncmp: nonconformant arrays"); endif endif endif endif elseif (iscellstr (s1)) [r1, c1] = size (s1); if (isstr (s2)) retval = strncmp(s2, s1, n); elseif (iscellstr (s2)) [r2, c2] = size (s2); if (r1 == 1 && c1 == 1) t1 = s1{:}; t2 = s2(:); m = length (t2); retval = zeros (m, 1); for i = 1:m retval(i) = strncmp (t1, t2{i}, n); endfor retval = reshape (retval, r2, c2); elseif (r2 == 1 && c2 == 1) # retval = strncmp(s2, s1, n); t1 = s1(:); t2 = s2{:}; m = length (t1); retval = zeros (m, 1); for i = 1:m retval(i) = strncmp (t1{i}, t2, n); endfor retval = reshape (retval, r1, c1); elseif (r1 == r2 && c1 == c2) t1 = s1(:); t2 = s2(:); m = length (t1); for i = 1:m retval(i) = strncmp (t1{i}, t2{i}, n); endfor retval = reshape (retval, r1, c1); else error ("strncmp: nonconformant cell arrays"); endif endif endif if (n < 1) retval = zeros(size(retval)); endif retval = retval == 1; endfunction --------------070304080508010003080402-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------