annotate scripts/strings/strjust.m @ 7016:93c65f2a5668

[project @ 2007-10-12 06:40:56 by jwe]
author jwe
date Fri, 12 Oct 2007 06:41:26 +0000
parents 34f96dd5441b
children a1dbe9d80eee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2000 Paul Kienzle
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
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
24 ## is specified, then all rows are right-justified.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
25 ## @end deftypefn
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
26
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
27 function x = strjust (x, just)
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
28
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
29 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5568
diff changeset
30 print_usage ();
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
31 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
32
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
33 if (nargin == 1)
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
34 just = "right";
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
35 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
36
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
37 just = tolower (just);
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
38
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
39 ## convert nulls to blanks
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
40 idx = find (toascii (x) == 0);
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
41 if (! isempty (idx))
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
42 x(idx) = " ";
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
43 endif
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
44
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
45 ## For all cases, left, right and center, the algorithm is the same.
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
46 ## Find the number of blanks at the left/right end to determine the
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
47 ## shift, rotate the row index by using mod with that shift, then
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
48 ## translate the shifted row index into an array index.
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
49 [nr, nc] = size (x);
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
50 idx = (x' != " ");
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
51 if (strcmp (just, "right"))
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
52 [N, hi] = max (cumsum (idx));
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
53 shift = hi;
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
54 elseif (strcmp (just, "left"))
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
55 [N, lo] = max (cumsum (flipud (idx)));
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
56 shift = (nc - lo);
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
57 else
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
58 [N, hi] = max (cumsum (idx));
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
59 [N, lo] = max (cumsum (flipud (idx)));
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
60 shift = ceil (nc - (lo-hi)/2);
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
61 endif
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
62 idx = rem (ones(nr,1)*[0:nc-1] + shift'*ones(1,nc), nc);
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
63 x = x (idx*nr + [1:nr]'*ones(1,nc));
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
64
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
65 endfunction