octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[changeset] Make strvcat an internal function


From: Thorsten Meyer
Subject: [changeset] Make strvcat an internal function
Date: Fri, 02 Jan 2009 10:03:56 +0100
User-agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018)

Hi,

here is a changeset that makes strvcat an internal function. I used the char() 
function in strfns.cc
as a template.

regards

Thorsten
# HG changeset patch
# User Thorsten Meyer <address@hidden>
# Date 1230884504 -3600
# Node ID 9427e828dcbdd4b47c1999694679d40ccc888702
# Parent  ba027376ee36709e71ee4c9b7d2fb75bcccaa4a7
Make strvcat an internal function

diff -r ba027376ee36 -r 9427e828dcbd scripts/ChangeLog
--- a/scripts/ChangeLog Thu Jan 01 13:13:26 2009 +0100
+++ b/scripts/ChangeLog Fri Jan 02 09:21:44 2009 +0100
@@ -0,0 +1,5 @@
+2009-01-02  Thorsten Meyer  <address@hidden>
+
+       * strings/strvcat.m: remove.
+       * strings/Makefile.in: remove strvcat.m.
+       
diff -r ba027376ee36 -r 9427e828dcbd scripts/strings/Makefile.in
--- a/scripts/strings/Makefile.in       Thu Jan 01 13:13:26 2009 +0100
+++ b/scripts/strings/Makefile.in       Fri Jan 02 09:21:44 2009 +0100
@@ -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))
diff -r ba027376ee36 -r 9427e828dcbd scripts/strings/strvcat.m
--- a/scripts/strings/strvcat.m Thu Jan 01 13:13:26 2009 +0100
+++ /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", address@hidden, ["num", "bers"])
-##     @result{} ans = 
-##        ["abc    "
-##         "98     "
-##         "99     "
-##         "d      "
-##         "numbers"]
-## @end group
-## @end example
-##
-## @seealso{char, cstrcat, strcat}
-## @end deftypefn
-
-## Author: Kurt Hornik <address@hidden>
-## Adapted-By: jwe
-## Modified: Paul Kienzle <address@hidden> converted
-##           str2mat to strvcat.  Same function except that strvcat
-##           ignores empty strings.
-## Modified by Alois Schloegl <address@hidden> 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]);
diff -r ba027376ee36 -r 9427e828dcbd src/ChangeLog
--- a/src/ChangeLog     Thu Jan 01 13:13:26 2009 +0100
+++ b/src/ChangeLog     Fri Jan 02 09:21:44 2009 +0100
@@ -0,0 +1,4 @@
+2009-01-02  Thorsten Meyer  <address@hidden>
+
+       * strfns.cc (Fstrvcap): Add function.
+       
diff -r ba027376ee36 -r 9427e828dcbd src/strfns.cc
--- a/src/strfns.cc     Thu Jan 01 13:13:26 2009 +0100
+++ b/src/strfns.cc     Fri Jan 02 09:21:44 2009 +0100
@@ -153,6 +153,123 @@
 %!assert(strcmp (char ("a", "bb", "ccc"), ["a  "; "bb "; "ccc"]));
 */
 
+DEFUN (strvcat, args, ,
+  "-*- texinfo -*-\n\
address@hidden {Built-in Function} {} strvcat (@var{x})\n\
address@hidden {Built-in Function} {} strvcat (@var{cell_array})\n\
address@hidden {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\
address@hidden
address@hidden
+strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", address@hidden, [\"num\", 
\"bers\"])\n\
+     @result{} [\"abc    \"\n\
+        \"98     \"\n\
+        \"99     \"\n\
+        \"d      \"\n\
+        \"numbers\"]\n\
address@hidden group\n\
address@hidden example\n\
+\n\
address@hidden, strcat, cstrcat}\n\
address@hidden 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\

reply via email to

[Prev in Thread] Current Thread [Next in Thread]