Mercurial > octave-antonio
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 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2003, 2005, 2006, 2007, 2009 Paul Kienzle |
3789 | 2 ## |
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. | |
3789 | 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/>. | |
3789 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} strjust (@var{s}, ["left"|"right"|"center"]) | |
21 ## Shift the non-blank text of @var{s} to the left, right or center of | |
22 ## the string. If @var{s} is a string array, justify each string in the | |
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 | 36 ## @end deftypefn |
37 | |
38 function x = strjust (x, just) | |
39 | |
40 if (nargin < 1 || nargin > 2) | |
6046 | 41 print_usage (); |
3789 | 42 endif |
43 | |
44 if (nargin == 1) | |
45 just = "right"; | |
10018 | 46 else |
47 just = tolower (just); | |
3789 | 48 endif |
49 | |
10018 | 50 if (ndims (x) != 2) |
51 error ("needs a string or character matrix"); | |
52 endif | |
3789 | 53 |
10018 | 54 if (isempty (x)) |
55 return | |
5568 | 56 endif |
3789 | 57 |
10018 | 58 if (rows (x) == 1) |
59 ## string case | |
60 nonbl = x != " " & x != char (0); | |
61 n = length (x); | |
62 switch (just) | |
63 case "right" | |
64 idx = find (nonbl, 1, "last"); | |
65 if (idx < n) # false if idx is empty | |
66 x = [" "(1, ones (1, n-idx)), x(1:idx)]; | |
67 endif | |
68 case "left" | |
69 idx = find (nonbl, 1, "first"); | |
70 if (idx > 1) # false if idx is empty | |
71 x = [x(idx:n), " "(1, ones (1, idx))]; | |
72 endif | |
73 case "center" | |
74 idx = find (nonbl, 1, "first"); | |
75 jdx = find (nonbl, 1, "last"); | |
76 if (idx > 1 || jdx < n) | |
77 nsp = ((idx - 1) + (n - jdx)) / 2; | |
78 lpad = " "(1, ones (1, floor (nsp))); | |
79 rpad = " "(1, ones (1, ceil (nsp))); | |
80 x = [lpad, x(idx:jdx), rpad]; | |
81 endif | |
82 otherwise | |
83 error ("strjust: invalid justify type: %s", just); | |
84 endswitch | |
5568 | 85 else |
10018 | 86 ## char matrix case. |
87 | |
88 ## For all cases, left, right and center, the algorithm is the same. | |
89 ## Find the number of blanks at the left/right end to determine the | |
90 ## shift, rotate the row index by using mod with that shift, then | |
91 ## translate the shifted row index into an array index. | |
92 [nr, nc] = size (x); | |
93 idx = (x != " " & x != char (0)).'; | |
94 if (strcmp (just, "right")) | |
95 [N, hi] = max (cumsum (idx)); | |
96 shift = hi; | |
97 elseif (strcmp (just, "left")) | |
98 [N, lo] = max (cumsum (flipud (idx))); | |
99 shift = (nc - lo); | |
100 else | |
101 [N, hi] = max (cumsum (idx)); | |
102 [N, lo] = max (cumsum (flipud (idx))); | |
103 shift = ceil (nc - (lo-hi)/2); | |
104 endif | |
105 idx = rem (ones(nr,1)*[0:nc-1] + shift'*ones(1,nc), nc); | |
106 x = x (idx*nr + [1:nr]'*ones(1,nc)); | |
107 | |
5568 | 108 endif |
3789 | 109 |
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"]); |