| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 /** | |
| 6 * An abstract string representation. | |
| 7 */ | |
| 8 class ByteString implements SourceString { | |
| 9 final List<int> bytes; | |
| 10 final int offset; | |
| 11 final int length; | |
| 12 int _hashCode; | |
| 13 | |
| 14 ByteString(List<int> this.bytes, int this.offset, int this.length); | |
| 15 | |
| 16 abstract String get charset(); | |
| 17 | |
| 18 String slowToString() => new String.fromCharCodes( | |
| 19 new Utf8Decoder(bytes, offset, length).decodeRest()); | |
| 20 | |
| 21 String toString() => "ByteString(${slowToString()})"; | |
| 22 | |
| 23 bool operator ==(other) { | |
| 24 throw "should be overridden in subclass"; | |
| 25 } | |
| 26 | |
| 27 Iterator<int> iterator() => new Utf8Decoder(bytes, offset, length); | |
| 28 | |
| 29 int hashCode() { | |
| 30 if (_hashCode === null) { | |
| 31 _hashCode = computeHashCode(); | |
| 32 } | |
| 33 return _hashCode; | |
| 34 } | |
| 35 | |
| 36 int computeHashCode() { | |
| 37 int code = 1; | |
| 38 int end = offset + length; | |
| 39 for (int i = offset; i < end; i++) { | |
| 40 code += 19 * code + bytes[i]; | |
| 41 } | |
| 42 return code; | |
| 43 } | |
| 44 | |
| 45 printOn(StringBuffer sb) { | |
| 46 sb.add(slowToString()); | |
| 47 } | |
| 48 | |
| 49 bool isEmpty() => length == 0; | |
| 50 } | |
| 51 | |
| 52 /** | |
| 53 * A string that consists purely of 7bit ASCII characters. | |
| 54 */ | |
| 55 class AsciiString extends ByteString { | |
| 56 final String charset = "ASCII"; | |
| 57 | |
| 58 AsciiString(List<int> bytes, int offset, int length) | |
| 59 : super(bytes, offset, length); | |
| 60 | |
| 61 static AsciiString of(List<int> bytes, int offset, int length) { | |
| 62 AsciiString string = new AsciiString(bytes, offset, length); | |
| 63 return string; | |
| 64 } | |
| 65 | |
| 66 Iterator<int> iterator() => new AsciiStringIterator(bytes); | |
| 67 | |
| 68 SourceString copyWithoutQuotes(int initial, int terminal) { | |
| 69 return new AsciiString(bytes, offset + initial, | |
| 70 length - initial - terminal); | |
| 71 } | |
| 72 | |
| 73 | |
| 74 static AsciiString fromString(String string) { | |
| 75 List<int> bytes = string.charCodes(); | |
| 76 return AsciiString.of(bytes, 0, bytes.length); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 | |
| 81 class AsciiStringIterator implements Iterator<int> { | |
| 82 final List<int> bytes; | |
| 83 int offset; | |
| 84 final int end; | |
| 85 AsciiStringIterator(List<int> bytes) | |
| 86 : this.bytes = bytes, offset = 0, end = bytes.length; | |
| 87 AsciiStringIterator.range(List<int> bytes, int from, int length) | |
| 88 : this.bytes = bytes, offset = from, end = from + length; | |
| 89 bool hasNext() => offset < end; | |
| 90 int next() => bytes[offset++]; | |
| 91 } | |
| 92 | |
| 93 | |
| 94 /** | |
| 95 * A string that consists of characters that can be encoded as UTF-8. | |
| 96 */ | |
| 97 class Utf8String extends ByteString { | |
| 98 final String charset = "UTF8"; | |
| 99 | |
| 100 Utf8String(List<int> bytes, int offset, int length) | |
| 101 : super(bytes, offset, length); | |
| 102 | |
| 103 static Utf8String of(List<int> bytes, int offset, int length) { | |
| 104 return new Utf8String(bytes, offset, length); | |
| 105 } | |
| 106 | |
| 107 static Utf8String fromString(String string) { | |
| 108 throw "not implemented yet"; | |
| 109 } | |
| 110 | |
| 111 Iterator<int> iterator() => new Utf8Decoder(bytes, 0, length); | |
| 112 | |
| 113 SourceString copyWithoutQuotes(int initial, int terminal) { | |
| 114 assert((){ | |
| 115 // Only allow dropping ASCII characters, to guarantee that | |
| 116 // the resulting Utf8String is still valid. | |
| 117 for (int i = 0; i < initial; i++) { | |
| 118 if (bytes[offset + i] >= 0x80) return false; | |
| 119 } | |
| 120 for (int i = 0; i < terminal; i++) { | |
| 121 if (bytes[offset + length - terminal + i] >= 0x80) return false; | |
| 122 } | |
| 123 return true; | |
| 124 }); | |
| 125 // TODO(lrn): Check that first and last bytes use the same type of quotes. | |
| 126 return new Utf8String(bytes, offset + initial, | |
| 127 length - initial - terminal); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 /** | |
| 132 * A ByteString-valued token. | |
| 133 */ | |
| 134 class ByteStringToken extends Token { | |
| 135 final ByteString value; | |
| 136 | |
| 137 ByteStringToken(PrecedenceInfo info, ByteString this.value, int charOffset) | |
| 138 : super(info, charOffset); | |
| 139 | |
| 140 String toString() => value.toString(); | |
| 141 } | |
| OLD | NEW |