function s = createPrintableScalar(B)
%MATLAB Code Generation Private Function
%
%   Convert B to character vector S appropriate for output as validator error
%   message. B must be a scalar and its type must be numeric, logical,
%   string, or enum.
%
%   Copyright 2016-2020 The MathWorks, Inc.
%#codegen

coder.internal.allowEnumInputs;
coder.internal.prefer_const(B);
VARSIZE = coder.const(~coder.internal.eml_option_eq('VariableSizing','DisableInInference'));
if ~coder.internal.isConstTrue(isscalar(B))
    s = '';
    return
end
if isenum(B)
    if VARSIZE || coder.internal.isConst(B)
        s = [class(B),'.',coder.internal.enumToChar(B)];
    else
        s = fixedSizeEnumChar(B);
    end
elseif isnumeric(B)
    if coder.internal.isConst(B)
        s = coder.const(feval('num2str',B));
    else
        s = coder.internal.num2str(B);
    end
elseif islogical(B)
    if B
        if VARSIZE
            s = 'true';
        else
            s = 'true ';
        end
    else
        s = 'false';
    end
elseif ischar(B)
    s = ['''',B,''''];
elseif isa(B,'string')
    s = ['''',char(B),''''];
end

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

function s = fixedSizeEnumChar(B)

[enumVals,enumNames] = enumeration(B);
maxNameLength = coder.const(fixedSizeEnumNameLength(enumNames));
cls = class(B);
classlen = coder.const(coder.internal.indexInt(length(cls)));
outlen = coder.const(classlen + 1 + maxNameLength);
s = blanks(outlen);
s(1:classlen) = cls;
s(classlen + 1) = '.';
nvals = coder.internal.indexInt(numel(enumVals));
for k = coder.unroll(1:nvals)
    if B == enumVals(k)
        name = coder.const(char(enumNames{k}));
        nameLen = coder.const(coder.internal.indexInt(length(name)));
        s(classlen + 1 + (1:nameLen)) = name;
        break
    end
end

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

function maxlen = fixedSizeEnumNameLength(enumNames)
coder.internal.prefer_const(enumNames);
maxlen = coder.internal.indexInt(0);
for k = 1:coder.internal.indexInt(numel(enumNames))
    maxlen = eml_max(maxlen,length(enumNames{k}));
end

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