| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 scanner; | 5 part of scanner; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Scanner that reads from a UTF-8 encoded list of bytes and creates tokens | 8 * Scanner that reads from a UTF-8 encoded list of bytes and creates tokens |
| 9 * that points to substrings. | 9 * that points to substrings. |
| 10 */ | 10 */ |
| 11 class Utf8BytesScanner extends ArrayBasedScanner { | 11 class Utf8BytesScanner extends ArrayBasedScanner { |
| 12 /** The file content. */ | 12 /** |
| 13 * The file content. |
| 14 * |
| 15 * The content is zero-terminated. |
| 16 */ |
| 13 List<int> bytes; | 17 List<int> bytes; |
| 14 | 18 |
| 15 /** | 19 /** |
| 16 * Points to the offset of the last byte returned by [advance]. | 20 * Points to the offset of the last byte returned by [advance]. |
| 17 * | 21 * |
| 18 * After invoking [currentAsUnicode], the [byteOffset] points to the last | 22 * After invoking [currentAsUnicode], the [byteOffset] points to the last |
| 19 * byte that is part of the (unicode or ASCII) character. That way, [advance] | 23 * byte that is part of the (unicode or ASCII) character. That way, [advance] |
| 20 * can always increase the byte offset by 1. | 24 * can always increase the byte offset by 1. |
| 21 */ | 25 */ |
| 22 int byteOffset = -1; | 26 int byteOffset = -1; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 /** | 63 /** |
| 60 * Creates a new Utf8BytesScanner. The source file is expected to be a | 64 * Creates a new Utf8BytesScanner. The source file is expected to be a |
| 61 * [Utf8BytesSourceFile] that holds a list of UTF-8 bytes. Otherwise the | 65 * [Utf8BytesSourceFile] that holds a list of UTF-8 bytes. Otherwise the |
| 62 * string text of the source file is decoded. | 66 * string text of the source file is decoded. |
| 63 * | 67 * |
| 64 * The list of UTF-8 bytes [file.slowUtf8Bytes()] is expected to return an | 68 * The list of UTF-8 bytes [file.slowUtf8Bytes()] is expected to return an |
| 65 * array whose last element is '0' to signal the end of the file. If this | 69 * array whose last element is '0' to signal the end of the file. If this |
| 66 * is not the case, the entire array is copied before scanning. | 70 * is not the case, the entire array is copied before scanning. |
| 67 */ | 71 */ |
| 68 Utf8BytesScanner(SourceFile file, {bool includeComments: false}) | 72 Utf8BytesScanner(SourceFile file, {bool includeComments: false}) |
| 69 : bytes = file.slowUtf8Bytes(), | 73 : bytes = file.slowUtf8ZeroTerminatedBytes(), |
| 70 super(file, includeComments) { | 74 super(file, includeComments) { |
| 71 ensureZeroTermination(); | 75 assert(bytes.last == 0); |
| 72 // Skip a leading BOM. | 76 // Skip a leading BOM. |
| 73 if (_containsBomAt(0)) byteOffset += 3; | 77 if (_containsBomAt(0)) byteOffset += 3; |
| 74 } | 78 } |
| 75 | 79 |
| 76 /** | 80 /** |
| 77 * Creates a new Utf8BytesScanner from a list of UTF-8 bytes. | 81 * Creates a new Utf8BytesScanner from a list of UTF-8 bytes. |
| 78 * | 82 * |
| 79 * The last element of the list is expected to be '0' to signal the end of | 83 * The last element of the list is expected to be '0' to signal the end of |
| 80 * the file. If this is not the case, the entire array is copied before | 84 * the file. If this is not the case, the entire array is copied before |
| 81 * scanning. | 85 * scanning. |
| 82 */ | 86 */ |
| 83 Utf8BytesScanner.fromBytes(this.bytes, {bool includeComments: false}) | 87 Utf8BytesScanner.fromBytes(List<int> zeroTerminatedBytes, |
| 84 : super(null, includeComments) { | 88 {bool includeComments: false}) |
| 85 ensureZeroTermination(); | 89 : this.bytes = zeroTerminatedBytes, |
| 86 } | 90 super(null, includeComments) { |
| 87 | 91 assert(bytes.last == 0); |
| 88 void ensureZeroTermination() { | |
| 89 if (bytes.isEmpty || bytes[bytes.length - 1] != 0) { | |
| 90 // TODO(lry): abort instead of copying the array, or warn? | |
| 91 var newBytes = new Uint8List(bytes.length + 1); | |
| 92 for (int i = 0; i < bytes.length; i++) { | |
| 93 newBytes[i] = bytes[i]; | |
| 94 } | |
| 95 newBytes[bytes.length] = 0; | |
| 96 bytes = newBytes; | |
| 97 } | |
| 98 } | 92 } |
| 99 | 93 |
| 100 bool _containsBomAt(int offset) { | 94 bool _containsBomAt(int offset) { |
| 101 const BOM_UTF8 = const [0xEF, 0xBB, 0xBF]; | 95 const BOM_UTF8 = const [0xEF, 0xBB, 0xBF]; |
| 102 | 96 |
| 103 return offset + 3 < bytes.length && | 97 return offset + 3 < bytes.length && |
| 104 bytes[offset] == BOM_UTF8[0] && | 98 bytes[offset] == BOM_UTF8[0] && |
| 105 bytes[offset + 1] == BOM_UTF8[1] && | 99 bytes[offset + 1] == BOM_UTF8[1] && |
| 106 bytes[offset + 2] == BOM_UTF8[2]; | 100 bytes[offset + 2] == BOM_UTF8[2]; |
| 107 } | 101 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 198 |
| 205 void appendSubstringToken(PrecedenceInfo info, int start, bool asciiOnly, | 199 void appendSubstringToken(PrecedenceInfo info, int start, bool asciiOnly, |
| 206 [int extraOffset = 0]) { | 200 [int extraOffset = 0]) { |
| 207 tail.next = new StringToken.fromUtf8Bytes( | 201 tail.next = new StringToken.fromUtf8Bytes( |
| 208 info, bytes, start, byteOffset + extraOffset, asciiOnly, tokenStart); | 202 info, bytes, start, byteOffset + extraOffset, asciiOnly, tokenStart); |
| 209 tail = tail.next; | 203 tail = tail.next; |
| 210 } | 204 } |
| 211 | 205 |
| 212 bool atEndOfFile() => byteOffset >= bytes.length - 1; | 206 bool atEndOfFile() => byteOffset >= bytes.length - 1; |
| 213 } | 207 } |
| OLD | NEW |