OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 /** | 5 /** |
6 * An abstract string representation. | 6 * An abstract string representation. |
7 */ | 7 */ |
8 class ByteString implements SourceString { | 8 abstract class ByteString extends Iterable<int> implements SourceString { |
9 final List<int> bytes; | 9 final List<int> bytes; |
10 final int offset; | 10 final int offset; |
11 final int length; | 11 final int length; |
12 int _hashCode; | 12 int _hashCode; |
13 | 13 |
14 ByteString(List<int> this.bytes, int this.offset, int this.length); | 14 ByteString(List<int> this.bytes, int this.offset, int this.length); |
15 | 15 |
16 String get charset; | 16 String get charset; |
17 | 17 |
18 String slowToString() => new String.fromCharCodes( | 18 String slowToString() => new String.fromCharCodes( |
19 new Utf8Decoder(bytes, offset, length).decodeRest()); | 19 new Utf8Decoder(bytes, offset, length).decodeRest()); |
20 | 20 |
21 String toString() => "ByteString(${slowToString()})"; | 21 String toString() => "ByteString(${slowToString()})"; |
22 | 22 |
23 bool operator ==(other) { | 23 bool operator ==(other) { |
24 throw "should be overridden in subclass"; | 24 throw "should be overridden in subclass"; |
25 } | 25 } |
26 | 26 |
27 Iterator<int> iterator() => new Utf8Decoder(bytes, offset, length); | 27 Iterator<int> get iterator => new Utf8Decoder(bytes, offset, length); |
28 | 28 |
29 int get hashCode { | 29 int get hashCode { |
30 if (_hashCode == null) { | 30 if (_hashCode == null) { |
31 _hashCode = computeHashCode(); | 31 _hashCode = computeHashCode(); |
32 } | 32 } |
33 return _hashCode; | 33 return _hashCode; |
34 } | 34 } |
35 | 35 |
36 int computeHashCode() { | 36 int computeHashCode() { |
37 int code = 1; | 37 int code = 1; |
(...skipping 21 matching lines...) Expand all Loading... |
59 final String charset = "ASCII"; | 59 final String charset = "ASCII"; |
60 | 60 |
61 AsciiString(List<int> bytes, int offset, int length) | 61 AsciiString(List<int> bytes, int offset, int length) |
62 : super(bytes, offset, length); | 62 : super(bytes, offset, length); |
63 | 63 |
64 static AsciiString of(List<int> bytes, int offset, int length) { | 64 static AsciiString of(List<int> bytes, int offset, int length) { |
65 AsciiString string = new AsciiString(bytes, offset, length); | 65 AsciiString string = new AsciiString(bytes, offset, length); |
66 return string; | 66 return string; |
67 } | 67 } |
68 | 68 |
69 Iterator<int> iterator() => new AsciiStringIterator(bytes); | 69 Iterator<int> get iterator => new AsciiStringIterator(bytes); |
70 | 70 |
71 SourceString copyWithoutQuotes(int initial, int terminal) { | 71 SourceString copyWithoutQuotes(int initial, int terminal) { |
72 return new AsciiString(bytes, offset + initial, | 72 return new AsciiString(bytes, offset + initial, |
73 length - initial - terminal); | 73 length - initial - terminal); |
74 } | 74 } |
75 | 75 |
76 | 76 |
77 static AsciiString fromString(String string) { | 77 static AsciiString fromString(String string) { |
78 List<int> bytes = string.charCodes; | 78 List<int> bytes = string.charCodes; |
79 return AsciiString.of(bytes, 0, bytes.length); | 79 return AsciiString.of(bytes, 0, bytes.length); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 | 83 |
84 class AsciiStringIterator implements Iterator<int> { | 84 class AsciiStringIterator implements Iterator<int> { |
85 final List<int> bytes; | 85 final List<int> bytes; |
86 int offset; | 86 int offset; |
87 final int end; | 87 final int end; |
| 88 int _current; |
| 89 |
88 AsciiStringIterator(List<int> bytes) | 90 AsciiStringIterator(List<int> bytes) |
89 : this.bytes = bytes, offset = 0, end = bytes.length; | 91 : this.bytes = bytes, offset = 0, end = bytes.length; |
90 AsciiStringIterator.range(List<int> bytes, int from, int length) | 92 AsciiStringIterator.range(List<int> bytes, int from, int length) |
91 : this.bytes = bytes, offset = from, end = from + length; | 93 : this.bytes = bytes, offset = from, end = from + length; |
92 bool get hasNext => offset < end; | 94 |
93 int next() => bytes[offset++]; | 95 int get current => _current; |
| 96 bool moveNext() { |
| 97 if (offset < end) { |
| 98 _current = bytes[offset++]; |
| 99 return true; |
| 100 } |
| 101 _current = null; |
| 102 return false; |
| 103 } |
94 } | 104 } |
95 | 105 |
96 | 106 |
97 /** | 107 /** |
98 * A string that consists of characters that can be encoded as UTF-8. | 108 * A string that consists of characters that can be encoded as UTF-8. |
99 */ | 109 */ |
100 class Utf8String extends ByteString { | 110 class Utf8String extends ByteString { |
101 final String charset = "UTF8"; | 111 final String charset = "UTF8"; |
102 | 112 |
103 Utf8String(List<int> bytes, int offset, int length) | 113 Utf8String(List<int> bytes, int offset, int length) |
104 : super(bytes, offset, length); | 114 : super(bytes, offset, length); |
105 | 115 |
106 static Utf8String of(List<int> bytes, int offset, int length) { | 116 static Utf8String of(List<int> bytes, int offset, int length) { |
107 return new Utf8String(bytes, offset, length); | 117 return new Utf8String(bytes, offset, length); |
108 } | 118 } |
109 | 119 |
110 static Utf8String fromString(String string) { | 120 static Utf8String fromString(String string) { |
111 throw "not implemented yet"; | 121 throw "not implemented yet"; |
112 } | 122 } |
113 | 123 |
114 Iterator<int> iterator() => new Utf8Decoder(bytes, 0, length); | 124 Iterator<int> get iterator => new Utf8Decoder(bytes, 0, length); |
115 | 125 |
116 SourceString copyWithoutQuotes(int initial, int terminal) { | 126 SourceString copyWithoutQuotes(int initial, int terminal) { |
117 assert((){ | 127 assert((){ |
118 // Only allow dropping ASCII characters, to guarantee that | 128 // Only allow dropping ASCII characters, to guarantee that |
119 // the resulting Utf8String is still valid. | 129 // the resulting Utf8String is still valid. |
120 for (int i = 0; i < initial; i++) { | 130 for (int i = 0; i < initial; i++) { |
121 if (bytes[offset + i] >= 0x80) return false; | 131 if (bytes[offset + i] >= 0x80) return false; |
122 } | 132 } |
123 for (int i = 0; i < terminal; i++) { | 133 for (int i = 0; i < terminal; i++) { |
124 if (bytes[offset + length - terminal + i] >= 0x80) return false; | 134 if (bytes[offset + length - terminal + i] >= 0x80) return false; |
(...skipping 10 matching lines...) Expand all Loading... |
135 * A ByteString-valued token. | 145 * A ByteString-valued token. |
136 */ | 146 */ |
137 class ByteStringToken extends Token { | 147 class ByteStringToken extends Token { |
138 final ByteString value; | 148 final ByteString value; |
139 | 149 |
140 ByteStringToken(PrecedenceInfo info, ByteString this.value, int charOffset) | 150 ByteStringToken(PrecedenceInfo info, ByteString this.value, int charOffset) |
141 : super(info, charOffset); | 151 : super(info, charOffset); |
142 | 152 |
143 String toString() => value.toString(); | 153 String toString() => value.toString(); |
144 } | 154 } |
OLD | NEW |