time | Calls | line |
---|
| | 122 | function makeValidFcnHandle = getMakeValidFcnHandle(replacementStyle, prefix)
|
| | 123 |
|
< 0.001 | 5 | 124 | invalidCharsRegExp = '[^a-zA-Z_0-9]';
|
| | 125 |
|
| | 126 | function name = replaceWithUnderscore(name)
|
| | 127 | % Replace invalid characters with underscores.
|
| | 128 | name = regexprep(name, invalidCharsRegExp, '_');
|
| | 129 | end
|
| | 130 |
|
| | 131 | function name = deleteInvalidChars(name)
|
| | 132 | % Delete invalid characters.
|
| | 133 | name = regexprep(name, invalidCharsRegExp, '');
|
| | 134 | end
|
| | 135 |
|
| | 136 | function name = replaceWithHex(name)
|
| | 137 | % Replace invalid characters with their hex equivalent, 0x1234,
|
| | 138 | % for compatibility with legacy GENVARNAME conversion scheme.
|
| | 139 | illegalStringChars = regexp(name, invalidCharsRegExp, 'match', 'forceCellOutput');
|
| | 140 | for elementIdx = 1:numel(illegalStringChars)
|
| | 141 | illegalElementChars = unique(char(illegalStringChars{elementIdx}));
|
| | 142 | illegalCharWidths = 2 + 2 * (illegalElementChars > intmax('uint8'));
|
| | 143 | replacement = "0x" + arrayfun(@dec2hex, illegalElementChars, illegalCharWidths, 'UniformOutput', false);
|
| | 144 | name(elementIdx) = replace(name(elementIdx), string(illegalElementChars), replacement);
|
| | 145 | end
|
| | 146 | end
|
| | 147 |
|
| | 148 | function name = makeValid(name, invalidReplacementFun)
|
| | 149 | % Remove leading and trailing whitespace and
|
| | 150 | % replace embedded whitespace with camel/mixed casing.
|
| | 151 | whitespace = compose([" ", "\f", "\n", "\r", "\t", "\v"]);
|
| | 152 | if any(contains(name, whitespace))
|
| | 153 | name = regexprep(name, '(?<=\S)\s+([a-z])', '${upper($1)}');
|
| | 154 | name = erase(name, whitespace);
|
| | 155 | end
|
| | 156 |
|
| | 157 | % Replace invalid characters as specified by ReplacementStyle.
|
| | 158 | name = invalidReplacementFun(name);
|
| | 159 |
|
| | 160 | % Prepend keyword with PREFIX and camel case.
|
| | 161 | for keywordIdx = 1:numel(name)
|
| | 162 | if iskeyword(name(keywordIdx))
|
| | 163 | name{keywordIdx} = [prefix, upper(name{keywordIdx}(1)), ...
|
| | 164 | lower(name{keywordIdx}(2:end))];
|
| | 165 | end
|
| | 166 | end
|
| | 167 |
|
| | 168 | % Insert PREFIX if the first column is non-letter.
|
| | 169 | name = regexprep(name,'^(?![a-z])', prefix, 'emptymatch', 'ignorecase');
|
| | 170 |
|
| | 171 | % Truncate NAME to NAMLENGTHMAX.
|
| | 172 | isTooLong = (strlength(name) > namelengthmax);
|
| | 173 | if any(isTooLong)
|
| | 174 | for isTooLongIdx = reshape(find(isTooLong), 1, [])
|
| | 175 | name{isTooLongIdx} = name{isTooLongIdx}(1:namelengthmax);
|
| | 176 | end
|
| | 177 | end
|
| | 178 | end
|
| | 179 |
|
< 0.001 | 5 | 180 | switch(replacementStyle)
|
< 0.001 | 5 | 181 | case "underscore"
|
< 0.001 | 5 | 182 | makeValidFcnHandle = @(n)makeValid(n, @replaceWithUnderscore);
|
| | 183 | case "delete"
|
| | 184 | makeValidFcnHandle = @(n)makeValid(n, @deleteInvalidChars);
|
| | 185 | otherwise
|
| | 186 | assert(replacementStyle == "hex");
|
| | 187 | makeValidFcnHandle = @(n)makeValid(n, @replaceWithHex);
|
< 0.001 | 5 | 188 | end
|
| | 189 |
|
< 0.001 | 5 | 190 | end
|
Other subfunctions in this file are not included in this listing.