8920
|
1 ## Copyright (C) 1996, 1998, 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008 |
7017
|
2 ## Kurt Hornik |
2325
|
3 ## |
2313
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
2268
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} deblank (@var{s}) |
5462
|
22 ## Remove trailing blanks and nulls from @var{s}. If @var{s} |
|
23 ## is a matrix, @var{deblank} trims each row to the length of longest |
|
24 ## string. If @var{s} is a cell array, operate recursively on each |
|
25 ## element of the cell array. |
3361
|
26 ## @end deftypefn |
2311
|
27 |
5428
|
28 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
29 ## Adapted-By: jwe |
2314
|
30 |
5462
|
31 function s = deblank (s) |
2325
|
32 |
2268
|
33 if (nargin != 1) |
6046
|
34 print_usage (); |
2268
|
35 endif |
2325
|
36 |
7429
|
37 char_arg = ischar (s); |
|
38 |
|
39 if (char_arg || isnumeric (s)) |
2268
|
40 |
7429
|
41 if (! isempty (s)) |
|
42 if (char_arg) |
|
43 k = find (! isspace (s) & s != "\0"); |
|
44 else |
|
45 warning ("deblank: expecting character string argument") |
|
46 k = find (s != 0); |
|
47 endif |
|
48 |
|
49 if (isempty (k)) |
|
50 s = resize (s, 0, 0); |
|
51 else |
|
52 s = s(:,1:ceil (max (k) / rows (s))); |
|
53 endif |
2268
|
54 endif |
|
55 |
5462
|
56 elseif (iscell(s)) |
|
57 |
6023
|
58 s = cellfun (@deblank, s, "UniformOutput", false); |
5462
|
59 |
2268
|
60 else |
7429
|
61 error ("deblank: expecting character string argument"); |
2268
|
62 endif |
|
63 |
|
64 endfunction |
7411
|
65 |
7429
|
66 %!assert (strcmp (deblank (" f o o "), " f o o")); |
|
67 |
|
68 %!assert (deblank ([]), []) |
|
69 %!assert (deblank ({}), {}) |
|
70 %!assert (deblank (""), "") |
|
71 |
|
72 %!assert (deblank ([0,0,0]), []) |
|
73 %!assert (deblank (' '), '') |
|
74 %!assert (deblank (" "), "") |
|
75 |
|
76 %!assert (typeinfo (deblank (" ")), "string") |
|
77 %!assert (typeinfo (deblank (' ')), "sq_string") |
|
78 |
|
79 %!assert (deblank ([1,2,0]), [1,2]) |
|
80 %!assert (deblank ([1,2,0,32]), [1,2,0,32]) |
|
81 |
|
82 %!assert (deblank (int8 ([1,2,0])), int8 ([1,2])) |
7411
|
83 |
|
84 %!error deblank (); |
|
85 |
|
86 %!error deblank ("foo", "bar"); |