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 protobuf; | 5 part of protobuf; |
6 | 6 |
7 // TODO(antonm): reconsider later if PbList should take care of equality. | 7 // TODO(antonm): reconsider later if PbList should take care of equality. |
8 bool _deepEquals(lhs, rhs) { | 8 bool _deepEquals(lhs, rhs) { |
9 // Some GeneratedMessages implement Map, so test this first. | |
10 if ((lhs is GeneratedMessage) && (rhs is GeneratedMessage)) { | |
Søren Gjesse
2016/04/04 16:06:27
Maybe negate this if to only have one lhs == rhs.
skybrian
2016/04/04 19:38:02
As a style thing, I'd rather repeat the == check t
| |
11 return lhs == rhs; | |
12 } | |
9 if ((lhs is List) && (rhs is List)) return _areListsEqual(lhs, rhs); | 13 if ((lhs is List) && (rhs is List)) return _areListsEqual(lhs, rhs); |
10 if ((lhs is Map) && (rhs is Map)) return _areMapsEqual(lhs, rhs); | 14 if ((lhs is Map) && (rhs is Map)) return _areMapsEqual(lhs, rhs); |
11 if ((lhs is ByteData) && (rhs is ByteData)) { | 15 if ((lhs is ByteData) && (rhs is ByteData)) { |
12 return _areByteDataEqual(lhs, rhs); | 16 return _areByteDataEqual(lhs, rhs); |
13 } | 17 } |
14 return lhs == rhs; | 18 return lhs == rhs; |
15 } | 19 } |
16 | 20 |
17 bool _areListsEqual(List lhs, List rhs) { | 21 bool _areListsEqual(List lhs, List rhs) { |
18 range(i) => new Iterable.generate(i, (i) => i); | 22 range(i) => new Iterable.generate(i, (i) => i); |
19 | 23 |
20 if (lhs.length != rhs.length) return false; | 24 if (lhs.length != rhs.length) return false; |
21 return range(lhs.length).every((i) => _deepEquals(lhs[i], rhs[i])); | 25 return range(lhs.length).every((i) => _deepEquals(lhs[i], rhs[i])); |
22 } | 26 } |
23 | 27 |
24 bool _areMapsEqual(Map lhs, Map rhs) { | 28 bool _areMapsEqual(Map lhs, Map rhs) { |
25 if (lhs.length != rhs.length) return false; | 29 if (lhs.length != rhs.length) return false; |
26 return lhs.keys.every((key) => _deepEquals(lhs[key], rhs[key])); | 30 return lhs.keys.every((key) => _deepEquals(lhs[key], rhs[key])); |
27 } | 31 } |
28 | 32 |
29 bool _areByteDataEqual(ByteData lhs, ByteData rhs) { | 33 bool _areByteDataEqual(ByteData lhs, ByteData rhs) { |
30 asBytes(d) => new Uint8List.view(d.buffer, d.offsetInBytes, d.lengthInBytes); | 34 asBytes(d) => new Uint8List.view(d.buffer, d.offsetInBytes, d.lengthInBytes); |
31 return _areListsEqual(asBytes(lhs), asBytes(rhs)); | 35 return _areListsEqual(asBytes(lhs), asBytes(rhs)); |
32 } | 36 } |
33 | 37 |
34 List/*<T>*/ sorted/*<T>*/(Iterable/*<T>*/ list) => new List.from(list)..sort(); | 38 List/*<T>*/ sorted/*<T>*/(Iterable/*<T>*/ list) => new List.from(list)..sort(); |
OLD | NEW |