changeset 12925:1c71c9bf0570

deblank.m: Speed up 15x on cellstr inputs. Restrict input to strings or cellstrings. * deblank.m: Replace cellfun call with regexprep for 15X speedup. Validate input is a string or cellstr. Correct and conolidate tests. * strtrim.m: Change input validation message to say that input must be string or cellstring.
author Rik <octave@nomad.inbox5.com>
date Fri, 05 Aug 2011 10:01:18 -0700
parents 1322308fa83a
children 838ada8779a5
files scripts/strings/deblank.m scripts/strings/strtrim.m
diffstat 2 files changed, 18 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/strings/deblank.m
+++ b/scripts/strings/deblank.m
@@ -18,7 +18,7 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} deblank (@var{s})
-## Remove trailing blanks and nulls from @var{s}.  If @var{s}
+## Remove trailing whitespace and nulls from @var{s}.  If @var{s}
 ## is a matrix, @var{deblank} trims each row to the length of longest
 ## string.  If @var{s} is a cell array, operate recursively on each
 ## element of the cell array.
@@ -33,53 +33,33 @@
     print_usage ();
   endif
 
-  char_arg = ischar (s);
-
-  if (char_arg || isnumeric (s))
+  if (ischar (s))
 
-    if (! isempty (s))
-      if (char_arg)
-        k = find (! isspace (s) & s != "\0");
-      else
-        warning ("deblank: expecting character string argument");
-        k = find (s != 0);
-      endif
-
-      if (isempty (k))
-        s = resize (s, 0, 0);
-      else
-        s = s(:,1:ceil (max (k) / rows (s)));
-      endif
+    k = find (! isspace (s) & s != "\0");
+    if (isempty (s) || isempty (k))
+      s = "";
+    else
+      s = s(:,1:ceil (max (k) / rows (s)));
     endif
 
-  elseif (iscell(s))
+  elseif (iscell (s))
 
-    s = cellfun (@deblank, s, "uniformoutput", false);
+    s = regexprep (s, "[\\s\v\\0]+$", '');
 
   else
-    error ("deblank: expecting character string argument");
+    error ("deblank: S argument must be a string or cellstring");
   endif
 
 endfunction
 
-%!assert (strcmp (deblank (" f o o  "), " f o o"));
 
-%!assert (deblank ([]), [])
-%!assert (deblank ({}), {})
-%!assert (deblank (""), "")
-
-%!assert (deblank ([0,0,0]), [])
+%!assert (strcmp (deblank (" f o o \0"), " f o o"));
 %!assert (deblank ('   '), '')
 %!assert (deblank ("   "), "")
-
-%!assert (typeinfo (deblank ("   ")), "string")
-%!assert (typeinfo (deblank ('   ')), "sq_string")
+%!assert (deblank (""), "")
+%!assert (deblank ({}), {})
 
-%!assert (deblank ([1,2,0]), [1,2])
-%!assert (deblank ([1,2,0,32]), [1,2,0,32])
+%!error <Invalid call to deblank> deblank ();
+%!error <Invalid call to deblank> deblank ("foo", "bar");
+%!error <argument must be a string> deblank (1);
 
-%!assert (deblank (int8 ([1,2,0])), int8 ([1,2]))
-
-%!error deblank ();
-
-%!error deblank ("foo", "bar");
--- a/scripts/strings/strtrim.m
+++ b/scripts/strings/strtrim.m
@@ -53,12 +53,12 @@
       s = s(:, ceil (min (k) / rows (s)):ceil (max (k) / rows (s)));
     endif
 
-  elseif (iscell(s))
+  elseif (iscell (s))
 
     s = regexprep (s, "^[\\s\v]+|[\\s\v]+$", '');
 
   else
-    error ("strtrim: S argument must be a string");
+    error ("strtrim: S argument must be a string or cellstring");
   endif
 
 endfunction