]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/deblank.m
update packages
[CreaPhase.git] / octave_packages / m / strings / deblank.m
1 ## Copyright (C) 1996-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} deblank (@var{s})
21 ## Remove trailing whitespace and nulls from @var{s}.  If @var{s}
22 ## is a matrix, @var{deblank} trims each row to the length of longest
23 ## string.  If @var{s} is a cell array of strings, operate recursively on each
24 ## string element.
25 ##
26 ## Examples:
27 ##
28 ## @example
29 ## @group
30 ## deblank ("    abc  ")
31 ##      @result{}  "    abc"
32 ##
33 ## deblank ([" abc   "; "   def   "])
34 ##      @result{}  [" abc  " ; "   def"]
35 ## @end group
36 ## @end example
37 ## @seealso{strtrim}
38 ## @end deftypefn
39
40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
41 ## Adapted-By: jwe
42
43 function s = deblank (s)
44
45   if (nargin != 1)
46     print_usage ();
47   endif
48
49   if (ischar (s))
50
51     k = find (! isspace (s) & s != "\0");
52     if (isempty (s) || isempty (k))
53       s = "";
54     else
55       s = s(:,1:ceil (max (k) / rows (s)));
56     endif
57
58   elseif (iscell (s))
59
60     char_idx = cellfun ("isclass", s, "char");
61     cell_idx = cellfun ("isclass", s, "cell");
62     if (! all (char_idx | cell_idx))  
63       error ("deblank: S argument must be a string or cellstring");
64     endif
65
66     ## Divide work load.  Recursive cellfun deblank call is slow
67     ## and avoided where possible.
68     s(char_idx) = regexprep (s(char_idx), "[\\s\v\\0]+$", '');
69     s(cell_idx) = cellfun ("deblank", s(cell_idx), "UniformOutput", false);
70
71   else
72     error ("deblank: S argument must be a string or cellstring");
73   endif
74
75 endfunction
76
77
78 %!assert (deblank (" f o o \0"), " f o o");
79 %!assert (deblank ('   '), '');
80 %!assert (deblank ("   "), "");
81 %!assert (deblank (""), "");
82 %!assert (deblank ({}), {});
83 %!assert (deblank ({" abc   ", {"   def   "}}), {" abc", {"   def"}});
84
85 %!error <Invalid call to deblank> deblank ();
86 %!error <Invalid call to deblank> deblank ("foo", "bar");
87 %!error <argument must be a string> deblank (1);
88 %!error <argument must be a string> deblank ({[]});
89