Skip to content

Commit

Permalink
[test] precompute isspace and pass on via opt
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Mar 24, 2024
1 parent ee830cd commit 5d8be93
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
opt.usemap = jsonopt('UseMap', 0, opt);
opt.arraydepth_ = 1;
opt.mmaponly = jsonopt('MmapOnly', 0, opt);
opt.isspace = isspace(inputstr);

if (jsonopt('ShowProgress', 0, opt) == 1)
opt.progressbar_ = waitbar(0, 'loading ...');
Expand All @@ -226,7 +227,7 @@
end
jsoncount = 1;
while pos <= inputlen
[cc, pos, w1] = next_char(inputstr, pos);
[cc, pos, w1] = next_char(inputstr, pos, opt);
switch (cc)
case '{'
if (nargout > 1 || opt.mmaponly)
Expand Down Expand Up @@ -303,15 +304,15 @@
mmap = {};
origpath = varargin{1}.jsonpath_;
end
pos = parse_char(inputstr, pos, '[');
pos = parse_char(inputstr, pos, '[', varargin{:});
object = cell(0, 1);
arraydepth = varargin{1}.arraydepth_;
pbar = -1;
if (isfield(varargin{1}, 'progressbar_'))
pbar = varargin{1}.progressbar_;
end
format = varargin{1}.formatversion;
[cc, pos] = next_char(inputstr, pos);
[cc, pos] = next_char(inputstr, pos, varargin{:});
endpos = [];

if cc ~= ']'
Expand All @@ -330,7 +331,7 @@
if (nextidx >= length(astr) - 1)
object = obj;
pos = endpos;
pos = parse_char(inputstr, pos, ']');
pos = parse_char(inputstr, pos, ']', varargin{:});
return
end
end
Expand All @@ -347,7 +348,7 @@
object = permute(object, ndims(object):-1:1);
end
pos = endpos;
pos = parse_char(inputstr, pos, ']');
pos = parse_char(inputstr, pos, ']', varargin{:});
if (pbar > 0)
waitbar(pos / length(inputstr), pbar, 'loading ...');
end
Expand Down Expand Up @@ -386,11 +387,11 @@
[val, pos, index_esc] = parse_value(inputstr, pos, esc, index_esc, varargin{:});
end
object{end + 1} = val;
[cc, pos] = next_char(inputstr, pos);
[cc, pos] = next_char(inputstr, pos, varargin{:});
if cc == ']'
break
end
[pos, w1, w2] = parse_char(inputstr, pos, ',');
[pos, w1, w2] = parse_char(inputstr, pos, ',', varargin{:});
end
end
end
Expand Down Expand Up @@ -419,31 +420,31 @@
object = object.';
end
end
pos = parse_char(inputstr, pos, ']');
pos = parse_char(inputstr, pos, ']', varargin{:});

if (pbar > 0)
waitbar(pos / length(inputstr), pbar, 'loading ...');
end
%% -------------------------------------------------------------------------

function [pos, w1, w2] = parse_char(inputstr, pos, c)
function [pos, w1, w2] = parse_char(inputstr, pos, c, varargin)
w1 = pos;
w2 = 0;
pos = skip_whitespace(pos, inputstr);
pos = skip_whitespace(pos, inputstr, varargin{:});
w1 = pos - w1;
if pos > length(inputstr) || inputstr(pos) ~= c
pos = error_pos(sprintf('Expected %c at position %%d', c), inputstr, pos);
else
pos = pos + 1;
w2 = pos;
pos = skip_whitespace(pos, inputstr);
pos = skip_whitespace(pos, inputstr, varargin{:});
w2 = pos - w2;
end
%% -------------------------------------------------------------------------

function [c, pos, w1] = next_char(inputstr, pos)
function [c, pos, w1] = next_char(inputstr, pos, varargin)
w1 = pos;
pos = skip_whitespace(pos, inputstr);
pos = skip_whitespace(pos, inputstr, varargin{:});
w1 = pos - w1;
if pos > length(inputstr)
c = [];
Expand Down Expand Up @@ -581,14 +582,14 @@
mmap = {};
origpath = varargin{1}.jsonpath_;
end
pos = parse_char(inputstr, pos, '{');
pos = parse_char(inputstr, pos, '{', varargin{:});
usemap = varargin{1}.usemap;
if (usemap)
object = containers.Map();
else
object = [];
end
[cc, pos] = next_char(inputstr, pos);
[cc, pos] = next_char(inputstr, pos, varargin{:});
if cc ~= '}'
while 1
[str, pos, index_esc] = parseStr(inputstr, pos, esc, index_esc, varargin{:});
Expand All @@ -601,7 +602,7 @@
if isempty(str) && ~usemap
str = 'x0x0_'; % empty name is valid in JSON, decodevarname('x0x0_') restores '\0'
end
[pos, w1, w2] = parse_char(inputstr, pos, ':');
[pos, w1, w2] = parse_char(inputstr, pos, ':', varargin{:});
if (nargout > 3)
varargin{1}.jsonpath_ = [origpath, '.', str];
mmap{end + 1} = {varargin{1}.jsonpath_, [pos, 0, w2]};
Expand All @@ -623,14 +624,14 @@
end
object.(str) = val;
end
[cc, pos] = next_char(inputstr, pos);
[cc, pos] = next_char(inputstr, pos, varargin{:});
if cc == '}'
break
end
pos = parse_char(inputstr, pos, ',');
pos = parse_char(inputstr, pos, ',', varargin{:});
end
end
pos = parse_char(inputstr, pos, '}');
pos = parse_char(inputstr, pos, '}', varargin{:});

%% -------------------------------------------------------------------------

Expand All @@ -645,9 +646,9 @@

%% -------------------------------------------------------------------------

function newpos = skip_whitespace(pos, inputstr)
function newpos = skip_whitespace(pos, inputstr, varargin)
newpos = pos;
while newpos <= length(inputstr) && isspace(inputstr(newpos))
while newpos <= length(inputstr) && varargin{1}.isspace(newpos)
newpos = newpos + 1;
end

Expand Down

0 comments on commit 5d8be93

Please sign in to comment.