| 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 library source_file; | 5 library source_file; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 import 'dart:convert' show UTF8; | 8 import 'dart:convert' show UTF8; |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 /** | 27 /** |
| 28 * The length of the string representation of this source file, i.e., | 28 * The length of the string representation of this source file, i.e., |
| 29 * equivalent to [:slowText().length:], but faster. | 29 * equivalent to [:slowText().length:], but faster. |
| 30 */ | 30 */ |
| 31 int get length; | 31 int get length; |
| 32 | 32 |
| 33 /** | 33 /** |
| 34 * Sets the string length of this source file. For source files based on UTF-8 | 34 * Sets the string length of this source file. For source files based on UTF-8 |
| 35 * byte arrays, the string length is computed and assigned by the scanner. | 35 * byte arrays, the string length is computed and assigned by the scanner. |
| 36 */ | 36 */ |
| 37 set length(v); | 37 set length(int v); |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * A map from line numbers to offsets in the string text representation of | 40 * A map from line numbers to offsets in the string text representation of |
| 41 * this source file. | 41 * this source file. |
| 42 */ | 42 */ |
| 43 List<int> get lineStarts { | 43 List<int> get lineStarts { |
| 44 if (lineStartsCache == null) { | 44 if (lineStartsCache == null) { |
| 45 // When reporting errors during scanning, the line numbers are not yet | 45 // When reporting errors during scanning, the line numbers are not yet |
| 46 // available and need to be computed using this slow path. | 46 // available and need to be computed using this slow path. |
| 47 lineStartsCache = lineStartsFromString(slowText()); | 47 lineStartsCache = lineStartsFromString(slowText()); |
| 48 } | 48 } |
| 49 return lineStartsCache; | 49 return lineStartsCache; |
| 50 } | 50 } |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Sets the line numbers map for this source file. This map is computed and | 53 * Sets the line numbers map for this source file. This map is computed and |
| 54 * assigned by the scanner, avoiding a separate traversal of the source file. | 54 * assigned by the scanner, avoiding a separate traversal of the source file. |
| 55 * | 55 * |
| 56 * The map contains one additional entry at the end of the file, as if the | 56 * The map contains one additional entry at the end of the file, as if the |
| 57 * source file had one more empty line at the end. This simplifies the binary | 57 * source file had one more empty line at the end. This simplifies the binary |
| 58 * search in [getLine]. | 58 * search in [getLine]. |
| 59 */ | 59 */ |
| 60 set lineStarts(v) => lineStartsCache = v; | 60 set lineStarts(List<int> v) => lineStartsCache = v; |
| 61 | 61 |
| 62 List<int> lineStartsCache; | 62 List<int> lineStartsCache; |
| 63 | 63 |
| 64 List<int> lineStartsFromString(String text) { | 64 List<int> lineStartsFromString(String text) { |
| 65 var starts = [0]; | 65 var starts = [0]; |
| 66 var index = 0; | 66 var index = 0; |
| 67 while (index < text.length) { | 67 while (index < text.length) { |
| 68 index = text.indexOf('\n', index) + 1; | 68 index = text.indexOf('\n', index) + 1; |
| 69 if (index <= 0) break; | 69 if (index <= 0) break; |
| 70 starts.add(index); | 70 starts.add(index); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 return slowText().substring(start, end); | 166 return slowText().substring(start, end); |
| 167 } | 167 } |
| 168 | 168 |
| 169 int get length { | 169 int get length { |
| 170 if (lengthCache == -1) { | 170 if (lengthCache == -1) { |
| 171 // During scanning the length is not yet assigned, so we use a slow path. | 171 // During scanning the length is not yet assigned, so we use a slow path. |
| 172 length = slowText().length; | 172 length = slowText().length; |
| 173 } | 173 } |
| 174 return lengthCache; | 174 return lengthCache; |
| 175 } | 175 } |
| 176 set length(v) => lengthCache = v; | 176 set length(int v) => lengthCache = v; |
| 177 int lengthCache = -1; | 177 int lengthCache = -1; |
| 178 } | 178 } |
| 179 | 179 |
| 180 class StringSourceFile extends SourceFile { | 180 class StringSourceFile extends SourceFile { |
| 181 | 181 |
| 182 final String text; | 182 final String text; |
| 183 | 183 |
| 184 StringSourceFile(String filename, this.text) : super(filename); | 184 StringSourceFile(String filename, this.text) : super(filename); |
| 185 | 185 |
| 186 int get length => text.length; | 186 int get length => text.length; |
| 187 set length(v) { } | 187 set length(int v) { } |
| 188 | 188 |
| 189 String slowText() => text; | 189 String slowText() => text; |
| 190 | 190 |
| 191 List<int> slowUtf8Bytes() => UTF8.encode(text); | 191 List<int> slowUtf8Bytes() => UTF8.encode(text); |
| 192 | 192 |
| 193 String slowSubstring(int start, int end) => text.substring(start, end); | 193 String slowSubstring(int start, int end) => text.substring(start, end); |
| 194 } | 194 } |
| OLD | NEW |