annotate scripts/strings/strjust.m @ 10018:fb8834c12035

fix & improve strjust
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 23 Dec 2009 14:25:51 +0100
parents 58604c45ca74
children ffee051323f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8442
diff changeset
1 ## Copyright (C) 2000, 2001, 2003, 2005, 2006, 2007, 2009 Paul Kienzle
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
2 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
4 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
9 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
14 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
15 ## 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
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
18
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {} strjust (@var{s}, ["left"|"right"|"center"])
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
21 ## Shift the non-blank text of @var{s} to the left, right or center of
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
22 ## the string. If @var{s} is a string array, justify each string in the
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
23 ## array. Null characters are replaced by blanks. If no justification
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
24 ## is specified, then all rows are right-justified. For example:
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
25 ##
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
26 ## @example
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
27 ## @group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
28 ## strjust (["a"; "ab"; "abc"; "abcd"])
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
29 ## @result{} ans =
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
30 ## a
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
31 ## ab
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
32 ## abc
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
33 ## abcd
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
34 ## @end group
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
35 ## @end example
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
36 ## @end deftypefn
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
37
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
38 function x = strjust (x, just)
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
39
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
40 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5568
diff changeset
41 print_usage ();
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
42 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
43
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
44 if (nargin == 1)
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
45 just = "right";
10018
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
46 else
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
47 just = tolower (just);
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
48 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
49
10018
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
50 if (ndims (x) != 2)
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
51 error ("needs a string or character matrix");
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
52 endif
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
53
10018
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
54 if (isempty (x))
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
55 return
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
56 endif
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
57
10018
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
58 if (rows (x) == 1)
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
59 ## string case
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
60 nonbl = x != " " & x != char (0);
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
61 n = length (x);
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
62 switch (just)
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
63 case "right"
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
64 idx = find (nonbl, 1, "last");
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
65 if (idx < n) # false if idx is empty
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
66 x = [" "(1, ones (1, n-idx)), x(1:idx)];
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
67 endif
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
68 case "left"
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
69 idx = find (nonbl, 1, "first");
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
70 if (idx > 1) # false if idx is empty
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
71 x = [x(idx:n), " "(1, ones (1, idx))];
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
72 endif
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
73 case "center"
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
74 idx = find (nonbl, 1, "first");
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
75 jdx = find (nonbl, 1, "last");
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
76 if (idx > 1 || jdx < n)
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
77 nsp = ((idx - 1) + (n - jdx)) / 2;
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
78 lpad = " "(1, ones (1, floor (nsp)));
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
79 rpad = " "(1, ones (1, ceil (nsp)));
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
80 x = [lpad, x(idx:jdx), rpad];
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
81 endif
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
82 otherwise
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
83 error ("strjust: invalid justify type: %s", just);
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
84 endswitch
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
85 else
10018
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
86 ## char matrix case.
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
87
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
88 ## For all cases, left, right and center, the algorithm is the same.
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
89 ## Find the number of blanks at the left/right end to determine the
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
90 ## shift, rotate the row index by using mod with that shift, then
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
91 ## translate the shifted row index into an array index.
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
92 [nr, nc] = size (x);
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
93 idx = (x != " " & x != char (0)).';
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
94 if (strcmp (just, "right"))
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
95 [N, hi] = max (cumsum (idx));
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
96 shift = hi;
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
97 elseif (strcmp (just, "left"))
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
98 [N, lo] = max (cumsum (flipud (idx)));
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
99 shift = (nc - lo);
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
100 else
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
101 [N, hi] = max (cumsum (idx));
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
102 [N, lo] = max (cumsum (flipud (idx)));
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
103 shift = ceil (nc - (lo-hi)/2);
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
104 endif
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
105 idx = rem (ones(nr,1)*[0:nc-1] + shift'*ones(1,nc), nc);
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
106 x = x (idx*nr + [1:nr]'*ones(1,nc));
fb8834c12035 fix & improve strjust
Jaroslav Hajek <highegg@gmail.com>
parents: 9036
diff changeset
107
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
108 endif
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
109
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
110 endfunction
8442
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
111
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
112 %!error <Invalid call to strjust> strjust();
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
113 %!error <Invalid call to strjust> strjust(["a";"ab"], "center", 1);
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
114 %!assert (strjust (["a"; "ab"; "abc"; "abcd"]),
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
115 %! [" a";" ab"; " abc"; "abcd"]);
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
116 %!assert (strjust (["a"; "ab"; "abc"; "abcd"], "center"),
502e58a0d44f Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7017
diff changeset
117 %! [" a "; " ab"; "abc "; "abcd"]);