1 | function 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'} |
---|
14 | iquote = findstr(code,''''); |
---|
15 | quotetransp = [double('_''.)}]') ... |
---|
16 | double('A'):double('Z') ... |
---|
17 | double('0'):double('9') ... |
---|
18 | double('a'):double('z')]; |
---|
19 | flagstring = 0; |
---|
20 | flagdoublequote = 0; |
---|
21 | jquote = []; |
---|
22 | for 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 |
---|
42 | end |
---|
43 | |
---|
44 | %- Find if a portion of code is a comment |
---|
45 | ipercent = findstr(code,'%'); |
---|
46 | jpercent = []; |
---|
47 | for 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 |
---|
53 | end |
---|
54 | |
---|
55 | %- Find continuation punctuation '...' |
---|
56 | icont = findstr(code,'...'); |
---|
57 | for 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 |
---|
65 | end |
---|
66 | |
---|
67 | %- Remove strings inside comments |
---|
68 | if ~isempty(jpercent) & ~isempty(jquote) |
---|
69 | jquote(find(jquote(:,1) > jpercent(1)),:) = []; |
---|
70 | end |
---|
71 | |
---|
72 | %- Split code in a cell array of strings |
---|
73 | icode = [jquote ; jpercent]; |
---|
74 | splitc = {}; |
---|
75 | if isempty(icode) |
---|
76 | splitc{1} = code; |
---|
77 | elseif icode(1,1) > 1 |
---|
78 | splitc{1} = code(1:icode(1,1)-1); |
---|
79 | end |
---|
80 | for 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 |
---|
87 | end |
---|