Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, 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 /* | 5 /* |
| 6 * Helper class which implements complex [Map] operations | 6 * Helper class which implements complex [Map] operations |
| 7 * in term of basic ones ([Map.getKeys], [Map.operator []], | 7 * in term of basic ones ([Map.getKeys], [Map.operator []], |
| 8 * [Map.operator []=] and [Map.remove].) Not all methods are | 8 * [Map.operator []=] and [Map.remove].) Not all methods are |
| 9 * necessary to implement each particular operation. | 9 * necessary to implement each particular operation. |
| 10 */ | 10 */ |
| 11 class Maps { | 11 class Maps { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 final result = []; | 52 final result = []; |
| 53 for (final k in map.getKeys()) { | 53 for (final k in map.getKeys()) { |
| 54 result.add(map[k]); | 54 result.add(map[k]); |
| 55 } | 55 } |
| 56 return result; | 56 return result; |
| 57 } | 57 } |
| 58 | 58 |
| 59 static int length(Map map) => map.getKeys().length; | 59 static int length(Map map) => map.getKeys().length; |
| 60 | 60 |
| 61 static bool isEmpty(Map map) => length(map) == 0; | 61 static bool isEmpty(Map map) => length(map) == 0; |
| 62 | |
| 63 /** | |
| 64 * Returns a string representing the specified map. The returned string | |
| 65 * looks like this: [:"{key0: value0, key1: value1, ... keyN: valueN}":]. | |
| 66 * The value returned by its [toString] method is used to represent each | |
| 67 * key or value. | |
| 68 * | |
| 69 * If the map collection contains a reference to itself, either | |
| 70 * directly as a key or value, or indirectly through other collections | |
| 71 * or maps, the contained reference is rendered as [:"{...}":]. This | |
| 72 * prevents the infinite regress that would otherwise occur. So, for example, | |
| 73 * calling this method on a map whose sole entry maps the string key "me" | |
| 74 * to a reference to the map would return [:"{me: {...}}":]. | |
| 75 * | |
| 76 * A typical implementation of a map's [toString] method will | |
| 77 * simply return the results of this method applied to the collection. | |
| 78 */ | |
| 79 static String mapToString(Map m) { | |
| 80 var result = new StringBuffer(); | |
| 81 _emitMap(m, result, new List()); | |
| 82 return result.toString(); | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * Appends a string representing the specified map to the specified | |
| 87 * string buffer. The string is formatted as per [mapToString]. | |
| 88 * The [:visiting:] list contains references to all of the enclosing | |
| 89 * collections and maps (which are currently in the process of being | |
| 90 * emitted into [:result:]). The [:visiting:] parameter allows this method | |
| 91 * to generate a [:"[...]":] or [:"{...}":] where required. In other words, | |
| 92 * it allows this method and [_emitCollection] to identify recursive maps | |
| 93 * and collections. | |
| 94 */ | |
| 95 static void _emitMap(Map m, StringBuffer result, List visiting) { | |
| 96 visiting.add(m); | |
| 97 result.add("{"); | |
|
Bob Nystrom
2012/02/06 18:23:17
Either way is OK, but we generally default to sing
jjb
2012/02/06 19:10:03
Interesting! This is the opposite of what I'm use
Bob Nystrom
2012/02/06 20:37:12
I think so. We find that we very frequently have H
Ivan Posva
2012/02/15 09:54:10
I think the choice is really personal preference.
| |
| 98 | |
| 99 bool first = true; | |
| 100 m.forEach((k, v) { | |
| 101 if (!first) { | |
| 102 result.add(", "); | |
| 103 } | |
| 104 first = false; | |
| 105 Collections._emitObject(k, result, visiting); | |
| 106 result.add(": "); | |
| 107 Collections._emitObject(v, result, visiting); | |
| 108 }); | |
| 109 | |
| 110 result.add("}"); | |
| 111 visiting.removeLast(); | |
| 112 } | |
| 62 } | 113 } |
| OLD | NEW |