Skip to content

Commit 8ed47ae

Browse files
committed
Move toString() to collection classes.
R=floitsch@google.com Review URL: https://codereview.chromium.org//18837002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@24836 260f80e4-7a28-3924-810f-c04153c831b5
1 parent 2985f34 commit 8ed47ae

File tree

13 files changed

+196
-16
lines changed

13 files changed

+196
-16
lines changed

runtime/lib/array.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class _ObjectArray<E> implements List<E> {
1414
void operator []=(int index, E value) native "ObjectArray_setIndexed";
1515

1616
String toString() {
17-
return ToString.iterableToString(this);
17+
return IterableMixinWorkaround.toStringIterable(this,'[' , ']');
1818
}
1919

2020
int get length native "ObjectArray_getLength";
@@ -457,7 +457,7 @@ class _ImmutableArray<E> implements List<E> {
457457
}
458458

459459
String toString() {
460-
return ToString.iterableToString(this);
460+
return IterableMixinWorkaround.toStringIterable(this, '[', ']');
461461
}
462462

463463
int indexOf(Object element, [int start = 0]) {

runtime/lib/growable_array.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ class _GrowableObjectArray<T> implements List<T> {
337337
}
338338

339339
String toString() {
340-
return ToString.iterableToString(this);
340+
return IterableMixinWorkaround.toStringIterable(this, '[', ']');
341341
}
342342

343343
Iterator<T> get iterator {

runtime/lib/typed_data.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ patch class ByteData {
279279
// the collection and list interfaces.
280280

281281
abstract class _TypedListBase {
282+
282283
// Method(s) implementing the Collection interface.
283284
bool contains(element) => IterableMixinWorkaround.contains(this, element);
284285

@@ -505,7 +506,7 @@ abstract class _TypedListBase {
505506
// Method(s) implementing Object interface.
506507

507508
String toString() {
508-
return ToString.iterableToString(this);
509+
return IterableMixinWorkaround.toStringIterable(this, '[', ']');
509510
}
510511

511512

sdk/lib/_collection_dev/iterable.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,9 @@ abstract class BidirectionalIterator<T> implements Iterator<T> {
710710
* The uses of this class will be replaced by mixins.
711711
*/
712712
class IterableMixinWorkaround {
713+
// A list to identify cyclic collections during toString() calls.
714+
static List _toStringList = new List();
715+
713716
static bool contains(Iterable iterable, var element) {
714717
for (final e in iterable) {
715718
if (e == element) return true;
@@ -903,6 +906,27 @@ class IterableMixinWorkaround {
903906
return buffer.toString();
904907
}
905908

909+
static String toStringIterable(Iterable iterable, String leftDelimiter,
910+
String rightDelimiter) {
911+
for (int i = 0; i < _toStringList.length; i++) {
912+
if (identical(_toStringList[i], iterable)) {
913+
return '$leftDelimiter...$rightDelimiter';
914+
}
915+
}
916+
917+
StringBuffer result = new StringBuffer();
918+
try {
919+
_toStringList.add(iterable);
920+
result.write(leftDelimiter);
921+
result.writeAll(iterable, ', ');
922+
result.write(rightDelimiter);
923+
} finally {
924+
assert(identical(_toStringList.last, iterable));
925+
_toStringList.removeLast();
926+
}
927+
return result.toString();
928+
}
929+
906930
static Iterable where(Iterable iterable, bool f(var element)) {
907931
return new WhereIterable(iterable, f);
908932
}

sdk/lib/_internal/lib/js_array.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ part of _interceptors;
1111
* argument added to each member.
1212
*/
1313
class JSArray<E> extends Interceptor implements List<E>, JSIndexable {
14+
1415
const JSArray();
1516

1617
checkMutable(reason) {
17-
if (this is !JSMutableArray) {
18+
if (this is !JSMutableArray) {
1819
throw new UnsupportedError(reason);
1920
}
2021
}
@@ -261,7 +262,7 @@ class JSArray<E> extends Interceptor implements List<E>, JSIndexable {
261262

262263
bool get isNotEmpty => !isEmpty;
263264

264-
String toString() => ToString.iterableToString(this);
265+
String toString() => IterableMixinWorkaround.toStringIterable(this, '[', ']');
265266

266267
List<E> toList({ bool growable: true }) =>
267268
new List<E>.from(this, growable: growable);

sdk/lib/collection/hash_set.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ part of dart.collection;
66

77
/** Common parts of [HashSet] and [LinkedHashSet] implementations. */
88
abstract class _HashSetBase<E> extends IterableBase<E> implements Set<E> {
9+
910
// Set.
1011
bool containsAll(Iterable<Object> other) {
1112
for (Object object in other) {
@@ -53,7 +54,8 @@ abstract class _HashSetBase<E> extends IterableBase<E> implements Set<E> {
5354
retainWhere(retainSet.contains);
5455
}
5556

56-
String toString() => ToString.iterableToString(this);
57+
// TODO(zarah) Remove this, and let it be inherited by IterableBase
58+
String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}');
5759
}
5860

5961
/**

sdk/lib/collection/linked_list.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ part of dart.collection;
1414
class LinkedList<E extends LinkedListEntry<E>>
1515
extends IterableBase<E>
1616
implements _LinkedListLink {
17+
1718
int _modificationCount = 0;
1819
int _length = 0;
1920
_LinkedListLink _next;
@@ -60,7 +61,8 @@ class LinkedList<E extends LinkedListEntry<E>>
6061

6162
Iterator<E> get iterator => new _LinkedListIterator<E>(this);
6263

63-
String toString() => ToString.iterableToString(this);
64+
// TODO(zarah) Remove this, and let it be inherited by IterableMixin
65+
String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}');
6466

6567
int get length => _length;
6668

sdk/lib/collection/list.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ typedef ListBase<E> = Object with ListMixin<E>;
2828
* mixin to prevent all modifications.
2929
*/
3030
abstract class ListMixin<E> implements List<E> {
31+
// A list to identify cyclic lists during toString() calls.
32+
static List _toStringList = new List();
33+
3134
// Iterable interface.
3235
Iterator<E> get iterator => new ListIterator<E>(this);
3336

@@ -477,5 +480,22 @@ abstract class ListMixin<E> implements List<E> {
477480

478481
Iterable<E> get reversed => new ReversedListIterable(this);
479482

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+
result.writeAll(this, ', ');
493+
result.write(']');
494+
} finally {
495+
assert(identical(_toStringList.last, this));
496+
_toStringList.removeLast();
497+
}
498+
499+
return result.toString();
500+
}
481501
}

sdk/lib/collection/maps.dart

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class Maps {
6060

6161
static bool isNotEmpty(Map map) => map.keys.isNotEmpty;
6262

63+
// A list to identify cyclic maps during toString() calls.
64+
static List _toStringList = new List();
65+
6366
/**
6467
* Returns a string representing the specified map. The returned string
6568
* looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':].
@@ -76,7 +79,33 @@ class Maps {
7679
* A typical implementation of a map's [toString] method will
7780
* simply return the results of this method applied to the collection.
7881
*/
79-
static String mapToString(Map m) => ToString.mapToString(m);
82+
static String mapToString(Map m) {
83+
for (int i = 0; i < _toStringList.length; i++) {
84+
if (identical(_toStringList[i], m)) { return '{...}'; }
85+
}
86+
87+
var result = new StringBuffer();
88+
try {
89+
_toStringList.add(m);
90+
result.write('{');
91+
bool first = true;
92+
m.forEach((k, v) {
93+
if(!first) {
94+
result.write(', ');
95+
}
96+
first = false;
97+
result.write(k);
98+
result.write(': ');
99+
result.write(v);
100+
});
101+
result.write('}');
102+
} finally {
103+
assert(identical(_toStringList.last, m));
104+
_toStringList.removeLast();
105+
}
106+
107+
return result.toString();
108+
}
80109

81110
static _id(x) => x;
82111

sdk/lib/collection/queue.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,8 @@ class DoubleLinkedQueue<E> extends IterableBase<E> implements Queue<E> {
301301
return new _DoubleLinkedQueueIterator<E>(_sentinel);
302302
}
303303

304-
String toString() {
305-
return ToString.iterableToString(this);
306-
}
304+
// TODO(zarah) Remove this, and let it be inherited by IterableBase
305+
String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}');
307306
}
308307

309308
class _DoubleLinkedQueueIterator<E> implements Iterator<E> {
@@ -530,9 +529,8 @@ class ListQueue<E> extends IterableBase<E> implements Queue<E> {
530529
}
531530
}
532531

533-
String toString() {
534-
return ToString.iterableToString(this);
535-
}
532+
// TODO(zarah) Remove this, and let it be inherited by IterableBase
533+
String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}');
536534

537535
// Queue interface.
538536

0 commit comments

Comments
 (0)