From bug-request at octave dot org Sat Dec 31 23:16:49 2005 Subject: str*cmp* functions From: William Poetra Yoga Hadisoeseno To: bug at octave dot org, "John W. Eaton" Cc: Octave Forge Date: Sun, 1 Jan 2006 13:15:13 +0800 ------=_Part_945_19199601.1136092513143 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I've done the following: 1. Changed the help text of the strcmp function to reflect its ability to accept cell arrays as arguments. 2. Simplified the strcmpi and strncmp functions. 3. Imported strncmpi from octave-forge and simplified it. 4. Written the strtrunc function. Explanation: 1. strcmp is actually already compatible with Matlab's strcmp, so I just added the help text. 2. The implementations of strcmpi, strncmp, and strncmpi (octave-forge) are too complex, IMO, so I simplified them. 3. But the changes made to strncmp and strncmpi needs strtrunc, so I wrote = it. 4. strtrunc takes a string or cellstr as its first argument (s), then truncates it to the value of the second argument (n). Although strtrunc is not present in Matlab, I think this is useful, so I'm sending it here. I hope John would accept this. The attachments: 1. str2.diff.txt: diff for strcmp, strcmpi, strncmp 2. str2.ChangeLog.src.txt: changelog entry for strcmp 3. str2.ChangeLog.scripts.txt: changelog entry for strcmpi, strncmp, strncmpi and strtrunc 4. strncmpi.m.txt, strtrunc.m.txt: the corresponding functions -- William Poetra Yoga Hadisoeseno ------=_Part_945_19199601.1136092513143 Content-Type: text/plain; name=str2.diff.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="str2.diff.txt" Index: src/strfns.cc =================================================================== RCS file: /cvs/octave/src/strfns.cc,v retrieving revision 1.33 diff -u -r1.33 strfns.cc --- src/strfns.cc 7 Nov 2005 19:12:21 -0000 1.33 +++ src/strfns.cc 1 Jan 2006 04:52:04 -0000 at @ -149,10 +149,19 @@ at deftypefn {Function File} {} strcmp (@var{s1}, @var{s2})\n\ Return 1 if the character strings at var{s1} and @var{s2} are the same,\n\ and 0 otherwise.\n\ - at strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ -function returns 1 if the strings are equal, and 0 otherwise. This is\n\ -just the opposite of the corresponding C library function.\n\ - at end deftypefn") +\n\ +If either at var{s1} or @var{s2} is a cell array of strings, then an array\n\ +of the same size is returned, containing the values described above for\n\ +every member of the cell array. The other argument may also be a cell\n\ +array of strings (of the same size or with only one element), char matrix\n\ +or character string.\n\ +\n\ + at strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ +function returns 1 if the character strings are equal, and 0 otherwise.\n\ +This is just the opposite of the corresponding C library function.\n\ + at end deftypefn\n\ +\n\ + at seealso{strcmpi, strncmp, strncmpi}") { octave_value retval; Index: scripts/strings/strcmpi.m =================================================================== RCS file: /cvs/octave/scripts/strings/strcmpi.m,v retrieving revision 1.4 diff -u -r1.4 strcmpi.m --- scripts/strings/strcmpi.m 8 Sep 2005 01:40:58 -0000 1.4 +++ scripts/strings/strcmpi.m 1 Jan 2006 04:52:04 -0000 at @ -1,4 +1,4 @@ -## Copyright (C) 2000 Bill Lash +## Copyright (C) 2000 Bill Lash ## ## This file is part of Octave. ## at @ -19,24 +19,31 @@ ## -*- texinfo -*- ## at deftypefn {Function File} {} strcmpi (@var{s1}, @var{s2}) -## Compare two strings, ignoring case, returning 1 if -## they are the same, and 0 otherwise. +## Ignoring case, return 1 if the character strings at var{s1} and @var{s2} +## 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. +## If either at var{s1} or @var{s2} is a cell array of strings, then an array +## of the same size is returned, containing the values described above for +## every member of the cell array. The other argument may also be a cell +## array of strings (of the same size or with only one element), char matrix +## or character string. +## +## at strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmpi +## function returns 1 if the character strings are equal, and 0 otherwise. +## This is just the opposite of the corresponding C library function. ## at end deftypefn +## +## at seealso{strcmp, strncmp, strncmpi} -## Author: Bill Lash +## Author: Bill Lash ## Adapted-by: jwe -function status = strcmpi(s1, s2) +function retval = strcmpi (s1, s2) if (nargin == 2) - status = (ischar (s1) && ischar(s2) && strcmp (upper (s1), upper (s2))); + retval = strcmp (upper (s1), upper (s2)); else - usage ("strcmpi (s, t)"); + usage ("strcmpi (s1, s2)"); endif endfunction - Index: scripts/strings/strncmp.m =================================================================== RCS file: /cvs/octave/scripts/strings/strncmp.m,v retrieving revision 1.2 diff -u -r1.2 strncmp.m --- scripts/strings/strncmp.m 8 Sep 2005 01:40:58 -0000 1.2 +++ scripts/strings/strncmp.m 1 Jan 2006 04:52:04 -0000 at @ -19,128 +19,31 @@ ## -*- texinfo -*- ## at deftypefn {Function File} {} strncmp (@var{s1}, @var{s2}, @var{n}) -## Compares the first at var{n} characters (columns) of two character -## strings, returning true if they are the same, and false otherwise. +## Return 1 if the first at var{n} characters of character strings @var{s1} +## and at var{s2} are the same, and 0 otherwise. +## +## If either at var{s1} or @var{s2} is a cell array of strings, then an array +## of the same size is returned, containing the values described above for +## every member of the cell array. The other argument may also be a cell +## array of strings (of the same size or with only one element), char matrix +## or character string. ## ## at strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp -## function returns true if the character strings are equal, and false -## otherwise. This is just the opposite of the corresponding C library -## function. +## function returns 1 if the character strings are equal, and 0 otherwise. +## This is just the opposite of the corresponding C library function. ## at end deftypefn +## +## at seealso{strcmp, strcmpi, strncmpi} ## Author: jwe -## Adapted from strcmp.m by Tom Holroyd +## Adapted from strcmp.m by Tom Holroyd function retval = strncmp (s1, s2, n) - if (nargin != 3) - usage ("strncmp (s, t, n)"); - endif - - retval = false; - - if (ischar (s1)) - [r1, c1] = size (s1); - if (ischar (s2)) - [r2, c2] = size (s2); - if (r1 == r2 && c1 == c2) - if (c1 == 0) - retval = true; - 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 = true; - 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, "logical"); - 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, "logical"); - 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, "logical"); - 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 (ischar (s2)) - retval = strncmp (s2, s1, n, "logical"); - 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, "logical"); - 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), "logical"); + if (nargin == 3) + retval = strcmp (strtrunc (s1, n), strtrunc (s2, n)); + else + usage ("strncmp (s1, s2, n)"); endif endfunction ------=_Part_945_19199601.1136092513143 Content-Type: text/plain; name=str2.ChangeLog.src.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="str2.ChangeLog.src.txt" 2006-01-01 William Poetra Yoga Hadisoeseno * src/strfns.cc: Fixed help message. ------=_Part_945_19199601.1136092513143 Content-Type: text/plain; name=str2.ChangeLog.scripts.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="str2.ChangeLog.scripts.txt" 2006-01-01 William Poetra Yoga Hadisoeseno * strings/strcmpi.m strings/strncmp.m: Simplified. * strings/strncmpi.m: Import from octave-forge, simplified. * strings/strtrunc.m: New function. ------=_Part_945_19199601.1136092513143 Content-Type: text/plain; name=strncmpi.m.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="strncmpi.m.txt" ## Copyright (C) 2000 Bill Lash ## ## 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} {} strncmpi (@var{s1}, @var{s2}, @var{n}) ## Ignoring case, return 1 if the first at var{n} characters of character ## strings at var{s1} and @var{s2} are the same, and 0 otherwise. ## ## If either at var{s1} or @var{s2} is a cell array of strings, then an array ## of the same size is returned, containing the values described above for ## every member of the cell array. The other argument may also be a cell ## array of strings (of the same size or with only one element), char matrix ## or character string. ## ## at strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmpi ## function returns 1 if the character strings are equal, and 0 otherwise. ## This is just the opposite of the corresponding C library function. ## at end deftypefn ## ## at seealso{strcmp, strcmpi, strncmp} ## Author: Bill Lash ## Adapted-by: wpyh function retval = strncmpi (s1, s2, n) if (nargin == 3) retval = strcmp (upper (strtrunc (s1, n)), upper (strtrunc (s2, n))); else usage ("strncmpi (s1, s2, n)"); endif endfunction ------=_Part_945_19199601.1136092513143 Content-Type: text/plain; name=strtrunc.m.txt; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="strtrunc.m.txt" ## Copyright (C) 2006 William Poetra Yoga Hadisoeseno ## ## 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} {} strtrunc (@var{s}, @var{n}) ## Truncate the character string at var{s} to length @var{n}. If @var{s} ## is a char matrix, then the number of columns are adjusted. ## ## If at var{s} is a cell array of strings, then the operation is performed ## on its members and the new cell array is returned. ## at end deftypefn function s = strtrunc (s, n) if (nargin != 2) usage ("strtrunc (s, n)"); endif if (ischar (s)) s_was_char = true; s = {s}; else s_was_char = false; endif if (iscellstr (s)) for i = 1:(numel (s)) s{i} = s{i}(:,1:(min (n, columns (s{i})))); endfor else error ("strtrunc: s must be a character string or a cell array of strings"); endif if (s_was_char) s = s{:}; endif endfunction ------=_Part_945_19199601.1136092513143-- ------------------------------------------------------------- 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 -------------------------------------------------------------