annotate scripts/strings/deblank.m @ 33617:ec2635a02328 bytecode-interpreter tip

maint: Merge default to bytecode-interpreter.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 21 May 2024 18:29:03 +0200
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 1996-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2314
diff changeset
7 ##
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24461
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24461
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22252
diff changeset
13 ## (at your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22252
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22252
diff changeset
18 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
19 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24461
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
2268
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
25
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3215
diff changeset
26 ## -*- texinfo -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{s} =} deblank (@var{s})
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
28 ## Remove trailing whitespace and nulls from @var{s}.
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
29 ##
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
30 ## If @var{s} is a matrix, @var{deblank} trims each row to the length of
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
31 ## the longest string. If @var{s} is a cell array of strings, operate
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
32 ## recursively on each string element.
12981
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
33 ##
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
34 ## Examples:
13929
9cae456085c2 Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13162
diff changeset
35 ##
12981
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
36 ## @example
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
37 ## @group
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
38 ## deblank (" abc ")
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
39 ## @result{} " abc"
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
40 ##
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
41 ## deblank ([" abc "; " def "])
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
42 ## @result{} [" abc " ; " def"]
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
43 ## @end group
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
44 ## @end example
4ec4096f65d1 doc: State that required input is cellstr, not cell, for strtrim and deblank (Bug #34038)
Rik <octave@nomad.inbox5.com>
parents: 12975
diff changeset
45 ## @seealso{strtrim}
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3215
diff changeset
46 ## @end deftypefn
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
47
5462
74804828df1a [project @ 2005-09-22 18:36:22 by jwe]
jwe
parents: 5443
diff changeset
48 function s = deblank (s)
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2314
diff changeset
49
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
50 if (nargin != 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6023
diff changeset
51 print_usage ();
2268
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
52 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2314
diff changeset
53
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
54 if (isempty (s))
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
55
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
56 ## Return empty objects unchanged (Matlab compatibility)
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
57
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
58 elseif (ischar (s))
2268
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
59
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
60 ## Find indices of non-whitespace characters. If s is a
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
61 ## char matrix, the indices are in column major order.
12925
1c71c9bf0570 deblank.m: Speed up 15x on cellstr inputs. Restrict input to strings or cellstrings.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
62 k = find (! isspace (s) & s != "\0");
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
63 if (isempty (k))
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
64 ## Even if s was a char matrix! (Matlab compatibility)
12925
1c71c9bf0570 deblank.m: Speed up 15x on cellstr inputs. Restrict input to strings or cellstrings.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
65 s = "";
1c71c9bf0570 deblank.m: Speed up 15x on cellstr inputs. Restrict input to strings or cellstrings.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
66 else
1c71c9bf0570 deblank.m: Speed up 15x on cellstr inputs. Restrict input to strings or cellstrings.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
67 s = s(:,1:ceil (max (k) / rows (s)));
2268
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
68 endif
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
69
13162
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
70 elseif (iscell (s))
5462
74804828df1a [project @ 2005-09-22 18:36:22 by jwe]
jwe
parents: 5443
diff changeset
71
13162
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
72 char_idx = cellfun ("isclass", s, "char");
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
73 cell_idx = cellfun ("isclass", s, "cell");
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
74 empty_idx = cellfun ("isempty", s);
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
75 if (! all (char_idx | cell_idx | empty_idx))
13162
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
76 error ("deblank: S argument must be a string or cellstring");
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
77 endif
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
78
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
79 ## Divide work load. Recursive cellfun deblank call is slow
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
80 ## and avoided where possible.
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
81 s(char_idx) = regexprep (s(char_idx), "[\\s\v\\0]+$", '');
4e92b71dcc97 deblank.m: Modified to accept nested cellstr arrays (Bug #34123)
Rik <octave@nomad.inbox5.com>
parents: 12981
diff changeset
82 s(cell_idx) = cellfun ("deblank", s(cell_idx), "UniformOutput", false);
5462
74804828df1a [project @ 2005-09-22 18:36:22 by jwe]
jwe
parents: 5443
diff changeset
83
2268
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
84 else
12925
1c71c9bf0570 deblank.m: Speed up 15x on cellstr inputs. Restrict input to strings or cellstrings.
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
85 error ("deblank: S argument must be a string or cellstring");
2268
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
86 endif
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
87
ee5ec3133ed3 [project @ 1996-05-24 00:53:19 by jwe]
jwe
parents:
diff changeset
88 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
89
7429
744f522dbabe [project @ 2008-01-30 07:44:07 by jwe]
jwe
parents: 7411
diff changeset
90
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
91 %!assert (deblank (" f o o \0"), " f o o")
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
92 %!assert (deblank (" \t f o o \t \0"), " \t f o o")
24461
0a034d34a686 Modify BIST tests to pass when string_fill_char is not ' '.
Rik <rik@octave.org>
parents: 23220
diff changeset
93 %!assert (deblank (char (" abc ", " def ")), [" abc " ; " def"])
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
94 %!assert (deblank ([" "; " "]), "")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
95 %!assert (deblank (' '), '')
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
96 %!assert (deblank (" "), "")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
97 %!assert (deblank (""), "")
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
98 %!assert (deblank ([]), [])
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
99 %!assert (deblank ({}), {})
22252
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
100 %!assert (deblank ({[]}), {[]})
54dc5062879e deblank.m: Matlab compatible behavior for empty input (bug #48725)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
101 %!assert (deblank ({[], []}), {[], []})
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
102 %!assert (deblank ({" abc ", {" def "}}), {" abc", {" def"}})
7429
744f522dbabe [project @ 2008-01-30 07:44:07 by jwe]
jwe
parents: 7411
diff changeset
103
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
104 %!error <Invalid call to deblank> deblank ()
28564
1dd765e54265 fix tests for new argument number mismatch error message
John W. Eaton <jwe@octave.org>
parents: 27985
diff changeset
105 %!error <called with too many inputs> deblank ("foo", "bar")
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
106 %!error <argument must be a string> deblank (1)