source: MML/trunk/applications/m2html/private/splitcode.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 2.4 KB
Line 
1function splitc = splitcode(code)
2%SPLITCODE Split a line of Matlab code in string, comment and other
3%  SPLITC = SPLITCODE(CODE) splits line of Matlab code CODE into a cell
4%  array SPLITC where each element is either a character array ('...'),
5%  a comment (%...), a continuation (...) or something else.
6%  Note that CODE = [SPLITC{:}]
7%
8%  See also M2HTML, HIGHLIGHT
9
10%  Copyright (C) 2003 Guillaume Flandin <Guillaume@artefact.tk>
11%  $Revision: 1.0 $Date: 2003/29/04 17:33:43 $
12
13%- Label quotes in {'transpose', 'beginstring', 'midstring', 'endstring'}
14iquote = findstr(code,'''');
15quotetransp = [double('_''.)}]') ...
16                           double('A'):double('Z') ...
17                           double('0'):double('9') ...
18                           double('a'):double('z')];
19flagstring = 0;
20flagdoublequote = 0;
21jquote = [];
22for i=1:length(iquote)
23        if ~flagstring
24                if iquote(i) > 1 & any(quotetransp == double(code(iquote(i)-1)))
25                        % => 'transpose';
26                else
27                        % => 'beginstring';
28                        jquote(size(jquote,1)+1,:) = [iquote(i) length(code)];
29                        flagstring = 1;
30                end
31        else % if flagstring
32                if flagdoublequote | ...
33                   (iquote(i) < length(code) & strcmp(code(iquote(i)+1),''''))
34                        % => 'midstring';
35                        flagdoublequote = ~flagdoublequote;
36                else
37                        % => 'endstring';
38                        jquote(size(jquote,1),2) = iquote(i);
39                        flagstring = 0;
40                end
41        end
42end
43
44%- Find if a portion of code is a comment
45ipercent = findstr(code,'%');
46jpercent = [];
47for i=1:length(ipercent)
48        if isempty(jquote) | ...
49           ~any((ipercent(i) > jquote(:,1)) & (ipercent(i) < jquote(:,2)))
50                jpercent = [ipercent(i) length(code)];
51                break;
52        end
53end
54
55%- Find continuation punctuation '...'
56icont = findstr(code,'...');
57for i=1:length(icont)
58        if (isempty(jquote) | ...
59                ~any((icont(i) > jquote(:,1)) & (icont(i) < jquote(:,2)))) & ...
60                (isempty(jpercent) | ...
61                icont(i) < jpercent(1))
62                jpercent = [icont(i) length(code)];
63                break;
64        end
65end
66
67%- Remove strings inside comments
68if ~isempty(jpercent) & ~isempty(jquote)
69        jquote(find(jquote(:,1) > jpercent(1)),:) = [];
70end
71
72%- Split code in a cell array of strings
73icode = [jquote ; jpercent];
74splitc = {};
75if isempty(icode)
76        splitc{1} = code;
77elseif icode(1,1) > 1
78        splitc{1} = code(1:icode(1,1)-1);
79end
80for i=1:size(icode,1)
81        splitc{end+1} = code(icode(i,1):icode(i,2));
82        if i < size(icode,1) & icode(i+1,1) > icode(i,2) + 1
83                splitc{end+1} = code((icode(i,2)+1):(icode(i+1,1)-1));
84        elseif i == size(icode,1) & icode(i,2) < length(code)
85                splitc{end+1} = code(icode(i,2)+1:end);
86        end
87end
Note: See TracBrowser for help on using the repository browser.