Skip to content

Commit 2fa6975

Browse files
committed
[compat] make optimized parsers work on matlab r2010/octave 5.2
1 parent a0dc2aa commit 2fa6975

File tree

8 files changed

+428
-350
lines changed

8 files changed

+428
-350
lines changed

encodevarname.m

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,25 @@
3737
% License: GPLv3 or 3-clause BSD license, see https://github.com/NeuroJSON/easyh5 for details
3838
%
3939

40-
% Fast path: check first character directly instead of calling isvarname
40+
% Check first character - use isvarname for the first char check to maintain
41+
% Octave compatibility (Octave allows underscore as first character)
4142
c1 = str(1);
42-
if ~((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z'))
43-
% First char is not a letter - need to encode it
44-
if (exist('unicode2native', 'builtin'))
43+
firstcharvalid = (c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z');
44+
45+
% In Octave, underscore is valid as first character; check this case
46+
if ~firstcharvalid && c1 == '_'
47+
persistent isaliasaliased
48+
if isempty(isaliasaliased)
49+
isaliasaliased = exist('OCTAVE_VERSION', 'builtin') ~= 0;
50+
end
51+
if isaliasaliased
52+
firstcharvalid = true;
53+
end
54+
end
55+
56+
if ~firstcharvalid
57+
% First char is not valid - need to encode it
58+
if exist('unicode2native', 'builtin')
4559
str = sprintf('x0x%s_%s', sprintf('%X', unicode2native(c1)), str(2:end));
4660
else
4761
str = sprintf('x0x%X_%s', c1 + 0, str(2:end));
@@ -66,11 +80,11 @@
6680
end
6781

6882
% Slow path: has invalid characters, need full encoding
69-
if (exist('unicode2native', 'builtin'))
83+
if exist('unicode2native', 'builtin')
7084
str = regexprep(str, '([^0-9A-Za-z_])', '_0x${sprintf(''%X'',unicode2native($1))}_');
7185
else
7286
cpos = find(~ismember(str, ['0':'9', 'A':'Z', 'a':'z', '_']));
73-
if (isempty(cpos))
87+
if isempty(cpos)
7488
return
7589
end
7690
str0 = str;
@@ -79,7 +93,7 @@
7993
for i = 1:length(cpos)
8094
str = [str str0(pos0(i) + 1:cpos(i) - 1) sprintf('_0x%X_', str0(cpos(i)) + 0)];
8195
end
82-
if (cpos(end) ~= length(str))
96+
if cpos(end) ~= length(str)
8397
str = [str str0(pos0(end - 1) + 1:pos0(end))];
8498
end
8599
end

jdataencode.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
opt.N_ArrayType = [opt.prefix '_ArrayType_'];
100100
opt.N_ArraySize = [opt.prefix '_ArraySize_'];
101101
opt.N_ArrayData = [opt.prefix '_ArrayData_'];
102+
opt.N_ArrayLabel = [opt.prefix '_ArrayLabel_'];
102103
opt.N_ArrayZipSize = [opt.prefix '_ArrayZipSize_'];
103104
opt.N_ArrayZipType = [opt.prefix '_ArrayZipType_'];
104105
opt.N_ArrayZipData = [opt.prefix '_ArrayZipData_'];
@@ -159,7 +160,7 @@
159160
attrname = keys(attr);
160161
for j = 1:length(attrname)
161162
if (strcmp(attrname{j}, 'dims'))
162-
newitem.(attrpath{i}).(encodevarname('_ArrayLabel_')) = attr(attrname{j});
163+
newitem.(attrpath{i}).(opt.N_ArrayLabel) = attr(attrname{j});
163164
else
164165
newitem.(attrpath{i}).(attrname{j}) = attr(attrname{j});
165166
end

loadjson.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@
215215
if ~isempty(nonws_idx)
216216
marker = repmat(inputlen + 1, 1, inputlen);
217217
marker(nonws_idx) = nonws_idx;
218-
opt.next_nonws_ = fliplr(cummin(fliplr(marker)));
218+
if (exist('cummin', 'builtin'))
219+
cummin_ = @cummin;
220+
else
221+
cummin_ = @(x) arrayfun(@(i) min(x(1:i)), 1:length(x));
222+
end
223+
opt.next_nonws_ = fliplr(cummin_(fliplr(marker)));
219224
else
220225
opt.next_nonws_ = repmat(inputlen + 1, 1, inputlen);
221226
end

0 commit comments

Comments
 (0)