# HG changeset patch # User Rik # Date 1312563678 25200 # Node ID 1c71c9bf057001fb6792dbaa310777072c8d3689 # Parent 1322308fa83ab8b887cc66358252d00e8a5bd30c 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. diff --git a/scripts/strings/deblank.m b/scripts/strings/deblank.m --- 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 deblank (); +%!error deblank ("foo", "bar"); +%!error deblank (1); -%!assert (deblank (int8 ([1,2,0])), int8 ([1,2])) - -%!error deblank (); - -%!error deblank ("foo", "bar"); diff --git a/scripts/strings/strtrim.m b/scripts/strings/strtrim.m --- 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