octave-maintainers
[Top][All Lists]
Advanced

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

Simplification of blanks.m and strcat.m


From: Bill Denney
Subject: Simplification of blanks.m and strcat.m
Date: Tue, 31 Oct 2006 00:22:17 -0500
User-agent: Thunderbird 1.5.0.7 (Windows/20060909)

Both the blanks.m and strcat.m functions were more complex than necessary. Here are simplifications (that will hopefully also speed them up).

scripts/ChangeLog:

2006-10-31  Bill Denney  <address@hidden>
* scripts/blanks.m, scripts/strcat.m: simplify the functions, speed them up, and add some tests
Index: blanks.m
===================================================================
RCS file: /cvs/octave/scripts/strings/blanks.m,v
retrieving revision 1.17
diff -u -r1.17 blanks.m
--- blanks.m    10 Oct 2006 16:10:31 -0000      1.17
+++ blanks.m    31 Oct 2006 05:19:34 -0000
@@ -1,4 +1,4 @@
-## Copyright (C) 1996 Kurt Hornik
+## Copyright (C) 1996, 2006 Kurt Hornik
 ##
 ## This file is part of Octave.
 ##
@@ -20,6 +20,7 @@
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} blanks (@var{n})
 ## Return a string of @var{n} blanks.
+## @seealso{repmat}
 ## @end deftypefn
 
 ## Author: Kurt Hornik <address@hidden>
@@ -29,12 +30,15 @@
 
   if (nargin != 1)
     print_usage ();
-  endif
-
-  if (isscalar (n) && n == round (n))
-    s = char (ones (1, n) * toascii (" "));
-  else
+  elseif ~ (isscalar (n) && n == round (n))
     error ("blanks: n must be a non-negative integer");
   endif
 
+  s(1,1:n) = " ";
+
 endfunction
+
+## There really isn't that much to test here
+%!assert(blanks (0), "")
+%!assert(blanks (5), "     ")
+%!assert(blanks (10), "          ")
Index: strcat.m
===================================================================
RCS file: /cvs/octave/scripts/strings/strcat.m,v
retrieving revision 1.20
diff -u -r1.20 strcat.m
--- strcat.m    10 Oct 2006 16:10:31 -0000      1.20
+++ strcat.m    31 Oct 2006 05:19:34 -0000
@@ -1,4 +1,4 @@
-## Copyright (C) 1996, 1997 John W. Eaton
+## Copyright (C) 1996, 1997, 2006 John W. Eaton
 ##
 ## This file is part of Octave.
 ##
@@ -33,28 +33,20 @@
 
 ## Author: jwe
 
-function st = strcat (s, varargin)
+function st = strcat (varargin)
 
-  if (nargin > 0)
-    if (ischar (s))
-      tmpst = s;
-    else
-      error ("strcat: all arguments must be strings");
-    endif
-    n = nargin - 1;
-    k = 1;
-    while (n--)
-      tmp = varargin{k++};
-      if (ischar (tmp))
-       tmpst = [tmpst, tmp];
-      else
-       error ("strcat: all arguments must be strings");
-      endif
-    endwhile
-  else
+  if (nargin < 1)
     print_usage ();
+  elseif ~ iscellstr (varargin)
+    error ("strcat: all arguments must be strings");
   endif
 
-  st = tmpst;
+  st = [varargin{:}];
 
 endfunction
+
+## test the dimensionality
+## 1d
+%!assert(strcat("ab ", "ab "), "ab ab ")
+## 2d
+%!assert(strcat(["ab ";"cde"], ["ab ";"cde"]), ["ab ab ";"cdecde"])

reply via email to

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