function identifier = createProbeIdentifier(str, maxShortNameLength)
% CREATEPROBEIDENTIFIER checks if the probe name contains valid ANSI C
% characters and its length isn't longer than maxShortNameLength

% Copyright 2012-2019 The MathWorks, Inc.

% replace invalid characters (non ANSI C) with underscore
str = regexprep(str, '[^a-zA-Z0-9]', '_');

if length(str) > maxShortNameLength 
    % get MD5 digest
    cs = coder.profile.md5([], str);
    cs = typecast(cs, 'uint16');
    assert(length(cs) >= 16, 'The MD5 digest must contain 16 characters at least');
    cs = dec2hex( cs, 2 );
    cs = lower(cs(1:16));
    identifier = [ str(1:maxShortNameLength-(1+length(cs))), '_', cs ];
else
    identifier = str;
end

% remove double underscores
identifier = regexprep(identifier,'_+', '_');
