| 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 dart2js.io.source_file; | 5 library dart2js.io.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 import 'line_column_provider.dart'; | 10 import 'line_column_provider.dart'; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Represents a file of source code. The content can be either a [String] or | 13 * Represents a file of source code. The content can be either a [String] or |
| 14 * a UTF-8 encoded [List<int>] of bytes. | 14 * a UTF-8 encoded [List<int>] of bytes. |
| 15 */ | 15 */ |
| 16 abstract class SourceFile implements LineColumnProvider { | 16 abstract class SourceFile implements LineColumnProvider { |
| 17 /// The absolute URI of the source file. |
| 18 Uri get uri; |
| 17 | 19 |
| 18 /** The name of the file. */ | 20 /// The name of the file. |
| 19 String get filename; | 21 /// |
| 22 /// This is [uri], maybe relativized to a more human-readable form. |
| 23 String get filename => uri.toString(); |
| 20 | 24 |
| 21 /** The text content of the file represented as a String. */ | 25 /** The text content of the file represented as a String. */ |
| 22 String slowText(); | 26 String slowText(); |
| 23 | 27 |
| 24 /** The content of the file represented as a UTF-8 encoded [List<int>]. */ | 28 /** The content of the file represented as a UTF-8 encoded [List<int>]. */ |
| 25 List<int> slowUtf8Bytes(); | 29 List<int> slowUtf8Bytes(); |
| 26 | 30 |
| 27 /** | 31 /** |
| 28 * The length of the string representation of this source file, i.e., | 32 * The length of the string representation of this source file, i.e., |
| 29 * equivalent to [:slowText().length:], but faster. | 33 * equivalent to [:slowText().length:], but faster. |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 for (; i < toColumn; i++) { | 162 for (; i < toColumn; i++) { |
| 159 buf.write(colorize('^')); | 163 buf.write(colorize('^')); |
| 160 } | 164 } |
| 161 } | 165 } |
| 162 | 166 |
| 163 return buf.toString(); | 167 return buf.toString(); |
| 164 } | 168 } |
| 165 } | 169 } |
| 166 | 170 |
| 167 class Utf8BytesSourceFile extends SourceFile { | 171 class Utf8BytesSourceFile extends SourceFile { |
| 168 final String filename; | 172 final Uri uri; |
| 169 | 173 |
| 170 /** The UTF-8 encoded content of the source file. */ | 174 /** The UTF-8 encoded content of the source file. */ |
| 171 final List<int> content; | 175 final List<int> content; |
| 172 | 176 |
| 173 Utf8BytesSourceFile(this.filename, this.content); | 177 Utf8BytesSourceFile(this.uri, this.content); |
| 174 | 178 |
| 175 String slowText() => UTF8.decode(content); | 179 String slowText() => UTF8.decode(content); |
| 176 | 180 |
| 177 List<int> slowUtf8Bytes() => content; | 181 List<int> slowUtf8Bytes() => content; |
| 178 | 182 |
| 179 String slowSubstring(int start, int end) { | 183 String slowSubstring(int start, int end) { |
| 180 // TODO(lry): to make this faster, the scanner could record the UTF-8 slack | 184 // TODO(lry): to make this faster, the scanner could record the UTF-8 slack |
| 181 // for all positions of the source text. We could use [:content.sublist:]. | 185 // for all positions of the source text. We could use [:content.sublist:]. |
| 182 return slowText().substring(start, end); | 186 return slowText().substring(start, end); |
| 183 } | 187 } |
| 184 | 188 |
| 185 int get length { | 189 int get length { |
| 186 if (lengthCache == -1) { | 190 if (lengthCache == -1) { |
| 187 // During scanning the length is not yet assigned, so we use a slow path. | 191 // During scanning the length is not yet assigned, so we use a slow path. |
| 188 length = slowText().length; | 192 length = slowText().length; |
| 189 } | 193 } |
| 190 return lengthCache; | 194 return lengthCache; |
| 191 } | 195 } |
| 192 set length(int v) => lengthCache = v; | 196 set length(int v) => lengthCache = v; |
| 193 int lengthCache = -1; | 197 int lengthCache = -1; |
| 194 } | 198 } |
| 195 | 199 |
| 196 class CachingUtf8BytesSourceFile extends Utf8BytesSourceFile { | 200 class CachingUtf8BytesSourceFile extends Utf8BytesSourceFile { |
| 197 String cachedText; | 201 String cachedText; |
| 202 final String filename; |
| 198 | 203 |
| 199 CachingUtf8BytesSourceFile(String filename, List<int> content) | 204 CachingUtf8BytesSourceFile(Uri uri, this.filename, List<int> content) |
| 200 : super(filename, content); | 205 : super(uri, content); |
| 201 | 206 |
| 202 String slowText() { | 207 String slowText() { |
| 203 if (cachedText == null) { | 208 if (cachedText == null) { |
| 204 cachedText = super.slowText(); | 209 cachedText = super.slowText(); |
| 205 } | 210 } |
| 206 return cachedText; | 211 return cachedText; |
| 207 } | 212 } |
| 208 } | 213 } |
| 209 | 214 |
| 210 class StringSourceFile extends SourceFile { | 215 class StringSourceFile extends SourceFile { |
| 216 final Uri uri; |
| 211 final String filename; | 217 final String filename; |
| 212 final String text; | 218 final String text; |
| 213 | 219 |
| 214 StringSourceFile(this.filename, this.text); | 220 StringSourceFile(this.uri, this.filename, this.text); |
| 221 |
| 222 StringSourceFile.fromUri(Uri uri, String text) |
| 223 : this(uri, uri.toString(), text); |
| 224 |
| 225 StringSourceFile.fromName(String filename, String text) |
| 226 : this(new Uri(path: filename), filename, text); |
| 215 | 227 |
| 216 int get length => text.length; | 228 int get length => text.length; |
| 217 set length(int v) { } | 229 set length(int v) { } |
| 218 | 230 |
| 219 String slowText() => text; | 231 String slowText() => text; |
| 220 | 232 |
| 221 List<int> slowUtf8Bytes() => UTF8.encode(text); | 233 List<int> slowUtf8Bytes() => UTF8.encode(text); |
| 222 | 234 |
| 223 String slowSubstring(int start, int end) => text.substring(start, end); | 235 String slowSubstring(int start, int end) => text.substring(start, end); |
| 224 } | 236 } |
| OLD | NEW |