| 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 import 'dart:typed_data' show Uint8List; |
| 9 | 10 |
| 10 import 'line_column_provider.dart'; | 11 import 'line_column_provider.dart'; |
| 11 | 12 |
| 12 /** | 13 /** |
| 13 * Represents a file of source code. The content can be either a [String] or | 14 * Represents a file of source code. The content can be either a [String] or |
| 14 * a UTF-8 encoded [List<int>] of bytes. | 15 * a UTF-8 encoded [List<int>] of bytes. |
| 15 */ | 16 */ |
| 16 abstract class SourceFile implements LineColumnProvider { | 17 abstract class SourceFile implements LineColumnProvider { |
| 17 /// The absolute URI of the source file. | 18 /// The absolute URI of the source file. |
| 18 Uri get uri; | 19 Uri get uri; |
| 19 | 20 |
| 20 /// The name of the file. | 21 /// The name of the file. |
| 21 /// | 22 /// |
| 22 /// This is [uri], maybe relativized to a more human-readable form. | 23 /// This is [uri], maybe relativized to a more human-readable form. |
| 23 String get filename => uri.toString(); | 24 String get filename => uri.toString(); |
| 24 | 25 |
| 25 /** The text content of the file represented as a String. */ | 26 /** The text content of the file represented as a String. */ |
| 26 String slowText(); | 27 String slowText(); |
| 27 | 28 |
| 28 /** The content of the file represented as a UTF-8 encoded [List<int>]. */ | 29 /** |
| 29 List<int> slowUtf8Bytes(); | 30 * The content of the file represented as a UTF-8 encoded [List<int>], |
| 31 * terminated with a trailing 0 byte. |
| 32 */ |
| 33 List<int> slowUtf8ZeroTerminatedBytes(); |
| 30 | 34 |
| 31 /** | 35 /** |
| 32 * The length of the string representation of this source file, i.e., | 36 * The length of the string representation of this source file, i.e., |
| 33 * equivalent to [:slowText().length:], but faster. | 37 * equivalent to [:slowText().length:], but faster. |
| 34 */ | 38 */ |
| 35 int get length; | 39 int get length; |
| 36 | 40 |
| 37 /** | 41 /** |
| 38 * Sets the string length of this source file. For source files based on UTF-8 | 42 * Sets the string length of this source file. For source files based on UTF-8 |
| 39 * byte arrays, the string length is computed and assigned by the scanner. | 43 * byte arrays, the string length is computed and assigned by the scanner. |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 165 |
| 162 for (; i < toColumn; i++) { | 166 for (; i < toColumn; i++) { |
| 163 buf.write(colorize('^')); | 167 buf.write(colorize('^')); |
| 164 } | 168 } |
| 165 } | 169 } |
| 166 | 170 |
| 167 return buf.toString(); | 171 return buf.toString(); |
| 168 } | 172 } |
| 169 } | 173 } |
| 170 | 174 |
| 175 List<int> _zeroTerminateIfNecessary(List<int> bytes) { |
| 176 if (bytes.length > 0 && bytes.last == 0) return bytes; |
| 177 List<int> result = new Uint8List(bytes.length + 1); |
| 178 result.setRange(0, bytes.length, bytes); |
| 179 result[result.length - 1] = 0; |
| 180 return result; |
| 181 } |
| 182 |
| 171 class Utf8BytesSourceFile extends SourceFile { | 183 class Utf8BytesSourceFile extends SourceFile { |
| 172 final Uri uri; | 184 final Uri uri; |
| 173 | 185 |
| 174 /** The UTF-8 encoded content of the source file. */ | 186 /** The UTF-8 encoded content of the source file. */ |
| 175 final List<int> content; | 187 final List<int> zeroTerminatedContent; |
| 176 | 188 |
| 177 Utf8BytesSourceFile(this.uri, this.content); | 189 /** |
| 190 * Creates a Utf8BytesSourceFile. |
| 191 * |
| 192 * If possible, the given [content] should be zero-terminated. If it isn't, |
| 193 * the constructor clones the content and adds a trailing 0. |
| 194 */ |
| 195 Utf8BytesSourceFile(this.uri, List<int> content) |
| 196 : this.zeroTerminatedContent = _zeroTerminateIfNecessary(content); |
| 178 | 197 |
| 179 String slowText() => UTF8.decode(content); | 198 String slowText() { |
| 199 // Don't convert the trailing zero byte. |
| 200 return UTF8.decoder.convert( |
| 201 zeroTerminatedContent, 0, zeroTerminatedContent.length - 1); |
| 202 } |
| 180 | 203 |
| 181 List<int> slowUtf8Bytes() => content; | 204 List<int> slowUtf8ZeroTerminatedBytes() => zeroTerminatedContent; |
| 182 | 205 |
| 183 String slowSubstring(int start, int end) { | 206 String slowSubstring(int start, int end) { |
| 184 // TODO(lry): to make this faster, the scanner could record the UTF-8 slack | 207 // TODO(lry): to make this faster, the scanner could record the UTF-8 slack |
| 185 // for all positions of the source text. We could use [:content.sublist:]. | 208 // for all positions of the source text. We could use [:content.sublist:]. |
| 186 return slowText().substring(start, end); | 209 return slowText().substring(start, end); |
| 187 } | 210 } |
| 188 | 211 |
| 189 int get length { | 212 int get length { |
| 190 if (lengthCache == -1) { | 213 if (lengthCache == -1) { |
| 191 // During scanning the length is not yet assigned, so we use a slow path. | 214 // During scanning the length is not yet assigned, so we use a slow path. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 : this(uri, uri.toString(), text); | 246 : this(uri, uri.toString(), text); |
| 224 | 247 |
| 225 StringSourceFile.fromName(String filename, String text) | 248 StringSourceFile.fromName(String filename, String text) |
| 226 : this(new Uri(path: filename), filename, text); | 249 : this(new Uri(path: filename), filename, text); |
| 227 | 250 |
| 228 int get length => text.length; | 251 int get length => text.length; |
| 229 set length(int v) { } | 252 set length(int v) { } |
| 230 | 253 |
| 231 String slowText() => text; | 254 String slowText() => text; |
| 232 | 255 |
| 233 List<int> slowUtf8Bytes() => UTF8.encode(text); | 256 List<int> slowUtf8ZeroTerminatedBytes() { |
| 257 return _zeroTerminateIfNecessary(UTF8.encode(text)); |
| 258 } |
| 234 | 259 |
| 235 String slowSubstring(int start, int end) => text.substring(start, end); | 260 String slowSubstring(int start, int end) => text.substring(start, end); |
| 236 } | 261 } |
| OLD | NEW |