comparison m-toolbox/html_help/highlight/highlight.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function highlight(mfile,options,outfile)
2 %HIGHLIGHT - Syntax Highlighting of Matlab M-files in HTML, LaTeX, RTF and XML.
3 % HIGHLIGHT(MFILE) takes an M-file MFILE in input and writes on disk an HTML
4 % file with the same basename ('foo.m' => 'foo.html') adding colored syntax
5 % highlighting on comment, string and Matlab keyword elements.
6 % HIGHLIGHT(MFILE,OPTIONS) with OPTIONS being string 'html', 'xhtml', 'tex',
7 % 'rtf' or 'xml', allows to choose the output format between HTML, XHTML, LaTeX
8 % RTF or XML. The same rule is used to determine the name of the output file.
9 % HIGHLIGHT(MFILE,OPTIONS) allows to specify some options in a structure:
10 % options.type - Output file type
11 % [ {'html'} | 'tex' | 'rtf' | 'xml' | 'xhtml']
12 % options.tabs - Replace '\t' in source code by n white space
13 % [ 0 ... {4} ... n]
14 % options.linenb - Display line number in front of each line [ 0 | {1} ]
15 % HIGHLIGHT(MFILE,OPTIONS,OUTFILE) allows to specify the name of the output
16 % file. OUTFILE can also be a file handle. In that case, no header will be
17 % written, only the highlighted Matlab code will be sent to the OUTFILE stream.
18
19 % Output file can be customized (font style, font color, font size, ...):
20 % o HTML: use CSS (Cascading Style Sheets) to define 'comment', 'string',
21 % 'keyword', 'cont' and 'code' SPAN elements and 'mcode' PRE tag.
22 % o LaTeX: packages 'alltt' and 'color' are required, you can modify colors
23 % in defining colors 'string', 'comment' and 'keyword' using command
24 % \definecolor{mycolor}{rgb}{a,b,c}
25 % o RTF: Colors are defined in the Color Table at the beginning of the
26 % document. See Rich Text Format Specifications for more details:
27 % <http://msdn.microsoft.com/library/en-us/dnrtfspec/html/rtfspec.asp>
28 % o XML: you will find the DTD of the resulting XML file in matlab.dtd
29 % You can then use XSL to transform your XML file in what you want.
30 % For example, mat2html.xsl transforms your XML file in HTML as it would
31 % be if you would have used highlight.m to to so.
32 % On Matlab 6.5, the command is:
33 % xslt highlight.xml mat2html.xsl highlight.html
34 % On Linux, using libxslt <http://xmlsoft.org/XSLT/>, the command is:
35 % xsltproc -o highlight.html mat2html.xsl highlight.xml
36
37 % Copyright (C) 2003 Guillaume Flandin <Guillaume@artefact.tk>
38 % $Revision: 1.1 $Date: 2010/02/24 10:14:32 $
39
40 % This program is free software; you can redistribute it and/or
41 % modify it under the terms of the GNU General Public License
42 % as published by the Free Software Foundation; either version 2
43 % of the License, or any later version.
44 %
45 % This program is distributed in the hope that it will be useful,
46 % but WITHOUT ANY WARRANTY; without even the implied warranty of
47 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 % GNU General Public License for more details.
49 %
50 % You should have received a copy of the GNU General Public License
51 % along with this program; if not, write to the Free Software
52 % Foundation Inc, 59 Temple Pl. - Suite 330, Boston, MA 02111-1307, USA.
53
54 % Suggestions for improvement and fixes are always welcome, although no
55 % guarantee is made whether and when they will be implemented.
56 % Send requests to Guillaume@artefact.tk
57
58 % TODO % Improve distinction between 'end' keyword and array subscript
59 % TODO % Smart indentation is *very* buggy (unusable => undocumented)
60 % TODO % Handle wrap mode for long lines in LaTeX mode (and in HTML)
61 % TODO % Improve the XSL transformer from XML to LaTeX
62
63 %- Set up options
64 error(nargchk(1,3,nargin));
65
66 opt = struct('type', 'html', ...
67 'tabs', 4, ...
68 'linenb', 1, ...
69 'indent', 0);
70 if nargin >= 2
71 if isstruct(options)
72 names = fieldnames(options);
73 for i=1:length(names)
74 opt = setfield(opt,names{i},getfield(options,names{i}));
75 end
76 elseif ischar(options)
77 opt.type = options;
78 else
79 error('Bad input argument.');
80 end
81 end
82 if strcmp(lower(opt.type),'latex'), opt.type = 'tex'; end
83 if strcmp(lower(opt.type),'xml'), opt.linenb = 1; end
84
85 %- If no output filename is provided, one is chosen according to mfile
86 if nargin < 3
87 [pathstr, name, ext] = fileparts(mfile);
88 outfile = fullfile(pathstr, [name, '.' lower(opt.type)]);
89 end
90
91 %- If an output filename is provided, a standard header is created
92 if ischar(outfile)
93 outfid = fopen(outfile,'wt');
94 if outfid == -1
95 error(sprintf('Cannot open ',outfile));
96 end
97 feval([lower(opt.type) '_file_start'],outfid,mfile);
98 %- Otherwise a file handle is provided
99 else
100 outfid = outfile;
101 end
102
103 %- Open the Matlab mfile to be highlighted
104 mfid = fopen(mfile,'rt');
105 if mfid == -1
106 error(sprintf('Cannot open ',mfile));
107 end
108
109 %- Write the syntax highlighted mfile code in the output file
110 write_highlighted_code(mfid,outfid,opt)
111
112 %- Close the Matlab mfile and potentially the output file
113 fclose(mfid);
114 if ischar(outfile),
115 feval([lower(opt.type) '_file_end'],outfid);
116 fclose(outfid);
117 end
118
119 %===============================================================================
120 function write_highlighted_code(mfid,outfid,opt)
121 matlabKeywords = getMatlabKeywords;
122 mKeySort = getMatlabKeywordsSorted;
123 out_format = feval([lower(opt.type) '_format']);
124 strtok_delim = sprintf(' \t\n\r(){}[]<>+-*~!|\\@&/.,:;="''%');
125
126 fprintf(outfid,out_format.pre_start);
127 nbline = 1;
128 nbblanks = 0;
129 flagnextline = 0;
130 while 1
131 tline = fgetl(mfid);
132 if ~ischar(tline), break, end
133 tline = feval([lower(opt.type) '_special_char'],horztab(tline,opt.tabs));
134 %- Display the line number at each line
135 if opt.linenb
136 fprintf(outfid,out_format.nb_line,nbline);
137 nbline = nbline + 1;
138 end
139 %- Remove blanks at the beginning of the line
140 if opt.indent
141 tline = fliplr(deblank(fliplr(tline)));
142 end
143 nbblanks = nbblanks + flagnextline;
144 flagnextline = 0;
145 %- Split code into meaningful chunks
146 splitc = splitcode(tline);
147 newline = '';
148 for i=1:length(splitc)
149 if isempty(splitc{i})
150 elseif splitc{i}(1) == ''''
151 newline = [newline sprintf(out_format.string,splitc{i})];
152 elseif splitc{i}(1) == '%'
153 newline = [newline sprintf(out_format.comment,splitc{i})];
154 elseif ~isempty(strmatch('...',splitc{i}))
155 newline = [newline sprintf(out_format.cont,'...')];
156 if ~isempty(splitc{i}(4:end))
157 newline = [newline sprintf(out_format.comment,splitc{i}(4:end))];
158 end
159 else
160 %- Look for Matlab keywords
161 r = splitc{i};
162 stringcode = '';
163 while 1
164 [t,r,q] = strtok(r,strtok_delim);
165 stringcode = [stringcode q];
166 if isempty(t), break, end;
167 isNextIncr = any(strcmp(t,mKeySort.nextincr));
168 isNextIncr2 = any(strcmp(t,mKeySort.nextincr2));
169 isCurrDecr = any(strcmp(t,mKeySort.currdecr));
170 isNextDecr = any(strcmp(t,mKeySort.nextdecr));
171 isOther = any(strcmp(t,mKeySort.other));
172 if isNextDecr % if strcmp(t,'end')
173 % 'end' is keyword or array subscript ?
174 rr = fliplr(deblank(fliplr(r)));
175 icomma = strmatch(',',rr);
176 isemicolon = strmatch(';',rr);
177 if ~(isempty(rr) | ~isempty([icomma isemicolon]))
178 isNextDecr = 0;
179 else
180 nbblanks = nbblanks - 1;
181 flagnextline = flagnextline + 1;
182 end
183 % TODO % false detection of a([end,1])
184 end
185 if isNextIncr, flagnextline = flagnextline + 1; end
186 if isNextIncr2, flagnextline = flagnextline + 2; end
187 if isNextDecr, flagnextline = flagnextline - 1; end
188 if isCurrDecr, nbblanks = nbblanks - 1; end
189 % if any(strcmp(t,matlabKeywords))
190 if isNextIncr | isNextIncr2 |isCurrDecr | isNextDecr | isOther
191 if ~isempty(stringcode)
192 newline = [newline sprintf(out_format.code,stringcode)];
193 stringcode = '';
194 end
195 newline = [newline sprintf(out_format.keyword,t)];
196 else
197 stringcode = [stringcode t];
198 end
199 end
200 if ~isempty(stringcode)
201 newline = [newline sprintf(out_format.code,stringcode)];
202 end
203 end
204 end
205 if ~opt.indent, nbblanks = 0; end
206 %- Print the syntax highlighted line
207 fprintf(outfid,out_format.line,[blanks(nbblanks*opt.tabs) newline]);
208 end
209 fprintf(outfid,out_format.pre_end);
210
211 %===============================================================================
212 % HTML FORMAT %
213 %===============================================================================
214 function html_file_start(fid,mfile)
215 fprintf(fid,[ ...
216 '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n' ...
217 '\t"http://www.w3.org/TR/REC-html40/loose.dtd">\n' ...
218 '<html>\n<head>\n<title>%s</title>\n' ...
219 '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n' ...
220 '<meta name="generator" content="highlight.m &copy; 2003 Guillaume Flandin">\n' ...
221 '<style type="text/css">\n' ...
222 ' .comment {color: #228B22;}\n' ...
223 ' .string {color: #B20000;}\n' ...
224 ' .keyword, .cont {color: #0000FF;}\n' ...
225 ' .cont {text-decoration: underline;}\n' ...
226 ' .code {color: #000000;}\n' ...
227 '</style>\n' ...
228 '</head>\n<body>\n'],mfile);
229
230 %-------------------------------------------------------------------------------
231 function html_file_end(fid)
232 fprintf(fid,'\n</body>\n</html>');
233
234 %-------------------------------------------------------------------------------
235 function format = html_format
236 format.string = '<span class="string">%s</span>';
237 format.comment = '<span class="comment">%s</span>';
238 format.code = '%s'; %'<span class="code">%s</span>';
239 format.keyword = '<span class="keyword">%s</span>';
240 format.cont = '<span class="cont">%s</span>';
241 format.pre_start = '<pre class="mcode">';
242 format.pre_end = '</pre>\n';
243 format.nb_line = '%04d ';
244 format.line = '%s\n';
245
246 %-------------------------------------------------------------------------------
247 function str = html_special_char(str)
248 %- See http://www.w3.org/TR/html4/charset.html#h-5.3.2
249 str = strrep(str,'&','&amp;');
250 str = strrep(str,'<','&lt;');
251 str = strrep(str,'>','&gt;');
252 str = strrep(str,'"','&quot;');
253
254 %===============================================================================
255 % XHTML FORMAT %
256 %===============================================================================
257 function xhtml_file_start(fid,mfile)
258 fprintf(fid,[ ...
259 '<?xml version="1.0"?>\n' ...
260 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' ...
261 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' ...
262 '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">\n' ...
263 '<head>\n<title>%s</title>\n' ...
264 '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n' ...
265 '<meta name="generator" content="highlight.m &copy; 2003 Guillaume Flandin" />\n' ...
266 '<style type="text/css">\n' ...
267 ' .comment {color: #228B22;}\n' ...
268 ' .string {color: #B20000;}\n' ...
269 ' .keyword, .cont {color: #0000FF;}\n' ...
270 ' .cont {text-decoration: underline;}\n' ...
271 ' .code {color: #000000;}\n' ...
272 '</style>\n' ...
273 '</head>\n<body>\n'],mfile);
274
275 %-------------------------------------------------------------------------------
276 function xhtml_file_end(fid)
277 fprintf(fid,'\n</body>\n</html>');
278
279 %-------------------------------------------------------------------------------
280 function format = xhtml_format
281 format.string = '<span class="string">%s</span>';
282 format.comment = '<span class="comment">%s</span>';
283 format.code = '%s'; %'<span class="code">%s</span>';
284 format.keyword = '<span class="keyword">%s</span>';
285 format.cont = '<span class="cont">%s</span>';
286 format.pre_start = '<pre class="mcode">';
287 format.pre_end = '</pre>\n';
288 format.nb_line = '%04d ';
289 format.line = '%s\n';
290
291 %-------------------------------------------------------------------------------
292 function str = xhtml_special_char(str)
293 %- See http://www.w3.org/TR/html4/charset.html#h-5.3.2
294 str = strrep(str,'&','&amp;');
295 str = strrep(str,'<','&lt;');
296 str = strrep(str,'>','&gt;');
297 str = strrep(str,'"','&quot;');
298
299
300 %===============================================================================
301 % XML FORMAT %
302 %===============================================================================
303 function xml_file_start(fid,mfile)
304 fprintf(fid,[ ...
305 '<?xml version="1.0" encoding="iso-8859-1" ?>\n' ...
306 '<!DOCTYPE mfile SYSTEM "matlab.dtd">\n' ...
307 '<mfile name="%s">\n'],mfile);
308
309 %-------------------------------------------------------------------------------
310 function xml_file_end(fid)
311 fprintf(fid,'</mfile>');
312
313 %-------------------------------------------------------------------------------
314 function format = xml_format
315 format.string = '<string>%s</string>';
316 format.comment = '<comment>%s</comment>';
317 format.code = '%s'; %'<code>%s</code>';
318 format.keyword = '<keyword>%s</keyword>';
319 format.cont = '<cont>%s</cont>';
320 format.pre_start = '';
321 format.pre_end = '';
322 format.nb_line = '<line nb="%04d ">';
323 format.line = '%s</line>\n';
324
325 %-------------------------------------------------------------------------------
326 function str = xml_special_char(str)
327 %- See http://www.w3.org/TR/REC-xml#sec-predefined-ent
328 str = strrep(str,'&','&amp;');
329 str = strrep(str,'<','&lt;');
330 str = strrep(str,'>','&gt;');
331 str = strrep(str,'"','&quot;');
332 %str = strrep(str,'''','&apos;');
333
334 %===============================================================================
335 % LaTeX FORMAT %
336 %===============================================================================
337 function tex_file_start(fid,mfile)
338 fprintf(fid,['\\documentclass[a4paper,10pt]{article}\n' ...
339 ' \\usepackage{alltt}\n' ...
340 ' \\usepackage{color}\n' ...
341 ' \\usepackage{fullpage}\n' ...
342 ' \\definecolor{string}{rgb}{0.7,0.0,0.0}\n' ...
343 ' \\definecolor{comment}{rgb}{0.13,0.54,0.13}\n' ...
344 ' \\definecolor{keyword}{rgb}{0.0,0.0,1.0}\n' ...
345 ' \\title{%s}\n' ...
346 ' \\author{\\textsc{Matlab}, The Mathworks, Inc.}\n' ...
347 '\\begin{document}\n' ...
348 '\\maketitle\n'],mfile);
349
350 %-------------------------------------------------------------------------------
351 function tex_file_end(fid)
352 fprintf(fid,'\\end{document}');
353
354 %-------------------------------------------------------------------------------
355 function format = tex_format
356 format.string = '\\textcolor{string}{%s}';
357 format.comment = 'textcolor{comment}{%s}'; % '%' has been replaced by '\%'
358 format.code = '%s';
359 format.keyword = '\\textcolor{keyword}{%s}';
360 format.cont = '\\textcolor{keyword}{\\underline{%s}}';
361 format.pre_start = '\\begin{alltt}\n';
362 format.pre_end = '\\end{alltt}\n';
363 format.nb_line = '%04d ';
364 format.line = '%s\n';
365
366 %-------------------------------------------------------------------------------
367 function str = tex_special_char(str)
368 % Special characters: # $ % & ~ _ ^ \ { }
369 str = strrep(str,'\','\(\backslash\)'); % $\backslash$ or \textbackslash or \verb+\+
370 str = strrep(str,'#','\#');
371 str = strrep(str,'$','\$');
372 str = strrep(str,'%','\%');
373 str = strrep(str,'&','\&');
374 str = strrep(str,'_','\_');
375 str = strrep(str,'{','\{');
376 str = strrep(str,'}','\}');
377 str = strrep(str,'^','\^{}');
378 str = strrep(str,'~','\~{}'); % or \textasciitilde
379
380 %===============================================================================
381 % RTF FORMAT %
382 %===============================================================================
383 function rtf_file_start(fid,mfile)
384 fprintf(fid,['{\\rtf1\\ansi\n\n' ...
385 '{\\fonttbl{\\f0\\fmodern\\fcharset0\\fprq1 Courier New;}}\n\n' ...
386 '{\\colortbl;\n' ...
387 '\\red0\\green0\\blue0;\n' ...
388 '\\red0\\green0\\blue255;\n' ...
389 '\\red33\\green138\\blue33;\n' ...
390 '\\red178\\green0\\blue0;}\n\n' ...
391 '{\\info{\\title %s}\n' ...
392 '{\\author HighLight.m Copyright 2003}\n' ...
393 '{\\creatim\\yr%s\\mo%s\\dy%s}' ...
394 '{\\*\\manager Guillaume Flandin}\n' ...
395 '{\\*\\company Artefact.tk}\n' ...
396 '{\\*\\hlinkbase http://www.madic.org/download/' ...
397 'matlab/highlight/}}\n\n'],mfile,...
398 datestr(date,'yyyy'),datestr(date,'mm'),datestr(date,'dd'));
399
400 %-------------------------------------------------------------------------------
401 function rtf_file_end(fid)
402 fprintf(fid,'}');
403
404 %-------------------------------------------------------------------------------
405 function format = rtf_format
406 format.string = '{\\cf4 %s}';
407 format.comment = '{\\cf3 %s}';
408 format.code = '{%s}';
409 format.keyword = '{\\cf2 %s}';
410 format.cont = '{\\cf2 %s}';
411 format.pre_start = '{\\f0\\fs16{';
412 format.pre_end = '}}';
413 format.nb_line = '{%04d }';
414 format.line = '%s\n\\par ';
415
416 %-------------------------------------------------------------------------------
417 function str = rtf_special_char(str)
418 str = strrep(str,'\','\\');
419 str = strrep(str,'{','\{');
420 str = strrep(str,'}','\}');
421
422 %===============================================================================
423 % MATLAB KEYWORDS %
424 %===============================================================================
425 function matlabKeywords = getMatlabKeywords
426 %matlabKeywords = iskeyword; % Matlab R13
427 matlabKeywords = {'break', 'case', 'catch', 'continue', 'elseif', 'else',...
428 'end', 'for', 'function', 'global', 'if', 'otherwise', ...
429 'persistent', 'return', 'switch', 'try', 'while'};
430
431 %-------------------------------------------------------------------------------
432 function mKeySort = getMatlabKeywordsSorted
433 mKeySort.nextincr = {'for', 'while', 'if', 'else', 'elseif', ...
434 'case', 'otherwise', 'try', 'catch'};
435 mKeySort.nextincr2 = {'switch'};
436 mKeySort.currdecr = {'else', 'elseif', 'case', 'otherwise', 'catch'};
437 mKeySort.nextdecr = {'end'};
438 mKeySort.other = {'break', 'continue', 'function', 'global', ...
439 'persistent', 'return'};
440
441 %===============================================================================
442 % HANDLE TAB CHARACTER %
443 %===============================================================================
444 function str = horztab(str,n)
445 %- For browsers, the horizontal tab character is the smallest non-zero
446 %- number of spaces necessary to line characters up along tab stops that are
447 %- every 8 characters: behaviour obtained when n = 0.
448 if n > 0
449 str = strrep(str,sprintf('\t'),blanks(n));
450 end
451
452 %===============================================================================
453 % MATLAB CODE PARSER %
454 %===============================================================================
455 function splitc = splitcode(code)
456 %Split a line of Matlab code in string, comment and other
457
458 %- Label quotes in {'transpose', 'beginstring', 'midstring', 'endstring'}
459 iquote = findstr(code,'''');
460 quotetransp = [double('_''.)}]') ...
461 double('A'):double('Z') ...
462 double('0'):double('9') ...
463 double('a'):double('z')];
464 flagstring = 0;
465 flagdoublequote = 0;
466 jquote = [];
467 for i=1:length(iquote)
468 if ~flagstring
469 if iquote(i) > 1 & any(quotetransp == double(code(iquote(i)-1)))
470 % => 'transpose';
471 else
472 % => 'beginstring';
473 jquote(size(jquote,1)+1,:) = [iquote(i) length(code)];
474 flagstring = 1;
475 end
476 else % if flagstring
477 if flagdoublequote | ...
478 (iquote(i) < length(code) & strcmp(code(iquote(i)+1),''''))
479 % => 'midstring';
480 flagdoublequote = ~flagdoublequote;
481 else
482 % => 'endstring';
483 jquote(size(jquote,1),2) = iquote(i);
484 flagstring = 0;
485 end
486 end
487 end
488
489 %- Find if a portion of code is a comment
490 ipercent = findstr(code,'%');
491 jpercent = [];
492 for i=1:length(ipercent)
493 if isempty(jquote) | ...
494 ~any((ipercent(i) > jquote(:,1)) & (ipercent(i) < jquote(:,2)))
495 jpercent = [ipercent(i) length(code)];
496 break;
497 end
498 end
499
500 %- Find continuation punctuation '...'
501 icont = findstr(code,'...');
502 for i=1:length(icont)
503 if (isempty(jquote) | ...
504 ~any((icont(i) > jquote(:,1)) & (icont(i) < jquote(:,2)))) & ...
505 (isempty(jpercent) | ...
506 icont(i) < jpercent(1))
507 jpercent = [icont(i) length(code)];
508 break;
509 end
510 end
511
512 %- Remove strings inside comments
513 if ~isempty(jpercent) & ~isempty(jquote)
514 jquote(find(jquote(:,1) > jpercent(1)),:) = [];
515 end
516
517 %- Split code in a cell array of strings
518 icode = [jquote ; jpercent];
519 splitc = {};
520 if isempty(icode)
521 splitc{1} = code;
522 elseif icode(1,1) > 1
523 splitc{1} = code(1:icode(1,1)-1);
524 end
525 for i=1:size(icode,1)
526 splitc{end+1} = code(icode(i,1):icode(i,2));
527 if i < size(icode,1) & icode(i+1,1) > icode(i,2) + 1
528 splitc{end+1} = code((icode(i,2)+1):(icode(i+1,1)-1));
529 elseif i == size(icode,1) & icode(i,2) < length(code)
530 splitc{end+1} = code(icode(i,2)+1:end);
531 end
532 end
533
534 %===============================================================================
535 % MODIFIED VERSION OF STRTOK %
536 %===============================================================================
537 function [token, remainder, quotient] = strtok(string, delimiters)
538 % Modified version of STRTOK to also return the quotient
539 % string = [quotient token remainder]
540 %STRTOK Find token in string.
541 % STRTOK(S) returns the first token in the string S delimited
542 % by "white space". Any leading white space characters are ignored.
543 %
544 % STRTOK(S,D) returns the first token delimited by one of the
545 % characters in D. Any leading delimiter characters are ignored.
546 %
547 % [T,R] = STRTOK(...) also returns the remainder of the original
548 % string.
549 % If the token is not found in S then R is an empty string and T
550 % is same as S.
551 %
552 % Copyright 1984-2002 The MathWorks, Inc.
553 % $Revision: 1.1 $ $Date: 2010/02/24 10:14:32 $
554
555 token = []; remainder = []; quotient = string;
556
557 len = length(string);
558 if len == 0
559 return
560 end
561
562 if (nargin == 1)
563 delimiters = [9:13 32]; % White space characters
564 end
565
566 i = 1;
567 while (any(string(i) == delimiters))
568 i = i + 1;
569 if (i > len), return, end
570 end
571 start = i;
572 while (~any(string(i) == delimiters))
573 i = i + 1;
574 if (i > len), break, end
575 end
576 finish = i - 1;
577
578 token = string(start:finish);
579
580 if (nargout >= 2)
581 remainder = string(finish + 1:length(string));
582 end
583
584 if (nargout == 3 & start > 1)
585 quotient = string(1:start-1);
586 else
587 quotient = [];
588 end