function y = strtrim(x)
%MATLAB Code Generation Library Function

%   Limitations:
%   1. Only supports 'char' inputs, cell array not supported.
%   2. Input values must be in the range 0-127.

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

eml_invariant(nargin > 0, ...
    'MATLAB:minrhs');
coder.internal.prefer_const(x);
if ~isstring(x) && ~ischar(x) && coder.internal.isAmbiguousTypes
    y = coder.ignoreConst(x);
    return
end
eml_invariant(~iscell(x), 'Coder:toolbox:CellArraysNotSupported', mfilename);
eml_invariant(ischar(x), ...
    'Coder:toolbox:unsupportedClass','strtrim',class(x));
eml_invariant(ismatrix(x), ...
    'MATLAB:strtrim:TooManyDimensions');
j1 = firstcol(x);
j2 = lastcol(x);
y = x(:,j1:j2);

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

function n = firstcol(x)
coder.internal.prefer_const(x);
n = coder.internal.indexInt(1);
while n <= size(x,2) && allwspace(x,n)
    n = n + 1;
end

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

function n = lastcol(x)
coder.internal.prefer_const(x);
n = coder.internal.indexInt(size(x,2));
while n > 0 && allwspace(x,n)
    n = n - 1;
end

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

function p = allwspace(x,col)
coder.internal.prefer_const(x);
coder.inline('always');
for i = 1:size(x,1)
    if ~isstrprop(x(i,col),'wspace') || x(i, col) == char(0)
        p = false;
        return
    end
end
p = true;

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