function x = strjust(x,justify)
%MATLAB Code Generation Library Function

% Limitations:
% 
%   1. Does not support a cell array of strings for first input

%   Copyright 1984-2015 The MathWorks, Inc.
%#codegen

eml_invariant(nargin > 0, ...
    'MATLAB:minrhs');
coder.internal.prefer_const(x);
eml_invariant(~iscell(x), 'Coder:toolbox:CellArraysNotSupported', mfilename);
eml_invariant(ischar(x), ...
    'Coder:toolbox:unsupportedClass','strjust',class(x));
eml_invariant(ismatrix(x), ...
    'Coder:toolbox:strjust_inputMustBe2D');
if nargin < 2
    justify = 'right';
else
    coder.internal.prefer_const(justify);
end
RIGHT = nargin < 2 || strcmpi(justify,'right');
LEFT = ~RIGHT && strcmpi(justify,'left');
CENTER = ~LEFT && ~RIGHT && strcmpi(justify,'center');
eml_invariant(RIGHT || LEFT || CENTER, ...
    'MATLAB:strjust:UnknownParameter');
m = coder.internal.indexInt(size(x,1));
n = coder.internal.indexInt(size(x,2));
if isempty(x)
    return
elseif RIGHT
    for i = 1:m
        shift = n - find_last_nonspace_in_row(x,i);
        x = shift_row_right(x,i,shift);
    end
elseif LEFT
    for i = 1:m
        shift = find_first_nonspace_in_row(x,i) - 1;
        x = shift_row_left(x,i,shift);
    end
else % if CENTER
    for i = 1:m
        j2 = find_last_nonspace_in_row(x,i);
        if j2 > 0
            j1 = find_first_nonspace_in_row(x,i);
            len = j2 - j1 + 1;
            leading_spaces = coder.internal.indexDivide(n-len,2);
            if j1 > leading_spaces
                x = shift_row_left(x,i,j1-leading_spaces-1);
            else
                x = shift_row_right(x,i,leading_spaces-j1+1);
            end
        end
    end
end

%--------------------------------------------------------------------------

function j = find_first_nonspace_in_row(x,i)
% Returns j = size(x,2)+1 if row i is all whitespace.
n = coder.internal.indexInt(size(x,2));
j = coder.internal.indexInt(1);
% MATLAB does not use ISSPACE here.
while j <= n && x(i,j) == ' ' % isspace(x(i,j))
    j = j + 1;
end

%--------------------------------------------------------------------------

function j = find_last_nonspace_in_row(x,i)
% Returns j = 0 if row i is all whitespace.
j = coder.internal.indexInt(size(x,2));
% MATLAB does not use ISSPACE here.
while j > 0 && x(i,j) == ' ' % isspace(x(i,j))
    j = j - 1;
end

%--------------------------------------------------------------------------

function x = shift_row_left(x,i,shift)
n = coder.internal.indexInt(size(x,2));
if shift > 0 && shift < n
    for j = 1:n-shift
        x(i,j) = x(i,j+shift);
    end
    for j = n-shift+1:n
        x(i,j) = ' ';
    end
end

%--------------------------------------------------------------------------

function x = shift_row_right(x,i,shift)
n = coder.internal.indexInt(size(x,2));
if shift > 0 && shift < n
    for j = n:-1:shift+1
        x(i,j) = x(i,j-shift);
    end
    for j = 1:shift
        x(i,j) = ' ';
    end
end

%--------------------------------------------------------------------------
