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 import 'dart:collection'; | 5 import 'dart:collection'; |
6 import '../util/characters.dart'; | 6 import '../util/characters.dart'; |
7 | 7 |
8 /** | 8 /** |
9 * The [DartString] type represents a Dart string value as a sequence of Unicode | 9 * The [DartString] type represents a Dart string value as a sequence of Unicode |
10 * Scalar Values. | 10 * Scalar Values. |
(...skipping 25 matching lines...) Expand all Loading... |
36 bool get isEmpty => length == 0; | 36 bool get isEmpty => length == 0; |
37 | 37 |
38 Iterator<int> get iterator; | 38 Iterator<int> get iterator; |
39 | 39 |
40 /** | 40 /** |
41 * The string represented by this [DartString]. | 41 * The string represented by this [DartString]. |
42 */ | 42 */ |
43 String slowToString(); | 43 String slowToString(); |
44 | 44 |
45 bool operator ==(var other) { | 45 bool operator ==(var other) { |
46 if (other is !DartString) return false; | 46 if (other is! DartString) return false; |
47 DartString otherString = other; | 47 DartString otherString = other; |
48 if (length != otherString.length) return false; | 48 if (length != otherString.length) return false; |
49 Iterator it1 = iterator; | 49 Iterator it1 = iterator; |
50 Iterator it2 = otherString.iterator; | 50 Iterator it2 = otherString.iterator; |
51 while (it1.moveNext()) { | 51 while (it1.moveNext()) { |
52 if (!it2.moveNext()) return false; | 52 if (!it2.moveNext()) return false; |
53 if (it1.current != it2.current) return false; | 53 if (it1.current != it2.current) return false; |
54 } | 54 } |
55 return true; | 55 return true; |
56 } | 56 } |
57 | 57 |
58 int get hashCode => throw new UnsupportedError('DartString.hashCode'); | 58 int get hashCode => throw new UnsupportedError('DartString.hashCode'); |
59 | 59 |
60 /** | 60 /** |
61 * A textual representation of this [DartString] with some debugging | 61 * A textual representation of this [DartString] with some debugging |
62 * information. | 62 * information. |
63 */ | 63 */ |
64 String toString() => "DartString#${length}:${slowToString()}"; | 64 String toString() => "DartString#${length}:${slowToString()}"; |
65 } | 65 } |
66 | 66 |
67 | |
68 /** | 67 /** |
69 * A [DartString] where the content is represented by an actual [String]. | 68 * A [DartString] where the content is represented by an actual [String]. |
70 */ | 69 */ |
71 class LiteralDartString extends DartString { | 70 class LiteralDartString extends DartString { |
72 final String string; | 71 final String string; |
73 const LiteralDartString(this.string); | 72 const LiteralDartString(this.string); |
74 int get length => string.length; | 73 int get length => string.length; |
75 Iterator<int> get iterator => string.codeUnits.iterator; | 74 Iterator<int> get iterator => string.codeUnits.iterator; |
76 String slowToString() => string; | 75 String slowToString() => string; |
77 } | 76 } |
(...skipping 27 matching lines...) Expand all Loading... |
105 * General case of a [SourceBasedDartString] where the source might contain | 104 * General case of a [SourceBasedDartString] where the source might contain |
106 * escapes. | 105 * escapes. |
107 */ | 106 */ |
108 class EscapedSourceDartString extends SourceBasedDartString { | 107 class EscapedSourceDartString extends SourceBasedDartString { |
109 String toStringCache; | 108 String toStringCache; |
110 EscapedSourceDartString(source, length) : super(source, length); | 109 EscapedSourceDartString(source, length) : super(source, length); |
111 Iterator<int> get iterator { | 110 Iterator<int> get iterator { |
112 if (toStringCache != null) return toStringCache.codeUnits.iterator; | 111 if (toStringCache != null) return toStringCache.codeUnits.iterator; |
113 return new StringEscapeIterator(source); | 112 return new StringEscapeIterator(source); |
114 } | 113 } |
| 114 |
115 String slowToString() { | 115 String slowToString() { |
116 if (toStringCache != null) return toStringCache; | 116 if (toStringCache != null) return toStringCache; |
117 StringBuffer buffer = new StringBuffer(); | 117 StringBuffer buffer = new StringBuffer(); |
118 StringEscapeIterator it = new StringEscapeIterator(source); | 118 StringEscapeIterator it = new StringEscapeIterator(source); |
119 while (it.moveNext()) { | 119 while (it.moveNext()) { |
120 buffer.writeCharCode(it.current); | 120 buffer.writeCharCode(it.current); |
121 } | 121 } |
122 toStringCache = buffer.toString(); | 122 toStringCache = buffer.toString(); |
123 return toStringCache; | 123 return toStringCache; |
124 } | 124 } |
(...skipping 12 matching lines...) Expand all Loading... |
137 this.right = right, | 137 this.right = right, |
138 length = left.length + right.length; | 138 length = left.length + right.length; |
139 | 139 |
140 Iterator<int> get iterator => new ConsDartStringIterator(this); | 140 Iterator<int> get iterator => new ConsDartStringIterator(this); |
141 | 141 |
142 String slowToString() { | 142 String slowToString() { |
143 if (toStringCache != null) return toStringCache; | 143 if (toStringCache != null) return toStringCache; |
144 toStringCache = left.slowToString() + right.slowToString(); | 144 toStringCache = left.slowToString() + right.slowToString(); |
145 return toStringCache; | 145 return toStringCache; |
146 } | 146 } |
| 147 |
147 String get source => slowToString(); | 148 String get source => slowToString(); |
148 } | 149 } |
149 | 150 |
150 class ConsDartStringIterator implements Iterator<int> { | 151 class ConsDartStringIterator implements Iterator<int> { |
151 HasNextIterator<int> currentIterator; | 152 HasNextIterator<int> currentIterator; |
152 DartString right; | 153 DartString right; |
153 bool hasNextLookAhead; | 154 bool hasNextLookAhead; |
154 int _current = null; | 155 int _current = null; |
155 | 156 |
156 ConsDartStringIterator(ConsDartString cons) | 157 ConsDartStringIterator(ConsDartString cons) |
(...skipping 12 matching lines...) Expand all Loading... |
169 _current = null; | 170 _current = null; |
170 return false; | 171 return false; |
171 } | 172 } |
172 _current = currentIterator.next(); | 173 _current = currentIterator.next(); |
173 hasNextLookAhead = currentIterator.hasNext; | 174 hasNextLookAhead = currentIterator.hasNext; |
174 if (!hasNextLookAhead) { | 175 if (!hasNextLookAhead) { |
175 nextPart(); | 176 nextPart(); |
176 } | 177 } |
177 return true; | 178 return true; |
178 } | 179 } |
| 180 |
179 void nextPart() { | 181 void nextPart() { |
180 if (right != null) { | 182 if (right != null) { |
181 currentIterator = new HasNextIterator<int>(right.iterator); | 183 currentIterator = new HasNextIterator<int>(right.iterator); |
182 right = null; | 184 right = null; |
183 hasNextLookAhead = currentIterator.hasNext; | 185 hasNextLookAhead = currentIterator.hasNext; |
184 } | 186 } |
185 } | 187 } |
186 } | 188 } |
187 | 189 |
188 /** | 190 /** |
189 *Iterator that returns the actual string contents of a string with escapes. | 191 *Iterator that returns the actual string contents of a string with escapes. |
190 */ | 192 */ |
191 class StringEscapeIterator implements Iterator<int>{ | 193 class StringEscapeIterator implements Iterator<int> { |
192 final Iterator<int> source; | 194 final Iterator<int> source; |
193 int _current = null; | 195 int _current = null; |
194 | 196 |
195 StringEscapeIterator(String source) : this.source = source.codeUnits.iterator; | 197 StringEscapeIterator(String source) : this.source = source.codeUnits.iterator; |
196 | 198 |
197 int get current => _current; | 199 int get current => _current; |
198 | 200 |
199 bool moveNext() { | 201 bool moveNext() { |
200 if (!source.moveNext()) { | 202 if (!source.moveNext()) { |
201 _current = null; | 203 _current = null; |
202 return false; | 204 return false; |
203 } | 205 } |
204 int code = source.current; | 206 int code = source.current; |
205 if (code != $BACKSLASH) { | 207 if (code != $BACKSLASH) { |
206 _current = code; | 208 _current = code; |
207 return true; | 209 return true; |
208 } | 210 } |
209 source.moveNext(); | 211 source.moveNext(); |
210 code = source.current; | 212 code = source.current; |
211 switch (code) { | 213 switch (code) { |
212 case $n: _current = $LF; break; | 214 case $n: |
213 case $r: _current = $CR; break; | 215 _current = $LF; |
214 case $t: _current = $TAB; break; | 216 break; |
215 case $b: _current = $BS; break; | 217 case $r: |
216 case $f: _current = $FF; break; | 218 _current = $CR; |
217 case $v: _current = $VTAB; break; | 219 break; |
| 220 case $t: |
| 221 _current = $TAB; |
| 222 break; |
| 223 case $b: |
| 224 _current = $BS; |
| 225 break; |
| 226 case $f: |
| 227 _current = $FF; |
| 228 break; |
| 229 case $v: |
| 230 _current = $VTAB; |
| 231 break; |
218 case $x: | 232 case $x: |
219 source.moveNext(); | 233 source.moveNext(); |
220 int value = hexDigitValue(source.current); | 234 int value = hexDigitValue(source.current); |
221 source.moveNext(); | 235 source.moveNext(); |
222 value = value * 16 + hexDigitValue(source.current); | 236 value = value * 16 + hexDigitValue(source.current); |
223 _current = value; | 237 _current = value; |
224 break; | 238 break; |
225 case $u: | 239 case $u: |
226 int value = 0; | 240 int value = 0; |
227 source.moveNext(); | 241 source.moveNext(); |
(...skipping 14 matching lines...) Expand all Loading... |
242 value = value * 16 + hexDigitValue(source.current); | 256 value = value * 16 + hexDigitValue(source.current); |
243 } | 257 } |
244 _current = value; | 258 _current = value; |
245 break; | 259 break; |
246 default: | 260 default: |
247 _current = code; | 261 _current = code; |
248 } | 262 } |
249 return true; | 263 return true; |
250 } | 264 } |
251 } | 265 } |
OLD | NEW |