Mercurial > hg > octave-nkf
annotate scripts/strings/strjust.m @ 10509:ddbd812d09aa
properly compress sparse matrices after assembly
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Apr 2010 12:57:44 +0200 |
parents | ffee051323f8 |
children | d1978e7364ad |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2003, 2005, 2006, 2007, 2009 Paul Kienzle |
10020 | 2 ## Copyright (C) 2009 Jaroslav Hajek |
3789 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3789 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3789 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} strjust (@var{s}, ["left"|"right"|"center"]) | |
22 ## Shift the non-blank text of @var{s} to the left, right or center of | |
23 ## the string. If @var{s} is a string array, justify each string in the | |
24 ## 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
|
25 ## 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
|
26 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
27 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
28 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
29 ## 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
|
30 ## @result{} ans = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
31 ## a |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
32 ## ab |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
33 ## abc |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
34 ## abcd |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
35 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
36 ## @end example |
3789 | 37 ## @end deftypefn |
38 | |
10020 | 39 function y = strjust (x, just) |
3789 | 40 |
41 if (nargin < 1 || nargin > 2) | |
6046 | 42 print_usage (); |
3789 | 43 endif |
44 | |
45 if (nargin == 1) | |
46 just = "right"; | |
10018 | 47 else |
48 just = tolower (just); | |
3789 | 49 endif |
50 | |
10018 | 51 if (ndims (x) != 2) |
52 error ("needs a string or character matrix"); | |
53 endif | |
3789 | 54 |
10018 | 55 if (isempty (x)) |
10020 | 56 y = x; |
57 else | |
58 ## Apparently, Matlab considers nulls to be blanks as well; however, does | |
59 ## not preserve the nulls, but rather converts them to blanks. That's a | |
60 ## bit unexpected, but it allows simpler processing, because we can move | |
61 ## just the nonblank characters. So we'll do the same here. | |
62 | |
63 [nr, nc] = size (x); | |
64 ## Find the indices of all nonblanks. | |
65 nonbl = x != " " & x != "\0"; | |
66 [idx, jdx] = find (nonbl); | |
3789 | 67 |
10020 | 68 if (strcmp (just, "right")) |
69 ## We wish to find the maximum column index for each row. Because jdx is | |
70 ## sorted, we can take advantage of the fact that assignment is processed | |
71 ## sequentially and for duplicate indices the last value will remain. | |
72 maxs = nc * ones (nr, 1); | |
73 maxs(idx) = jdx; | |
74 shift = nc - maxs; | |
75 elseif (strcmp (just, "left")) | |
76 ## See above for explanation. | |
77 mins = ones (nr, 1); | |
78 mins(flipud (idx(:))) = flipud (jdx(:)); | |
79 shift = 1 - mins; | |
80 else | |
81 ## Use both of the above. | |
82 mins = ones (nr, 1); | |
83 mins(flipud (idx(:))) = flipud (jdx(:)); | |
84 maxs = nc * ones (nr, 1); | |
85 maxs(idx) = jdx; | |
86 shift = floor ((nc + 1 - maxs - mins) / 2); | |
87 endif | |
10018 | 88 |
10020 | 89 ## Adjust the column indices. |
90 jdx += shift (idx); | |
10018 | 91 |
10020 | 92 ## Create a blank matrix and position the nonblank characters. |
93 y = " "(ones (1, nr), ones (1, nc)); | |
94 y(sub2ind ([nr, nc], idx, jdx)) = x(nonbl); | |
5568 | 95 endif |
3789 | 96 |
97 endfunction | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
98 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
99 %!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
|
100 %!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
|
101 %!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
|
102 %! [" 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
|
103 %!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
|
104 %! [" a "; " ab"; "abc "; "abcd"]); |