Index: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt |
— | — | @@ -20,20 +20,51 @@ |
21 | 21 | /* Static utilities */ |
22 | 22 | |
23 | 23 | // Flatten a list of lists. |
| 24 | + //var simple_flatten = function ( e ) { |
| 25 | + // // Fast single-level flatten: |
| 26 | + // //return [].concat.apply([], e); |
| 27 | + // |
| 28 | + // var es = []; |
| 29 | + // // flatten sub-arrays |
| 30 | + // for(var i = 0, length = e.length; i < length; i++) { |
| 31 | + // var ei = e[i]; |
| 32 | + // if ($.isArray(ei)) |
| 33 | + // es = es.concat(flatten(ei)); |
| 34 | + // else |
| 35 | + // es.push(ei); |
| 36 | + // }; |
| 37 | + // return es; |
| 38 | + //}; |
| 39 | + |
24 | 40 | var flatten = function ( e ) { |
25 | 41 | // Fast single-level flatten: |
26 | 42 | //return [].concat.apply([], e); |
27 | 43 | |
28 | 44 | var es = []; |
29 | 45 | // flatten sub-arrays |
30 | | - for(var i = 0, length = e.length; i < length; i++) { |
31 | | - var ei = e[i]; |
32 | | - if ($.isArray(ei)) |
33 | | - es = es.concat(flatten(ei)); |
34 | | - else |
35 | | - es.push(ei); |
| 46 | + var chunkStart = null, |
| 47 | + modified = false; |
| 48 | + for(var i = 0, l = e.length; i < l; i++) { |
| 49 | + if ( e[i].constructor === Array ) { |
| 50 | + if ( chunkStart !== null ) { |
| 51 | + es = es.concat( e.slice( chunkStart, i ), flatten( e[i] ) ); |
| 52 | + chunkStart = null; |
| 53 | + } else { |
| 54 | + es = es.concat(flatten(e[i])); |
| 55 | + } |
| 56 | + modified = true; |
| 57 | + } else if ( chunkStart === null ) { |
| 58 | + chunkStart = i; |
| 59 | + } |
36 | 60 | }; |
37 | | - return es; |
| 61 | + if ( modified ) { |
| 62 | + if ( chunkStart !== null ) { |
| 63 | + es = es.concat( e.slice( chunkStart, e.length ) ); |
| 64 | + } |
| 65 | + return es; |
| 66 | + } else { |
| 67 | + return e; |
| 68 | + } |
38 | 69 | }; |
39 | 70 | |
40 | 71 | |
— | — | @@ -48,24 +79,24 @@ |
49 | 80 | |
50 | 81 | var flatten_stringlist = function ( c ) { |
51 | 82 | var out = [], |
52 | | - text = []; |
| 83 | + text = ''; |
53 | 84 | c = flatten(c); |
54 | 85 | for (var i = 0, l = c.length; i < l; i++) { |
55 | 86 | var ci = c[i]; |
56 | 87 | if (ci.constructor === String) { |
57 | 88 | if(ci !== '') { |
58 | | - text.push(ci); |
| 89 | + text += ci; |
59 | 90 | } |
60 | 91 | } else { |
61 | | - if (text.length) { |
62 | | - out.push( text.join('') ); |
63 | | - text = []; |
| 92 | + if (text !== '') { |
| 93 | + out.push( text ); |
| 94 | + text = ''; |
64 | 95 | } |
65 | 96 | out.push(ci); |
66 | 97 | } |
67 | 98 | } |
68 | | - if (text.length) { |
69 | | - out.push( text.join('') ); |
| 99 | + if (text !== '' ) { |
| 100 | + out.push( text ); |
70 | 101 | } |
71 | 102 | return out; |
72 | 103 | } |