changeset 8462:ebdf1e058d85

Make strvcat an internal function
author Thorsten Meyer <thorsten.meyier@gmx.de>
date Mon, 12 Jan 2009 12:22:26 -0500
parents 9d456730b7a8
children a74871446af7
files scripts/ChangeLog scripts/strings/Makefile.in scripts/strings/strvcat.m src/ChangeLog src/strfns.cc
diffstat 5 files changed, 127 insertions(+), 130 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Jan 12 12:13:05 2009 -0500
+++ b/scripts/ChangeLog	Mon Jan 12 12:22:26 2009 -0500
@@ -1,3 +1,8 @@
+2009-01-12  Thorsten Meyer  <thorsten.meyier@gmx.de>
+
+	* strings/strvcat.m: Remove.
+	* strings/Makefile.in (SOURCES): Remove strvcat.m.
+	
 2009-01-12  John W. Eaton  <jwe@octave.org>
 
 	* plot/diffuse.m, plot/surfl.m, plot/specular.m: Style fixes.
--- a/scripts/strings/Makefile.in	Mon Jan 12 12:13:05 2009 -0500
+++ b/scripts/strings/Makefile.in	Mon Jan 12 12:22:26 2009 -0500
@@ -37,7 +37,7 @@
   dec2bin.m dec2hex.m findstr.m hex2dec.m index.m isletter.m isstrprop.m \
   mat2str.m regexptranslate.m rindex.m split.m str2double.m \
   str2num.m strcat.m cstrcat.m strcmpi.m strfind.m strjust.m strmatch.m \
-  strncmpi.m strrep.m strtok.m strtrim.m strtrunc.m strvcat.m \
+  strncmpi.m strrep.m strtok.m strtrim.m strtrunc.m \
   substr.m validatestring.m
 
 DISTFILES = $(addprefix $(srcdir)/, Makefile.in $(SOURCES))
--- a/scripts/strings/strvcat.m	Mon Jan 12 12:13:05 2009 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,129 +0,0 @@
-## Copyright (C) 1996, 2006, 2007 Kurt Hornik
-##
-## 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 3 of the License, 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, see
-## <http://www.gnu.org/licenses/>.
-
-## -*- texinfo -*-
-## @deftypefn {Function File} {} strvcat (@var{s_1}, @dots{}, @var{s_n})
-## Return a character array containing the strings (or cell-strings) 
-## @var{s_1}, @dots{}, @var{s_n} as
-## its rows.  Each string is padded with blanks in order to form a valid
-## matrix.  For numerical input, each element is converted to the
-## corresponding ASCII character.  Unlike @var{char}, empty strings are
-## removed. For example:
-##
-## @example
-## @group
-## strvcat ([97, 98, 99], "", @{"98", "99", 100@}, ["num", "bers"])
-##     @result{} ans = 
-##        ["abc    "
-##         "98     "
-##         "99     "
-##         "d      "
-##         "numbers"]
-## @end group
-## @end example
-##
-## @seealso{char, cstrcat, strcat}
-## @end deftypefn
-
-## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
-## Adapted-By: jwe
-## Modified: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> converted
-##           str2mat to strvcat.  Same function except that strvcat
-##           ignores empty strings.
-## Modified by Alois Schloegl <a.schloegl@ieee.org> Mar 2005
-##	     added support for cell-strings 
-## Modifed by David Bateman for NDArrays
-
-function retval = strvcat (varargin)
-
-  if (nargin == 0)
-    print_usage ();
-  endif
-
-  nr = zeros (nargin, 1);
-  nc = zeros (nargin, 1);
-  K = 0; 
-  nd = ndims (varargin{1});
-  sz = size (varargin{1});
-  for k = 1:nargin
-    s = varargin{k};
-    if (iscell (s))
-      for k1 = 1:length(s)
-	K++;
-	nr(K) = size (s{k1}, 1);
-	nc(K) = size (s{k1}, 2);
-	if (ndims (s{k1}) != nd)
-	  error ("strvcat: dimension mismatch");
-	else
-	  if (any (sz(3:nd) != size (s{k1}) (3:nd)))
-	    error ("strvcat: dimension mismatch");
-	  endif
-	endif
-      endfor
-    else
-      K++;
-      nr(K) = size (s, 1);
-      nc(K) = size (s, 2);
-      if (ndims (s) != nd)
-	error ("strvcat: dimension mismatch");
-      else
-	if (any (sz(3:nd) != size (s) (3:nd)))
-	  error ("strvcat: dimension mismatch");
-	endif
-      endif
-    endif
-  endfor
-
-  sz(1) = sum (nr);
-  sz(2) = max (nc);
-  retval = char (ones (sz) * toascii (" "));
-
-  idx = cell(nd,1);
-  for k = 3:nd
-    idx{k} = sz{k};
-  endfor
-
-  K = 0;
-  row_offset = 0;
-  for k = 1:nargin
-    s = varargin{k};
-    if (iscell (s))
-      for k1 = 1:length(s)
-	K = K + 1;
-	idx{1} = [row_offset + 1 : row_offset + nr(k)];
-	idx{2} = [1 : nc(K)];
-	retval(idx{:}) = char(s{k1});
-	row_offset = row_offset + size (s{k1}, 1);
-      endfor
-    else
-      K++;
-      if (nc(K) > 0)
-    	retval ((row_offset+1):(row_offset+nr(K)), 1:nc(K)) = char (s);
-      endif
-      row_offset = row_offset + nr(K);
-    endif
-  endfor
-
-endfunction
-
-%!shared s1,s2,s3,s4,c
-%! s1 = "quick"; s2 = "brown"; s3 = "fox"; s4 = ["quick";"brown";"fox  "];
-%! c{1} = s1; c{2} = s2; c{3} = s3;
-%!assert(strvcat(s1,s2,s3),s4)
-%!assert(strvcat(c),s4)
-%!assert(strvcat(s4,s4),[s4;s4]);
--- a/src/ChangeLog	Mon Jan 12 12:13:05 2009 -0500
+++ b/src/ChangeLog	Mon Jan 12 12:22:26 2009 -0500
@@ -1,3 +1,7 @@
+2009-01-12  Thorsten Meyer  <thorsten.meyier@gmx.de>
+
+	* strfns.cc (Fstrvcap): New function.
+	
 2008-11-29  Thorsten Meyer  <thorsten.meyier@gmx.de>
 
         * strfns.cc (Fchar, Fischar, Fstrncmp, Flist_in_columns):
--- a/src/strfns.cc	Mon Jan 12 12:13:05 2009 -0500
+++ b/src/strfns.cc	Mon Jan 12 12:22:26 2009 -0500
@@ -152,6 +152,123 @@
 %!assert(strcmp (char ("a", "bb", "ccc"), ["a  "; "bb "; "ccc"]));
 */
 
+DEFUN (strvcat, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} strvcat (@var{x})\n\
+@deftypefnx {Built-in Function} {} strvcat (@var{cell_array})\n\
+@deftypefnx {Built-in Function} {} strvcat (@var{s1}, @var{s2}, @dots{})\n\
+Create a character array from one or more numeric matrices, character\n\
+matrices or cell arrays.  For numerical input, each element is converted\n\
+to the corresponding ASCII character.  The arguments (and elements of\n\
+cell array(s)) are concatenated vertically.\n\
+The returned values are padded with blanks as needed to make each row\n\
+of the string array have the same length.  Unlike @code{char}, empty\n\
+strings are removed.\n\
+For example,\n\
+\n\
+@example\n\
+@group\n\
+strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\
+     @result{} [\"abc    \"\n\
+        \"98     \"\n\
+        \"99     \"\n\
+        \"d      \"\n\
+        \"numbers\"]\n\
+@end group\n\
+@end example\n\
+\n\
+@seealso{char, strcat, cstrcat}\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin > 0)
+    {
+      int n_elts = 0;
+
+      int max_len = 0;
+
+      for (int i = 0; i < nargin; i++)
+	{
+	  string_vector s = args(i).all_strings ();
+
+	  if (error_state)
+	    {
+	      error ("strvcat: unable to convert some args to strings");
+	      return retval;
+	    }
+
+          int n = s.length ();
+
+          // do not count empty strings in calculation of number of elements
+          if (n > 0)
+            {
+              for (int j = 0; j < n; j++)
+                {
+                  if (s[j].length () > 0)
+                    n_elts++;
+                }
+            }
+
+	  int s_max_len = s.max_length ();
+
+	  if (s_max_len > max_len)
+	    max_len = s_max_len;
+	}
+
+      string_vector result (n_elts);
+
+      int k = 0;
+
+      for (int i = 0; i < nargin; i++)
+	{
+	  string_vector s = args(i).all_strings ();
+
+	  int n = s.length ();
+
+          if (n > 0)
+            {
+	      for (int j = 0; j < n; j++)
+	        {
+	          std::string t = s[j];
+                  if (t.length () > 0)
+                    {
+                      int t_len = t.length ();
+
+                      if (max_len > t_len)
+                        t += std::string (max_len - t_len, ' ');
+
+                      result[k++] = t;
+                    }
+	        }
+            }
+	}
+
+      retval = octave_value (result, '\'');
+    }
+  else
+    print_usage ();
+
+  return retval;
+}
+
+/*
+%!error <Invalid call to strvcat> strvcat()
+%!assert (strvcat (""), "");
+%!assert (strvcat (100) == "d");
+%!assert (all(strvcat (100,100) == ["d";"d"]));
+%!assert (all(strvcat ({100,100}) == ["d";"d"]));
+%!assert (all(strvcat ([100,100]) == ["dd"]));
+%!assert (all(strvcat ({100,{100}}) == ["d";"d"]));
+%!assert (all(strvcat (100, [], 100) == ["d";"d"]))
+%!assert (all(strvcat ({100, [], 100}) == ["d";"d"]))
+%!assert (all(strvcat ({100,{100, {""}}}) == ["d";"d"]))
+%!assert (all(strvcat (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"]))
+%!assert(strcmp (strvcat ("a", "bb", "ccc"), ["a  "; "bb "; "ccc"]));
+*/
+
 
 DEFUN (ischar, args, ,
   "-*- texinfo -*-\n\