function libNames = addLibExtIfMissing(libNames, libExt)
% addLibExtIfMissing adds LIBEXT to LIBNAMES for elements that
% do not already have any extension. The check for any existing 
% extension is to test for the presence of '.' therefore LIBNAMES
% must not include a path because the path might include '.' or '..'

% Copyright 2020 The MathWorks, Inc.

% Identify entries with no file extension; potential enhancement to support nested library
% extensions, g2198785.
isTokenFcn = @(x) regexp(x, '\$\(.*\)', 'match', 'once');
if iscell(libNames)    
    addFileExtIdx = cellfun(@isempty, isTokenFcn(libNames)) & ~contains(libNames, '.');
else 
    addFileExtIdx = isempty(isTokenFcn(libNames)) && ~contains(libNames, '.');
end

% Add the library extension if needed
if iscell(libNames)
    libNames(addFileExtIdx) = ...
        regexprep(libNames(addFileExtIdx), '.*$', ['$0' libExt], 'once');
elseif addFileExtIdx
    libNames = regexprep(libNames, '.*$', ['$0' libExt], 'once');
end
