Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Side by Side Diff: sdk/lib/collection/list.dart

Issue 18837002: Move toString() to collection classes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 dart.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * Abstract implementation of a list. 8 * Abstract implementation of a list.
9 * 9 *
10 * All operations are defined in terms of `length`, `operator[]`, 10 * All operations are defined in terms of `length`, `operator[]`,
(...skipping 10 matching lines...) Expand all
21 * `operator[]` members. It implements write operations using those and 21 * `operator[]` members. It implements write operations using those and
22 * `length=` and `operator[]=` 22 * `length=` and `operator[]=`
23 * 23 *
24 * A fixed-length list should mix this class in, and the [FixedLengthListMixin] 24 * A fixed-length list should mix this class in, and the [FixedLengthListMixin]
25 * as well, in that order, to overwrite the methods that modify the length. 25 * as well, in that order, to overwrite the methods that modify the length.
26 * 26 *
27 * An unmodifiable list should mix [UnmodifiableListMixin] on top of this 27 * An unmodifiable list should mix [UnmodifiableListMixin] on top of this
28 * mixin to prevent all modifications. 28 * mixin to prevent all modifications.
29 */ 29 */
30 abstract class ListMixin<E> implements List<E> { 30 abstract class ListMixin<E> implements List<E> {
31 // A list to identify cyclic lists during toString() calls.
32 static List _toStringList = new List();
33
31 // Iterable interface. 34 // Iterable interface.
32 Iterator<E> get iterator => new ListIterator<E>(this); 35 Iterator<E> get iterator => new ListIterator<E>(this);
33 36
34 E elementAt(int index) => this[index]; 37 E elementAt(int index) => this[index];
35 38
36 void forEach(void action(E element)) { 39 void forEach(void action(E element)) {
37 int length = this.length; 40 int length = this.length;
38 for (int i = 0; i < length; i++) { 41 for (int i = 0; i < length; i++) {
39 action(this[i]); 42 action(this[i]);
40 if (length != this.length) { 43 if (length != this.length) {
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 setRange(index, index + iterable.length, iterable); 473 setRange(index, index + iterable.length, iterable);
471 } else { 474 } else {
472 for (E element in iterable) { 475 for (E element in iterable) {
473 this[index++] = element; 476 this[index++] = element;
474 } 477 }
475 } 478 }
476 } 479 }
477 480
478 Iterable<E> get reversed => new ReversedListIterable(this); 481 Iterable<E> get reversed => new ReversedListIterable(this);
479 482
480 String toString() => ToString.iterableToString(this); 483 String toString() {
484 for (int i = 0; i < _toStringList.length; i++) {
485 if (identical(_toStringList[i], this)) { return '[...]'; }
486 }
487
488 var result = new StringBuffer();
489 try {
490 _toStringList.add(this);
491 result.write('[');
492 for (int i = 0; i < length; i++) {
floitsch 2013/07/08 16:12:34 ditto. It would be nice if join wasn't too expensi
zarah 2013/07/08 16:57:46 Use writeAll instead since it is cheaper.
493 if (i > 0) {
494 result.write(', ');
495 }
496 result.write(this[i]);
497 }
498 result.write(']');
499 } finally {
500 _toStringList.removeLast();
floitsch 2013/07/08 16:12:34 assert.
zarah 2013/07/08 16:57:46 Done.
501 }
502
503 return result.toString();
504 }
481 } 505 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698