| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 class CodeBuffer implements StringBuffer { | 7 class CodeBuffer implements StringBuffer { |
| 8 StringBuffer buffer; | 8 StringBuffer buffer; |
| 9 List<CodeBufferMarker> markers; | 9 List<CodeBufferMarker> markers; |
| 10 int lastBufferOffset = 0; | 10 int lastBufferOffset = 0; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 } | 26 } |
| 27 /** | 27 /** |
| 28 * Converts [object] to a string and adds it to the buffer. If [object] is a | 28 * Converts [object] to a string and adds it to the buffer. If [object] is a |
| 29 * [CodeBuffer], adds its markers to [markers]. | 29 * [CodeBuffer], adds its markers to [markers]. |
| 30 */ | 30 */ |
| 31 CodeBuffer write(var object) { | 31 CodeBuffer write(var object) { |
| 32 if (object is CodeBuffer) { | 32 if (object is CodeBuffer) { |
| 33 return addBuffer(object); | 33 return addBuffer(object); |
| 34 } | 34 } |
| 35 if (mappedRangeCounter == 0) setSourceLocation(null); | 35 if (mappedRangeCounter == 0) setSourceLocation(null); |
| 36 buffer.add(object.toString()); | 36 buffer.write(object); |
| 37 return this; | 37 return this; |
| 38 } | 38 } |
| 39 | 39 |
| 40 CodeBuffer writeAll(Iterable<Object> objects) { | 40 CodeBuffer writeAll(Iterable<Object> objects) { |
| 41 for (var object in objects) { | 41 for (var object in objects) { |
| 42 write(object); | 42 write(object); |
| 43 } | 43 } |
| 44 return this; | 44 return this; |
| 45 } | 45 } |
| 46 | 46 |
| 47 CodeBuffer writeln(var object) { | 47 CodeBuffer writeln(var object) { |
| 48 return write(object).write("\n"); | 48 return write(object).write("\n"); |
| 49 } | 49 } |
| 50 | 50 |
| 51 CodeBuffer addBuffer(CodeBuffer other) { | 51 CodeBuffer addBuffer(CodeBuffer other) { |
| 52 if (other.markers.length > 0) { | 52 if (other.markers.length > 0) { |
| 53 CodeBufferMarker firstMarker = other.markers[0]; | 53 CodeBufferMarker firstMarker = other.markers[0]; |
| 54 int offsetDelta = | 54 int offsetDelta = |
| 55 buffer.length + firstMarker.offsetDelta - lastBufferOffset; | 55 buffer.length + firstMarker.offsetDelta - lastBufferOffset; |
| 56 markers.add(new CodeBufferMarker(offsetDelta, | 56 markers.add(new CodeBufferMarker(offsetDelta, |
| 57 firstMarker.sourcePosition)); | 57 firstMarker.sourcePosition)); |
| 58 for (int i = 1; i < other.markers.length; ++i) { | 58 for (int i = 1; i < other.markers.length; ++i) { |
| 59 markers.add(other.markers[i]); | 59 markers.add(other.markers[i]); |
| 60 } | 60 } |
| 61 lastBufferOffset = buffer.length + other.lastBufferOffset; | 61 lastBufferOffset = buffer.length + other.lastBufferOffset; |
| 62 } | 62 } |
| 63 buffer.add(other.getText()); | 63 buffer.write(other.getText()); |
| 64 return this; | 64 return this; |
| 65 } | 65 } |
| 66 | 66 |
| 67 CodeBuffer addAll(Iterable<Object> iterable) => writeAll(iterable); | 67 CodeBuffer addAll(Iterable<Object> iterable) => writeAll(iterable); |
| 68 | 68 |
| 69 CodeBuffer addCharCode(int charCode) => writeCharCode(charCode); | 69 CodeBuffer addCharCode(int charCode) => writeCharCode(charCode); |
| 70 | 70 |
| 71 CodeBuffer writeCharCode(int charCode) { | 71 CodeBuffer writeCharCode(int charCode) { |
| 72 return write(new String.fromCharCodes([charCode])); | 72 return write(new String.fromCharCodes([charCode])); |
| 73 } | 73 } |
| 74 | 74 |
| 75 CodeBuffer clear() { | 75 CodeBuffer clear() { |
| 76 buffer.clear(); | 76 buffer = new StringBuffer(); |
| 77 markers.clear(); | 77 markers.clear(); |
| 78 lastBufferOffset = 0; | 78 lastBufferOffset = 0; |
| 79 return this; | 79 return this; |
| 80 } | 80 } |
| 81 | 81 |
| 82 String toString() { | 82 String toString() { |
| 83 throw "Don't use CodeBuffer.toString() since it drops sourcemap data."; | 83 throw "Don't use CodeBuffer.toString() since it drops sourcemap data."; |
| 84 } | 84 } |
| 85 | 85 |
| 86 String getText() { | 86 String getText() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 110 }); | 110 }); |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 class CodeBufferMarker { | 114 class CodeBufferMarker { |
| 115 final int offsetDelta; | 115 final int offsetDelta; |
| 116 final sourcePosition; | 116 final sourcePosition; |
| 117 | 117 |
| 118 CodeBufferMarker(this.offsetDelta, this.sourcePosition); | 118 CodeBufferMarker(this.offsetDelta, this.sourcePosition); |
| 119 } | 119 } |
| OLD | NEW |