changeset 13158:a1049e4480f8

strtrim.m: Allow operation on nested cellstr arrays (Bug #34123). * strtrim.m: Divide work between regexprep on string portions of input and recursive cellfun call on cell elements.
author Rik <octave@nomad.inbox5.com>
date Sun, 18 Sep 2011 20:13:56 -0700
parents 8c7caa009a1e
children a8184fb6b0c7
files scripts/strings/strtrim.m
diffstat 1 files changed, 13 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/strings/strtrim.m	Sun Sep 18 17:09:39 2011 -0700
+++ b/scripts/strings/strtrim.m	Sun Sep 18 20:13:56 2011 -0700
@@ -39,7 +39,7 @@
 
 ## This function was derived from deblank.
 
-function s = strtrim (s)
+function s = strtrim2 (s)
 
   if (nargin != 1)
     print_usage ();
@@ -54,9 +54,18 @@
       s = s(:, ceil (min (k) / rows (s)):ceil (max (k) / rows (s)));
     endif
 
-  elseif (iscellstr (s))
+  elseif (iscell (s))
 
-    s = regexprep (s, "^[\\s\v]+|[\\s\v]+$", '');
+    char_idx = cellfun ("isclass", s, "char");
+    cell_idx = cellfun ("isclass", s, "cell");
+    if (! all (char_idx | cell_idx))  
+      error ("strtrim: S argument must be a string or cellstring");
+    endif
+
+    ## Divide work load.  Recursive cellfun strtrim call is slow
+    ## and avoided where possible.
+    s(char_idx) = regexprep (s(char_idx), "^[\\s\v]+|[\\s\v]+$", '');
+    s(cell_idx) = cellfun ("strtrim", s(cell_idx), "UniformOutput", false);
 
   else
     error ("strtrim: S argument must be a string or cellstring");
@@ -70,6 +79,7 @@
 %!assert (strtrim ("abc"), "abc");
 %!assert (strtrim ([" abc   "; "   def   "]), ["abc  "; "  def"]);
 %!assert (strtrim ({" abc   "; "   def   "}), {"abc"; "def"});
+%!assert (strtrim ({" abc   ", {"   def   "}}), {"abc", {"def"}});
 
 %!error <Invalid call to strtrim> strtrim ();
 %!error <Invalid call to strtrim> strtrim ("abc", "def");