Skip to content

Commit

Permalink
fast bracket matching
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed May 31, 2019
1 parent c109b54 commit d47be45
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
15 changes: 15 additions & 0 deletions fast_match_bracket.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function [endpos, maxlevel] = fast_match_bracket(key,pos,startpos,brackets)
if(nargin<4)
brackets='[]';
end
startpos=find( pos >= startpos, 1 );
count = key(startpos:end);
if(length(count)==1 && count==']')
endpos=pos(end);
maxlevel=1;
return;
end
flag=cumsum(count==brackets(1))-cumsum(count==brackets(2))+1;
endpos = find(flag==0,1);
maxlevel=max([1,max(flag(1:endpos))]);
endpos = pos(endpos + startpos-1);
43 changes: 21 additions & 22 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,16 @@
end

pos = 1; len = length(string); inputstr = string;
arraytoken=find(inputstr=='[' | inputstr==']' | inputstr=='"');
jstr=regexprep(inputstr,'\\\\',' ');
escquote=regexp(jstr,'\\"');
arraytoken=sort([arraytoken escquote]);
arraytokenidx=find(inputstr=='[' | inputstr==']');
arraytoken=inputstr(arraytokenidx);

% String delimiters and escape chars identified to improve speed:
esc = find(inputstr=='"' | inputstr=='\' ); % comparable to: regexp(inputstr, '["\\]');
index_esc = 1;

opt=varargin2struct(varargin{:});
opt.arraytoken_=arraytoken;
opt.arraytokenidx_=arraytokenidx;

if(jsonopt('ShowProgress',0,opt)==1)
opt.progressbar_=waitbar(0,'loading ...');
Expand Down Expand Up @@ -159,7 +158,7 @@
if next_char(inputstr) ~= ']'
try
if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))
[endpos, maxlevel]=match_bracket(inputstr,pos);
[endpos, maxlevel]=fast_match_bracket(varargin{1}.arraytoken_,varargin{1}.arraytokenidx_,pos);
if(~isempty(endpos))
arraystr=['[' inputstr(pos:endpos)];
arraystr=sscanf_prep(arraystr);
Expand Down Expand Up @@ -218,29 +217,30 @@
end
end
end
if(regexp(arraystr,':','once'))
error('One can not use ":" construct inside a JSON array');
end
arraystr=regexprep(arraystr,'\[','{');
arraystr=regexprep(arraystr,'\]','}');
if(jsonopt('ParseStringArray',0,varargin{:})==0)
arraystr=regexprep(arraystr,'\"','''');
end
object=eval(arraystr);
if(iscell(object))
object=cellfun(@unescapejsonstring,object,'UniformOutput',false);
if(isempty(regexp(arraystr,':','once')))
arraystr=regexprep(arraystr,'\[','{');
arraystr=regexprep(arraystr,'\]','}');
if(jsonopt('ParseStringArray',0,varargin{:})==0)
arraystr=regexprep(arraystr,'\"','''');
end
object=eval(arraystr);
if(iscell(object))
object=cellfun(@unescapejsonstring,object,'UniformOutput',false);
end
pos=endpos;
end
pos=endpos;
catch
while 1
end
if(pos~=endpos)
while 1
newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);
val = parse_value(inputstr, esc, newopt);
object{end+1} = val;
if next_char(inputstr) == ']'
break;
end
parse_char(inputstr, ',');
end
end
end
end

Expand Down Expand Up @@ -436,15 +436,14 @@ function error_pos(msg, inputstr)
% Invalid characters will be converted to underscores, and the prefix
% "x0x[Hex code]_" will be added if the first character is not a letter.
isoct=exist('OCTAVE_VERSION','builtin');
pos=regexp(str,'^[^A-Za-z]','once');
if(~isempty(pos))
if(~isvarname(str))
if(~isoct && str(1)+0 > 255)
str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');
else
str=sprintf('x0x%X_%s',char(str(1)),str(2:end));
end
end
if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' )))
if(isvarname(str))
return;
end
if(~isoct)
Expand Down

0 comments on commit d47be45

Please sign in to comment.