1########################################################################
2##
3## Copyright (C) 2000-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} strjust (@var{s})
28## @deftypefnx {} {} strjust (@var{s}, @var{pos})
29## Return the text, @var{s}, justified according to @var{pos}, which may
30## be @qcode{"left"}, @qcode{"center"}, or @qcode{"right"}.
31##
32## If @var{pos} is omitted it defaults to @qcode{"right"}.
33##
34## Null characters are replaced by spaces.  All other character data are
35## treated as non-white space.
36##
37## Example:
38##
39## @example
40## @group
41## strjust (["a"; "ab"; "abc"; "abcd"])
42##      @result{}
43##         "   a"
44##         "  ab"
45##         " abc"
46##         "abcd"
47## @end group
48## @end example
49## @seealso{deblank, strrep, strtrim, untabify}
50## @end deftypefn
51
52function y = strjust (s, pos = "right")
53
54  if (nargin < 1 || nargin > 2)
55    print_usage ();
56  elseif (! ischar (s) || ndims (s) > 2)
57    error ("strjust: S must be a string or 2-D character matrix");
58  endif
59
60  if (isempty (s))
61    y = s;
62    return;
63  endif
64
65  ## Apparently, Matlab considers nulls to be blanks as well; however, does
66  ## not preserve the nulls, but rather converts them to blanks.  That's a
67  ## bit unexpected, but it allows simpler processing, because we can move
68  ## just the nonblank characters.  So we'll do the same here.
69
70  [nr, nc] = size (s);
71  ## Find the indices of all nonblanks.
72  nonbl = s != " " & s != "\0";
73  [idx, jdx] = find (nonbl);
74
75  if (strcmpi (pos, "right"))
76    ## We wish to find the maximum column index for each row.  Because jdx is
77    ## sorted, we can take advantage of the fact that assignment is processed
78    ## sequentially and for duplicate indices the last value will remain.
79    maxs = repmat (nc, [nr, 1]);
80    maxs(idx) = jdx;
81    shift = nc - maxs;
82  elseif (strcmpi (pos, "left"))
83    ## See above for explanation.
84    mins = ones (nr, 1);
85    mins(flipud (idx(:))) = flipud (jdx(:));
86    shift = 1 - mins;
87  else
88    ## Use both of the above to achieve centering.
89    mins = ones (nr, 1);
90    mins(flipud (idx(:))) = flipud (jdx(:));
91    maxs = repmat (nc, [nr, 1]);
92    maxs(idx) = jdx;
93    shift = floor ((nc + 1 - maxs - mins) / 2);
94  endif
95
96  ## Adjust the column indices.
97  jdx += shift (idx);
98
99  ## Create a blank matrix and position the nonblank characters.
100  y = repmat (" ", nr, nc);
101  y(sub2ind ([nr, nc], idx, jdx)) = s(nonbl);
102
103endfunction
104
105
106%!assert (strjust (char ("a", "ab", "abc", "abcd")),
107%!        ["   a";"  ab"; " abc"; "abcd"])
108%!assert (strjust (char (" a", "  ab", "abc", "abcd"), "left"),
109%!        ["a   "; "ab  "; "abc "; "abcd"])
110%!assert (strjust (char ("a", "ab", "abc", "abcd"), "CENTER"),
111%!        [" a  "; " ab "; "abc "; "abcd"])
112%!assert (strjust (["";""]), "")
113
114## Test input validation
115%!error <Invalid call to strjust> strjust ()
116%!error <Invalid call to strjust> strjust (["a";"ab"], "center", 1)
117%!error <S must be a string> strjust (ones (3,3))
118%!error <S must be a string> strjust (char (ones (3,3,3)))
119