|
4 | 4 | % |
5 | 5 | % Encode an invalid variable name using a hex-format for bi-directional |
6 | 6 | % conversions. |
7 | | - |
| 7 | +% |
8 | 8 | % This function is sensitive to the default charset |
9 | 9 | % settings in MATLAB, please call feature('DefaultCharacterSet','utf8') |
10 | 10 | % to set the encoding to UTF-8 before calling this function. |
|
37 | 37 | % License: GPLv3 or 3-clause BSD license, see https://github.com/NeuroJSON/easyh5 for details |
38 | 38 | % |
39 | 39 |
|
40 | | -if (~isvarname(str(1))) |
| 40 | +% Fast path: check first character directly instead of calling isvarname |
| 41 | +c1 = str(1); |
| 42 | +if ~((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) |
| 43 | + % First char is not a letter - need to encode it |
41 | 44 | if (exist('unicode2native', 'builtin')) |
42 | | - str = sprintf('x0x%s_%s', sprintf('%X', unicode2native(str(1))), str(2:end)); |
| 45 | + str = sprintf('x0x%s_%s', sprintf('%X', unicode2native(c1)), str(2:end)); |
43 | 46 | else |
44 | | - str = sprintf('x0x%X_%s', char(str(1)) + 0, str(2:end)); |
| 47 | + str = sprintf('x0x%X_%s', c1 + 0, str(2:end)); |
45 | 48 | end |
46 | 49 | end |
47 | | -if (isvarname(str)) |
48 | | - return |
| 50 | + |
| 51 | +% Fast validation: check if all remaining chars are valid (alphanumeric or underscore) |
| 52 | +% This is faster than calling isvarname for simple cases |
| 53 | +len = length(str); |
| 54 | +if len <= 63 |
| 55 | + isvalid = true; |
| 56 | + for i = 1:len |
| 57 | + c = str(i); |
| 58 | + if ~((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') |
| 59 | + isvalid = false; |
| 60 | + break |
| 61 | + end |
| 62 | + end |
| 63 | + if isvalid |
| 64 | + return |
| 65 | + end |
49 | 66 | end |
| 67 | + |
| 68 | +% Slow path: has invalid characters, need full encoding |
50 | 69 | if (exist('unicode2native', 'builtin')) |
51 | 70 | str = regexprep(str, '([^0-9A-Za-z_])', '_0x${sprintf(''%X'',unicode2native($1))}_'); |
52 | 71 | else |
53 | 72 | cpos = find(~ismember(str, ['0':'9', 'A':'Z', 'a':'z', '_'])); |
54 | | - % cpos=regexp(str,'[^0-9A-Za-z_]'); |
55 | 73 | if (isempty(cpos)) |
56 | 74 | return |
57 | 75 | end |
|
0 commit comments