Mercurial > hg > octave-nkf
annotate scripts/strings/deblank.m @ 11589:b0084095098e
missing semicolons in script files
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 18:26:09 -0500 |
parents | fd0a3ac60b0e |
children | 1c71c9bf0570 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 Kurt Hornik |
2325 | 2 ## |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2268 | 18 |
3361 | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} deblank (@var{s}) | |
5462 | 21 ## Remove trailing blanks 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, operate recursively on each | |
24 ## element of the cell array. | |
3361 | 25 ## @end deftypefn |
2311 | 26 |
5428 | 27 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 28 ## Adapted-By: jwe |
2314 | 29 |
5462 | 30 function s = deblank (s) |
2325 | 31 |
2268 | 32 if (nargin != 1) |
6046 | 33 print_usage (); |
2268 | 34 endif |
2325 | 35 |
7429 | 36 char_arg = ischar (s); |
37 | |
38 if (char_arg || isnumeric (s)) | |
2268 | 39 |
7429 | 40 if (! isempty (s)) |
41 if (char_arg) | |
10549 | 42 k = find (! isspace (s) & s != "\0"); |
7429 | 43 else |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
44 warning ("deblank: expecting character string argument"); |
10549 | 45 k = find (s != 0); |
7429 | 46 endif |
47 | |
48 if (isempty (k)) | |
10549 | 49 s = resize (s, 0, 0); |
7429 | 50 else |
10549 | 51 s = s(:,1:ceil (max (k) / rows (s))); |
7429 | 52 endif |
2268 | 53 endif |
54 | |
5462 | 55 elseif (iscell(s)) |
56 | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
57 s = cellfun (@deblank, s, "uniformoutput", false); |
5462 | 58 |
2268 | 59 else |
7429 | 60 error ("deblank: expecting character string argument"); |
2268 | 61 endif |
62 | |
63 endfunction | |
7411 | 64 |
7429 | 65 %!assert (strcmp (deblank (" f o o "), " f o o")); |
66 | |
67 %!assert (deblank ([]), []) | |
68 %!assert (deblank ({}), {}) | |
69 %!assert (deblank (""), "") | |
70 | |
71 %!assert (deblank ([0,0,0]), []) | |
72 %!assert (deblank (' '), '') | |
73 %!assert (deblank (" "), "") | |
74 | |
75 %!assert (typeinfo (deblank (" ")), "string") | |
76 %!assert (typeinfo (deblank (' ')), "sq_string") | |
77 | |
78 %!assert (deblank ([1,2,0]), [1,2]) | |
79 %!assert (deblank ([1,2,0,32]), [1,2,0,32]) | |
80 | |
81 %!assert (deblank (int8 ([1,2,0])), int8 ([1,2])) | |
7411 | 82 |
83 %!error deblank (); | |
84 | |
85 %!error deblank ("foo", "bar"); |