function y = char(varargin) %#codegen
%MATLAB Code Generation Library Function
%
% Limitations:
%
%   1. Cell input not supported
%   2. Enum inputs must be compile-time scalars

%   Copyright 1984-2020 The MathWorks, Inc.
%#codegen
coder.internal.allowEnumInputs();
coder.internal.prefer_const(varargin);
narginchk(0,Inf);
if nargin == 1
    s = varargin{1};
    coder.internal.assert(~iscell(s), 'Coder:toolbox:CellArraysNotSupported', mfilename);
    coder.internal.assert(~islogical(s), 'Coder:builtins:LogicalToChar');
    if ischar(s)
        coder.inline('always');
        y = s;
    elseif isenum(s)
        coder.internal.assert(coder.internal.isConstTrue(isscalar(s)), ...
            'Coder:toolbox:EnumMustBeScalar', mfilename);
        y = coder.internal.enumToChar(s);
    else
        coder.internal.assert(isnumeric(s), 'Coder:builtins:ConversionNotPossible', mfilename, class(s));
        y = coder.nullcopy(eml_expand(' ',size(s)));
        y(:) = s(:);
    end
else
    m = charnrows(varargin{:});
    n = charncols(varargin{:});
    y = eml_expand(' ',[m,n]);
    row_offset = 0;
    for k = coder.unroll(1:nargin)
        ak = varargin{k};
        coder.internal.errorIf(isenum(ak), 'MATLAB:class:UndefinedMethod', mfilename, class(ak));
        coder.internal.assert(ischar(ak) || isnumeric(ak), 'MATLAB:character:InvalidInput');
        [mk,nk] = size(ak);
        for i = 1:mk
            for j = 1:nk
                y(i+row_offset,j) = ak(i,j);
            end
        end
        row_offset = row_offset + mk;
    end
end

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

function m = charnrows(varargin)
coder.internal.prefer_const(varargin);
if nargin == 0
    m = 0;
else
    m = size(varargin{1},1);
    for k = coder.unroll(2:nargin)
        m = m + size(varargin{k},1);
    end
end

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

function n = charncols(varargin)
coder.internal.prefer_const(varargin);
if nargin == 0
    n = 0;
else
    [~,n] = size(varargin{1});
    for k = coder.unroll(2:nargin)
        [~,nk] = size(varargin{k});
        if nk > n
            n = nk;
        end
    end
end

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