changeset 5416:9433f94acd29

[project @ 2005-07-18 18:36:29 by jwe]
author jwe
date Mon, 18 Jul 2005 18:36:42 +0000
parents 1fbae9f046e3
children 58cd95e83cc5
files scripts/ChangeLog scripts/strings/strcmp.m src/ChangeLog src/strfns.cc
diffstat 4 files changed, 184 insertions(+), 288 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Jul 18 18:16:12 2005 +0000
+++ b/scripts/ChangeLog	Mon Jul 18 18:36:42 2005 +0000
@@ -1,3 +1,7 @@
+2005-07-18  John W. Eaton  <jwe@octave.org>
+
+	* strings/strcmp.m: Delete.
+
 2005-07-13  John W. Eaton  <jwe@octave.org>
 
 	* deprecated/hypergeometric_rnd.m: Preserve compatibility with old
--- a/scripts/strings/strcmp.m	Mon Jul 18 18:16:12 2005 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,147 +0,0 @@
-## 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 -*-
-## @deftypefn {Function File} {} strcmp (@var{s1}, @var{s2})
-## Compares two character strings, returning true if they are the same,
-## and false otherwise.
-##
-## @strong{Caution:}  For compatibility with @sc{Matlab}, Octave's strcmp
-## function returns true if the character strings are equal, and false
-## otherwise.  This is just the opposite of the corresponding C library
-## function.
-## @end deftypefn
-
-## Author: jwe
-
-function retval = strcmp (s1, s2)
-
-  if (nargin != 2)
-    usage ("strcmp (s, t)");
-  endif
-
-  retval = false;
-
-  if (isstr (s1))
-    [r1, c1] = size (s1);
-    if (isstr (s2))
-      [r2, c2] = size (s2);
-      if (r1 == r2 && c1 == c2)
-	if (c1 == 0)
-          retval = true;
-	else
-          retval = all (all (s1 == s2));
-	endif
-      endif
-    elseif (iscellstr (s2))
-      [r2, c2] = size (s2);
-      if (r1 == 1)
-	t2 = s2(:);
-	n = length (t2);
-	retval = zeros (n, 1, "logical");
-	for i = 1:n
-	  retval(i) = strcmp (s1, t2{i});
-	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) = strcmp (deblank (s1(i,:)), t2);
-	  endfor
-	else
-	  t2 = s2(:);
-	  n = length (t2);
-	  if (n == r1)
-	    retval = zeros (n, 1, "logical");
-	    for i = 1:n
-	      retval(i) = strcmp (deblank (s1(i,:)), t2{i});
-	    endfor
-	    retval = reshape (retval, r2, c2);
-	  endif
-	endif
-      endif
-    endif
-  elseif (iscellstr (s1))
-    [r1, c1] = size (s1);
-    if (isstr (s2))
-      [r2, c2] = size (s2);
-      if (r2 == 1)
-	t1 = s1(:);
-	n = length (t1);
-	retval = zeros (n, 1, "logical");
-	for i = 1:n
-	  retval(i) = strcmp (t1{i}, s2);
-	endfor
-	retval = reshape (retval, r1, c1);
-      elseif (r2 > 1)
-	if (r1 == 1 && c1 == 1)
-	  t1 = s1{1};
-	  retval = zeros (r2, 1, "logical");
-	  for i = 1:r2
-	    retval(i) = strcmp (t1, deblank (s2(i,:)));
-	  endfor
-	else
-	  t1 = s1(:);
-	  n = length (t1);
-	  if (n == r2)
-	    retval = zeros (n, 1, "logical");
-	    for i = 1:n
-	      retval(i) = strcmp (t1{i}, deblank (s2(i,:)));
-	    endfor
-	    retval = reshape (retval, r1, c1);
-	  endif
-	endif
-      endif      
-    elseif (iscellstr (s2))
-      [r2, c2] = size (s2);
-      if (r1 == 1 && c1 == 1)
-	t1 = s1{:};
-	t2 = s2(:);
-	n = length (t2);
-	retval = zeros (n, 1, "logical");
-	for i = 1:n
-	  retval(i) = strcmp (t1, t2{i});
-	endfor
-	retval = reshape (retval, r2, c2);
-      elseif (r2 == 1 && c2 == 1)
-	t1 = s1(:);
-	t2 = s2{:};
-	n = length (t1);
-	retval = zeros (n, 1, "logical");
-	for i = 1:n
-	  retval(i) = strcmp (t1{i}, t2);
-	endfor
-	retval = reshape (retval, r1, c1);
-      elseif (r1 == r2 && c1 == c2)
-	t1 = s1(:);
-	t2 = s2(:);
-	n = length (t1);
-	for i = 1:n
-	  retval(i) = strcmp (t1{i}, t2{i});
-	endfor
-	retval = reshape (retval, r1, c1);
-      else
-	error ("strcmp: nonconformant cell arrays");
-      endif
-    endif
-  endif
-
-endfunction
--- a/src/ChangeLog	Mon Jul 18 18:16:12 2005 +0000
+++ b/src/ChangeLog	Mon Jul 18 18:36:42 2005 +0000
@@ -1,3 +1,9 @@
+2005-07-18  John W. Eaton  <jwe@octave.org>
+
+	* strfns.cc (Fstrcmp): New function from Soren Hauberg
+	<soren@hauberg.org> and Tom Holroyd <tomh@kurage.nimh.nih.gov>.
+	Adapt to Octave conventions.
+
 2005-07-11  David Bateman  <dbateman@free.fr>
 
 	* ov-fc-inline.cc (Fvectorize): Allow string arguments
--- a/src/strfns.cc	Mon Jul 18 18:16:12 2005 +0000
+++ b/src/strfns.cc	Mon Jul 18 18:36:42 2005 +0000
@@ -29,6 +29,7 @@
 
 #include "dMatrix.h"
 
+#include "Cell.h"
 #include "defun.h"
 #include "error.h"
 #include "gripes.h"
@@ -153,179 +154,211 @@
 just the opposite of the corresponding C library function.\n\
 @end deftypefn")
 {
-  if (args.length () != 2)
-    {
-      usage ("strcmp (S, T)");
-      return octave_value();
-    }
-
-  octave_value retval = false;
+  octave_value retval;
 
-  bool s1_string = args(0).is_string ();
-  bool s1_cell = args(0).is_cell ();
-  bool s2_string = args(1).is_string ();
-  bool s2_cell = args(1).is_cell ();
+  if (args.length () == 2)
+    {
+      bool s1_string = args(0).is_string ();
+      bool s1_cell = args(0).is_cell ();
+      bool s2_string = args(1).is_string ();
+      bool s2_cell = args(1).is_cell ();
 
-  if (s1_string && s2_string) // must match exactly in all dimensions
-    {
-      const dim_vector dv1 = args(0).dims ();
-      const dim_vector dv2 = args(1).dims ();
+      if (s1_string && s2_string)
+	{
+	  // Must match exactly in all dimensions.
+
+	  const dim_vector dv1 = args(0).dims ();
+	  const dim_vector dv2 = args(1).dims ();
 
-      if (dv1.length () == dv2.length ())
-	{
-	  for (int i = 0; i < dv1.length (); i++)
-	    if (dv1(i) != dv2(i))
-	      return retval;
+	  if (dv1.length () == dv2.length ())
+	    {
+	      for (int i = 0; i < dv1.length (); i++)
+		{
+		  if (dv1(i) != dv2(i))
+		    {
+		      retval = false;
+		      return retval;
+		    }
+		}
 
-	  if (dv1(0) == 0)
-	    retval = true;
-	  else
-	    {
-	      charNDArray s1 = args(0).char_array_value ();
-	      charNDArray s2 = args(1).char_array_value ();
+	      if (dv1(0) == 0)
+		retval = true;
+	      else
+		{
+		  charNDArray s1 = args(0).char_array_value ();
+		  charNDArray s2 = args(1).char_array_value ();
 
-	      for (int i = 0; i < dv1.numel (); i++)
-		if (s1(i) != s2(i))
-		  return retval;
+		  for (int i = 0; i < dv1.numel (); i++)
+		    {
+		      if (s1(i) != s2(i))
+			{
+			  retval = false;
+			  return retval;
+			}
+		    }
 
-	      retval = true;
+		  retval = true;
+		}
 	    }
 	}
-    }
-  else if ((s1_string && s2_cell) || (s1_cell && s2_string))
-    {
-      string_vector str;
-      Cell cell;
-      int r;
-
-      if (s1_string)
-	{
-	  str = args(0).all_strings ();
-	  r = args(0).rows ();
-	  cell = args(1).cell_value ();
-	}
-      else
-	{
-	  str = args(1).all_strings ();
-	  r = args(1).rows ();
-	  cell = args(0).cell_value ();
-	}
-
-      if (r == 1) // broadcast the string
+      else if ((s1_string && s2_cell) || (s1_cell && s2_string))
 	{
-	  boolNDArray output (cell.dimensions);
+	  string_vector str;
+	  Cell cell;
+	  int r;
 
-	  for (int i = 0; i < cell.length (); i++)
-	    if (cell(i).is_string ())
-	      output(i) = (cell(i).string_value () == str[0]);
-	    else
-	      output(i) = false;
+	  if (s1_string)
+	    {
+	      str = args(0).all_strings ();
+	      r = args(0).rows ();
+	      cell = args(1).cell_value ();
+	    }
+	  else
+	    {
+	      str = args(1).all_strings ();
+	      r = args(1).rows ();
+	      cell = args(0).cell_value ();
+	    }
+
+	  if (r == 1)
+	    {
+	      // Broadcast the string.
+
+	      boolNDArray output (cell.dimensions);
 
-	  retval = octave_value (output);
-	}
-      else if (r > 1)
-	{
-	  if (cell.length () == 1) // broadcast the cell
+	      for (int i = 0; i < cell.length (); i++)
+		if (cell(i).is_string ())
+		  output(i) = (cell(i).string_value () == str[0]);
+		else
+		  output(i) = false;
+
+	      retval = output;
+	    }
+	  else if (r > 1)
 	    {
-	      const dim_vector& dv = dim_vector (r, 1);
-	      boolNDArray output (dv);
-
-	      if (cell(0).is_string ())
+	      if (cell.length () == 1)
 		{
-		  const string str2 = cell(0).string_value ();
+		  // Broadcast the cell.
+
+		  const dim_vector dv (r, 1);
+		  boolNDArray output (dv);
+
+		  if (cell(0).is_string ())
+		    {
+		      const std::string str2 = cell(0).string_value ();
 
-		  for (int i = 0; i < r; i++)
-		    output(i) = (str[i] == str2);
+		      for (int i = 0; i < r; i++)
+			output(i) = (str[i] == str2);
+		    }
+		  else
+		    {
+		      for (int i = 0; i < r; i++)
+			output(i) = false;
+		    }
+
+		  retval = output;
 		}
 	      else
 		{
-		  for (int i = 0; i < r; i++)
-		    output(i) = false;
-		}
+		  // Must match in all dimensions.
+
+		  boolNDArray output (cell.dimensions);
 
-	      retval = octave_value (output);
-	    }
-	  else // must match in all dimensions
-	    {
-	      boolNDArray output (cell.dimensions);
+		  if (cell.length () == r)
+		    {
+		      for (int i = 0; i < r; i++)
+			if (cell(i).is_string ())
+			  output(i) = (str[i] == cell(i).string_value ());
+			else
+			  output(i) = false;
 
-	      if (cell.length () == r)
-		{
-		  for (int i = 0; i < r; i++)
-		    if (cell(i).is_string ())
-		      output(i) = (str[i] == cell(i).string_value ());
-		    else
-		      output(i) = false;
-		  retval = octave_value (output);
+		      retval = output;
+		    }
+		  else
+		    retval = false;
 		}
 	    }
 	}
-    }
-  else if (s1_cell && s2_cell)
-    {
-      Cell cell1;
-      Cell cell2;
-
-      int r1 = args(0).numel ();
-      int r2;
-
-      if (r1 == 1) // make the singleton cell2
-	{
-	  cell1 = args(1).cell_value ();
-	  cell2 = args(0).cell_value ();
-	  r1 = cell1.length ();
-	  r2 = 1;
-	}
-      else
+      else if (s1_cell && s2_cell)
 	{
-	  cell1 = args(0).cell_value ();
-	  cell2 = args(1).cell_value ();
-	  r2 = cell2.length ();
-	}
+	  Cell cell1;
+	  Cell cell2;
 
-      const dim_vector size1 = cell1.dimensions;
-      const dim_vector size2 = cell2.dimensions;
+	  int r1 = args(0).numel ();
+	  int r2;
+
+	  if (r1 == 1)
+	    {
+	      // Make the singleton cell2.
 
-      boolNDArray output (size1);
-      
-      if (r2 == 1) // broadcast cell2
-	if (! cell2(0).is_string ())
-	  for (int i = 0; i < r1; i++)
-	    output(i) = false;
-	else
-	  {
-	    const string str2 = cell2(0).string_value ();
+	      cell1 = args(1).cell_value ();
+	      cell2 = args(0).cell_value ();
+	      r1 = cell1.length ();
+	      r2 = 1;
+	    }
+	  else
+	    {
+	      cell1 = args(0).cell_value ();
+	      cell2 = args(1).cell_value ();
+	      r2 = cell2.length ();
+	    }
 
-	    for (int i = 0; i < r1; i++)
-	      if (cell1(i).is_string ())
+	  const dim_vector size1 = cell1.dimensions;
+	  const dim_vector size2 = cell2.dimensions;
+
+	  boolNDArray output (size1);
+
+	  if (r2 == 1)
+	    {
+	      // Broadcast cell2.
+
+	      if (! cell2(0).is_string ())
 		{
-		  const string str1 = cell1(i).string_value ();
-		  output(i) = (str1 == str2);
+		  for (int i = 0; i < r1; i++)
+		    output(i) = false;
 		}
 	      else
-		output(i) = false;
-	  }
-      else
-	{
-	  if (size1 != size2)
-	    {
-	      error ("strcmp: nonconformant cell arrays");
-	      return octave_value ();
+		{
+		  const std::string str2 = cell2(0).string_value ();
+
+		  for (int i = 0; i < r1; i++)
+		    {
+		      if (cell1(i).is_string ())
+			{
+			  const std::string str1 = cell1(i).string_value ();
+			  output(i) = (str1 == str2);
+			}
+		      else
+			output(i) = false;
+		    }
+		}
 	    }
-      
-	  for (int i = 0; i < r1; i++)
-	    if (cell1(i).is_string () && cell2(i).is_string ())
-	      {
-		const string str1 = cell1(i).string_value ();
-		const string str2 = cell2(i).string_value ();
-		output(i) = (str1 == str2);
-	      }
-	    else
-	      output(i) = false;
+	  else
+	    {
+	      if (size1 != size2)
+		{
+		  error ("strcmp: nonconformant cell arrays");
+		  return retval;
+		}
+
+	      for (int i = 0; i < r1; i++)
+		{
+		  if (cell1(i).is_string () && cell2(i).is_string ())
+		    {
+		      const std::string str1 = cell1(i).string_value ();
+		      const std::string str2 = cell2(i).string_value ();
+		      output(i) = (str1 == str2);
+		    }
+		  else
+		    output(i) = false;
+		}
+	    }
+
+	  retval = output;
 	}
-
-      retval = octave_value (output);
     }
+  else
+    print_usage ("strcmp");
 
   return retval;
 }