OLD | NEW |
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.dev; | 5 part of dart._collection.dev; |
6 | 6 |
7 /** | 7 /** |
8 * Temporary move `toString` methods into this class. | 8 * Temporary move `toString` methods into this class. |
9 */ | 9 */ |
10 class ToString { | 10 class ToString { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 * emitted into [:result:]). The [:visiting:] parameter allows this method to | 44 * emitted into [:result:]). The [:visiting:] parameter allows this method to |
45 * generate a [:'[...]':] or [:'{...}':] where required. In other words, | 45 * generate a [:'[...]':] or [:'{...}':] where required. In other words, |
46 * it allows this method and [_emitMap] to identify recursive collections | 46 * it allows this method and [_emitMap] to identify recursive collections |
47 * and maps. | 47 * and maps. |
48 */ | 48 */ |
49 static void _emitCollection(Collection c, | 49 static void _emitCollection(Collection c, |
50 StringBuffer result, | 50 StringBuffer result, |
51 List visiting) { | 51 List visiting) { |
52 visiting.add(c); | 52 visiting.add(c); |
53 bool isList = c is List; | 53 bool isList = c is List; |
54 result.add(isList ? '[' : '{'); | 54 result.write(isList ? '[' : '{'); |
55 | 55 |
56 bool first = true; | 56 bool first = true; |
57 for (var e in c) { | 57 for (var e in c) { |
58 if (!first) { | 58 if (!first) { |
59 result.add(', '); | 59 result.write(', '); |
60 } | 60 } |
61 first = false; | 61 first = false; |
62 _emitObject(e, result, visiting); | 62 _emitObject(e, result, visiting); |
63 } | 63 } |
64 | 64 |
65 result.add(isList ? ']' : '}'); | 65 result.write(isList ? ']' : '}'); |
66 visiting.removeLast(); | 66 visiting.removeLast(); |
67 } | 67 } |
68 | 68 |
69 /** | 69 /** |
70 * Appends a string representing the specified object to the specified | 70 * Appends a string representing the specified object to the specified |
71 * string buffer. If the object is a [Collection] or [Map], it is formatted | 71 * string buffer. If the object is a [Collection] or [Map], it is formatted |
72 * as per [collectionToString] or [mapToString]; otherwise, it is formatted | 72 * as per [collectionToString] or [mapToString]; otherwise, it is formatted |
73 * by invoking its own [toString] method. | 73 * by invoking its own [toString] method. |
74 * | 74 * |
75 * The [:visiting:] list contains references to all of the enclosing | 75 * The [:visiting:] list contains references to all of the enclosing |
76 * collections and maps (which are currently in the process of being | 76 * collections and maps (which are currently in the process of being |
77 * emitted into [:result:]). The [:visiting:] parameter allows this method | 77 * emitted into [:result:]). The [:visiting:] parameter allows this method |
78 * to generate a [:'[...]':] or [:'{...}':] where required. In other words, | 78 * to generate a [:'[...]':] or [:'{...}':] where required. In other words, |
79 * it allows this method and [_emitCollection] to identify recursive maps | 79 * it allows this method and [_emitCollection] to identify recursive maps |
80 * and collections. | 80 * and collections. |
81 */ | 81 */ |
82 static void _emitObject(Object o, StringBuffer result, List visiting) { | 82 static void _emitObject(Object o, StringBuffer result, List visiting) { |
83 if (o is Collection) { | 83 if (o is Collection) { |
84 if (_containsRef(visiting, o)) { | 84 if (_containsRef(visiting, o)) { |
85 result.add(o is List ? '[...]' : '{...}'); | 85 result.write(o is List ? '[...]' : '{...}'); |
86 } else { | 86 } else { |
87 _emitCollection(o, result, visiting); | 87 _emitCollection(o, result, visiting); |
88 } | 88 } |
89 } else if (o is Map) { | 89 } else if (o is Map) { |
90 if (_containsRef(visiting, o)) { | 90 if (_containsRef(visiting, o)) { |
91 result.add('{...}'); | 91 result.write('{...}'); |
92 } else { | 92 } else { |
93 _emitMap(o, result, visiting); | 93 _emitMap(o, result, visiting); |
94 } | 94 } |
95 } else { // o is neither a collection nor a map | 95 } else { // o is neither a collection nor a map |
96 result.add(o); | 96 result.write(o); |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 /** | 100 /** |
101 * Returns true if the specified collection contains the specified object | 101 * Returns true if the specified collection contains the specified object |
102 * reference. | 102 * reference. |
103 */ | 103 */ |
104 static _containsRef(Collection c, Object ref) { | 104 static _containsRef(Collection c, Object ref) { |
105 for (var e in c) { | 105 for (var e in c) { |
106 if (identical(e, ref)) return true; | 106 if (identical(e, ref)) return true; |
(...skipping 28 matching lines...) Expand all Loading... |
135 * string buffer. The string is formatted as per [mapToString]. | 135 * string buffer. The string is formatted as per [mapToString]. |
136 * The [:visiting:] list contains references to all of the enclosing | 136 * The [:visiting:] list contains references to all of the enclosing |
137 * collections and maps (which are currently in the process of being | 137 * collections and maps (which are currently in the process of being |
138 * emitted into [:result:]). The [:visiting:] parameter allows this method | 138 * emitted into [:result:]). The [:visiting:] parameter allows this method |
139 * to generate a [:'[...]':] or [:'{...}':] where required. In other words, | 139 * to generate a [:'[...]':] or [:'{...}':] where required. In other words, |
140 * it allows this method and [_emitCollection] to identify recursive maps | 140 * it allows this method and [_emitCollection] to identify recursive maps |
141 * and collections. | 141 * and collections. |
142 */ | 142 */ |
143 static void _emitMap(Map m, StringBuffer result, List visiting) { | 143 static void _emitMap(Map m, StringBuffer result, List visiting) { |
144 visiting.add(m); | 144 visiting.add(m); |
145 result.add('{'); | 145 result.write('{'); |
146 | 146 |
147 bool first = true; | 147 bool first = true; |
148 m.forEach((k, v) { | 148 m.forEach((k, v) { |
149 if (!first) { | 149 if (!first) { |
150 result.add(', '); | 150 result.write(', '); |
151 } | 151 } |
152 first = false; | 152 first = false; |
153 _emitObject(k, result, visiting); | 153 _emitObject(k, result, visiting); |
154 result.add(': '); | 154 result.write(': '); |
155 _emitObject(v, result, visiting); | 155 _emitObject(v, result, visiting); |
156 }); | 156 }); |
157 | 157 |
158 result.add('}'); | 158 result.write('}'); |
159 visiting.removeLast(); | 159 visiting.removeLast(); |
160 } | 160 } |
161 } | 161 } |
OLD | NEW |