Mercurial > hg > octave-lyh
comparison scripts/strings/strjust.m @ 10020:ffee051323f8
rewrite strjust
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 24 Dec 2009 12:02:27 +0100 |
parents | fb8834c12035 |
children | d1978e7364ad |
comparison
equal
deleted
inserted
replaced
10019:7ad32bf759c3 | 10020:ffee051323f8 |
---|---|
1 ## Copyright (C) 2000, 2001, 2003, 2005, 2006, 2007, 2009 Paul Kienzle | 1 ## Copyright (C) 2000, 2001, 2003, 2005, 2006, 2007, 2009 Paul Kienzle |
2 ## Copyright (C) 2009 Jaroslav Hajek | |
2 ## | 3 ## |
3 ## This file is part of Octave. | 4 ## This file is part of Octave. |
4 ## | 5 ## |
5 ## Octave is free software; you can redistribute it and/or modify it | 6 ## 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 | 7 ## under the terms of the GNU General Public License as published by |
33 ## abcd | 34 ## abcd |
34 ## @end group | 35 ## @end group |
35 ## @end example | 36 ## @end example |
36 ## @end deftypefn | 37 ## @end deftypefn |
37 | 38 |
38 function x = strjust (x, just) | 39 function y = strjust (x, just) |
39 | 40 |
40 if (nargin < 1 || nargin > 2) | 41 if (nargin < 1 || nargin > 2) |
41 print_usage (); | 42 print_usage (); |
42 endif | 43 endif |
43 | 44 |
50 if (ndims (x) != 2) | 51 if (ndims (x) != 2) |
51 error ("needs a string or character matrix"); | 52 error ("needs a string or character matrix"); |
52 endif | 53 endif |
53 | 54 |
54 if (isempty (x)) | 55 if (isempty (x)) |
55 return | 56 y = x; |
56 endif | 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. | |
57 | 62 |
58 if (rows (x) == 1) | 63 [nr, nc] = size (x); |
59 ## string case | 64 ## Find the indices of all nonblanks. |
60 nonbl = x != " " & x != char (0); | 65 nonbl = x != " " & x != "\0"; |
61 n = length (x); | 66 [idx, jdx] = find (nonbl); |
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 | |
85 else | |
86 ## char matrix case. | |
87 | 67 |
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")) | 68 if (strcmp (just, "right")) |
95 [N, hi] = max (cumsum (idx)); | 69 ## We wish to find the maximum column index for each row. Because jdx is |
96 shift = hi; | 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; | |
97 elseif (strcmp (just, "left")) | 75 elseif (strcmp (just, "left")) |
98 [N, lo] = max (cumsum (flipud (idx))); | 76 ## See above for explanation. |
99 shift = (nc - lo); | 77 mins = ones (nr, 1); |
78 mins(flipud (idx(:))) = flipud (jdx(:)); | |
79 shift = 1 - mins; | |
100 else | 80 else |
101 [N, hi] = max (cumsum (idx)); | 81 ## Use both of the above. |
102 [N, lo] = max (cumsum (flipud (idx))); | 82 mins = ones (nr, 1); |
103 shift = ceil (nc - (lo-hi)/2); | 83 mins(flipud (idx(:))) = flipud (jdx(:)); |
84 maxs = nc * ones (nr, 1); | |
85 maxs(idx) = jdx; | |
86 shift = floor ((nc + 1 - maxs - mins) / 2); | |
104 endif | 87 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 | 88 |
89 ## Adjust the column indices. | |
90 jdx += shift (idx); | |
91 | |
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); | |
108 endif | 95 endif |
109 | 96 |
110 endfunction | 97 endfunction |
111 | 98 |
112 %!error <Invalid call to strjust> strjust(); | 99 %!error <Invalid call to strjust> strjust(); |