6161% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
6262%
6363
64- global pos inStr len esc index_esc len_esc isoct arraytoken
64+ global pos index_esc isoct arraytoken
6565
6666if (regexp(fname ,' ^\s*(?:\[.*\])|(?:\{.*\})\s*$' ,' once' ))
6767 string= fname ;
8888
8989% String delimiters and escape chars identified to improve speed:
9090esc = find(inStr ==' "' | inStr ==' \' ); % comparable to: regexp(inStr, '["\\]');
91- index_esc = 1 ; len_esc = length( esc );
91+ index_esc = 1 ;
9292
9393opt= varargin2struct(varargin{: });
9494
9797end
9898jsoncount= 1 ;
9999while pos <= len
100- switch (next_char )
100+ switch (next_char( inStr ) )
101101 case ' {'
102- data{jsoncount } = parse_object(opt );
102+ data{jsoncount } = parse_object(inStr , esc , opt );
103103 case ' ['
104- data{jsoncount } = parse_array(opt );
104+ data{jsoncount } = parse_array(inStr , esc , opt );
105105 otherwise
106- error_pos(' Outer level structure must be an object or an array' );
106+ error_pos(' Outer level structure must be an object or an array' , inStr );
107107 end
108108 jsoncount= jsoncount + 1 ;
109109end % while
118118end
119119
120120% %-------------------------------------------------------------------------
121- function object = parse_object(varargin )
122- parse_char(' {' );
121+ function object = parse_object(inStr , esc , varargin )
122+ parse_char(inStr , ' {' );
123123 object = [];
124- if next_char ~= ' }'
124+ if next_char( inStr ) ~= ' }'
125125 while 1
126- str = parseStr(varargin{: });
126+ str = parseStr(inStr , esc , varargin{: });
127127 if isempty(str )
128- error_pos(' Name of value at position %d cannot be empty' );
128+ error_pos(' Name of value at position %d cannot be empty' , inStr );
129129 end
130- parse_char(' :' );
131- val = parse_value(varargin{: });
130+ parse_char(inStr , ' :' );
131+ val = parse_value(inStr , esc , varargin{: });
132132 object.(valid_field(str ))=val ;
133- if next_char == ' }'
133+ if next_char( inStr ) == ' }'
134134 break ;
135135 end
136- parse_char(' ,' );
136+ parse_char(inStr , ' ,' );
137137 end
138138 end
139- parse_char(' }' );
139+ parse_char(inStr , ' }' );
140140 if (isstruct(object ))
141141 object= struct2jdata(object );
142142 end
143143
144144% %-------------------------------------------------------------------------
145145
146- function object = parse_array(varargin ) % JSON array is written in row-major order
147- global pos inStr isoct
148- parse_char(' [' );
146+ function object = parse_array(inStr , esc , varargin ) % JSON array is written in row-major order
147+ global pos isoct
148+ parse_char(inStr , ' [' );
149149 object = cell(0 , 1 );
150150 dim2= [];
151151 arraydepth= jsonopt(' JSONLAB_ArrayDepth_' ,1 ,varargin{: });
154154 pbar= varargin{1 }.progressbar_;
155155 end
156156
157- if next_char ~= ' ]'
157+ if next_char( inStr ) ~= ' ]'
158158 if (jsonopt(' FastArrayParser' ,1 ,varargin{: })>=1 && arraydepth >= jsonopt(' FastArrayParser' ,1 ,varargin{: }))
159159 [endpos , e1l , e1r ]=matching_bracket(inStr ,pos );
160160 arraystr= [' [' inStr(pos : endpos )];
180180 if (nextidx >= length(astr )-1 )
181181 object= obj ;
182182 pos= endpos ;
183- parse_char(' ]' );
183+ parse_char(inStr , ' ]' );
184184 return ;
185185 end
186186 end
196196 if (nextidx >= length(astr )-1 )
197197 object= reshape(obj ,dim2 ,numel(obj )/dim2 )' ;
198198 pos= endpos ;
199- parse_char(' ]' );
199+ parse_char(inStr , ' ]' );
200200 if (pbar > 0)
201201 waitbar(pos / length(inStr ),pbar ,' loading ...' );
202202 end
220220 catch
221221 while 1
222222 newopt= varargin2struct(varargin{: },' JSONLAB_ArrayDepth_' ,arraydepth + 1 );
223- val = parse_value(newopt );
223+ val = parse_value(inStr , esc , newopt );
224224 object{end + 1 } = val ;
225- if next_char == ' ]'
225+ if next_char( inStr ) == ' ]'
226226 break ;
227227 end
228- parse_char(' ,' );
228+ parse_char(inStr , ' ,' );
229229 end
230230 end
231231 end
241241 catch
242242 end
243243 end
244- parse_char(' ]' );
244+ parse_char(inStr , ' ]' );
245245
246246 if (pbar > 0)
247247 waitbar(pos / length(inStr ),pbar ,' loading ...' );
248248 end
249249% %-------------------------------------------------------------------------
250250
251- function parse_char(c )
252- global pos inStr len
253- pos= skip_whitespace(pos ,inStr , len );
254- if pos > len || inStr(pos ) ~= c
255- error_pos(sprintf(' Expected %c at position %% d' , c ));
251+ function parse_char(inStr , c )
252+ global pos
253+ pos= skip_whitespace(pos , inStr );
254+ if pos > length( inStr ) || inStr(pos ) ~= c
255+ error_pos(sprintf(' Expected %c at position %% d' , c ), inStr );
256256 else
257257 pos = pos + 1 ;
258- pos= skip_whitespace(pos ,inStr , len );
258+ pos= skip_whitespace(pos , inStr );
259259 end
260260
261261% %-------------------------------------------------------------------------
262262
263- function c = next_char
264- global pos inStr len
265- pos= skip_whitespace(pos ,inStr , len );
266- if pos > len
263+ function c = next_char( inStr )
264+ global pos
265+ pos= skip_whitespace(pos , inStr );
266+ if pos > length( inStr )
267267 c = [];
268268 else
269269 c = inStr(pos );
270270 end
271271
272272% %-------------------------------------------------------------------------
273273
274- function newpos = skip_whitespace(pos ,inStr , len )
274+ function newpos = skip_whitespace(pos , inStr )
275275 newpos= pos ;
276- while newpos <= len && isspace(inStr(newpos ))
276+ while newpos <= length( inStr ) && isspace(inStr(newpos ))
277277 newpos = newpos + 1 ;
278278 end
279279
280280% %-------------------------------------------------------------------------
281- function str = parseStr(varargin )
282- global pos inStr len esc index_esc len_esc
281+ function str = parseStr(inStr , esc , varargin )
282+ global pos index_esc
283283 % len, ns = length(inStr), keyboard
284284 if inStr(pos ) ~= ' "'
285- error_pos(' String starting with " expected at position %d ' );
285+ error_pos(' String starting with " expected at position %d ' , inStr );
286286 else
287287 pos = pos + 1 ;
288288 end
289289 str = ' ' ;
290- while pos <= len
291- while index_esc <= len_esc && esc(index_esc ) < pos
290+ while pos <= length( inStr )
291+ while index_esc <= length( esc ) && esc(index_esc ) < pos
292292 index_esc = index_esc + 1 ;
293293 end
294- if index_esc > len_esc
295- str = [str inStr(pos : len )];
296- pos = len + 1 ;
294+ if index_esc > length( esc )
295+ str = [str inStr(pos : end )];
296+ pos = length( inStr ) + 1 ;
297297 break ;
298298 else
299299 str = [str inStr(pos : esc(index_esc )-1 )];
@@ -314,8 +314,8 @@ function parse_char(c)
314314 end
315315 return ;
316316 case ' \'
317- if pos + 1 > len
318- error_pos(' End of file reached right after escape character' );
317+ if pos + 1 > length( inStr )
318+ error_pos(' End of file reached right after escape character' , inStr );
319319 end
320320 pos = pos + 1 ;
321321 switch inStr(pos )
@@ -326,8 +326,8 @@ function parse_char(c)
326326 str(nstr + 1 ) = sprintf([' \' inStr(pos )]);
327327 pos = pos + 1 ;
328328 case ' u'
329- if pos + 4 > len
330- error_pos(' End of file reached in escaped unicode character' );
329+ if pos + 4 > length( inStr )
330+ error_pos(' End of file reached in escaped unicode character' , inStr );
331331 end
332332 str(nstr +(1 : 6 )) = inStr(pos - 1 : pos + 4 );
333333 pos = pos + 5 ;
@@ -338,12 +338,12 @@ function parse_char(c)
338338 pos = pos + 1 ;
339339 end
340340 end
341- error_pos(' End of file while expecting end of inStr' );
341+ error_pos(' End of file while expecting end of inStr' , inStr );
342342
343343% %-------------------------------------------------------------------------
344344
345- function num = parse_number(varargin )
346- global pos inStr isoct
345+ function num = parse_number(inStr , varargin )
346+ global pos isoct
347347 currstr= inStr(pos : min(pos + 30 ,end ));
348348 if (isoct ~= 0 )
349349 numstr= regexp(currstr ,' ^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?' ,' end' );
@@ -352,32 +352,32 @@ function parse_char(c)
352352 else
353353 [num , one , err , delta ] = sscanf(currstr , ' %f ' , 1 );
354354 if ~isempty(err )
355- error_pos(' Error reading number at position %d ' );
355+ error_pos(' Error reading number at position %d ' , inStr );
356356 end
357357 end
358358 pos = pos + delta - 1 ;
359359
360360% %-------------------------------------------------------------------------
361361
362- function val = parse_value(varargin )
363- global pos inStr len
364-
362+ function val = parse_value(inStr , esc , varargin )
363+ global pos
364+ len = length( inStr );
365365 if (isfield(varargin{1 },' progressbar_' ))
366366 waitbar(pos / len ,varargin{1 }.progressbar_,' loading ...' );
367367 end
368368
369369 switch (inStr(pos ))
370370 case ' "'
371- val = parseStr(varargin{: });
371+ val = parseStr(inStr , esc , varargin{: });
372372 return ;
373373 case ' ['
374- val = parse_array(varargin{: });
374+ val = parse_array(inStr , esc , varargin{: });
375375 return ;
376376 case ' {'
377- val = parse_object(varargin{: });
377+ val = parse_object(inStr , esc , varargin{: });
378378 return ;
379379 case {' -' ,' 0' ,' 1' ,' 2' ,' 3' ,' 4' ,' 5' ,' 6' ,' 7' ,' 8' ,' 9' }
380- val = parse_number(varargin{: });
380+ val = parse_number(inStr , varargin{: });
381381 return ;
382382 case ' t'
383383 if pos + 3 <= len && strcmpi(inStr(pos : pos + 3 ), ' true' )
@@ -398,11 +398,11 @@ function parse_char(c)
398398 return ;
399399 end
400400 end
401- error_pos(' Value expected at position %d ' );
401+ error_pos(' Value expected at position %d ' , inStr );
402402% %-------------------------------------------------------------------------
403403
404- function error_pos(msg )
405- global pos inStr len
404+ function error_pos(msg , inStr )
405+ global pos len
406406 poShow = max(min([pos - 15 pos - 1 pos pos + 20 ],len ),1 );
407407 if poShow(3 ) == poShow(2 )
408408 poShow(3 : 4 ) = poShow(2 )+[0 - 1 ]; % display nothing after
@@ -421,7 +421,7 @@ function error_pos(msg)
421421% "x0x[Hex code]_" will be added if the first character is not a letter.
422422 pos= regexp(str ,' ^[^A-Za-z]' ,' once' );
423423 if (~isempty(pos ))
424- if (~isoct )
424+ if (~isoct && str( 1 )+ 0 > 255 )
425425 str= regexprep(str ,' ^([^A-Za-z])' ,' x0x${sprintf(''%X'' ,unicode2native($1))}_' ,' once' );
426426 else
427427 str= sprintf(' x0x%X _%s ' ,char(str(1 )),str(2 : end ));
0 commit comments