Chromium Code Reviews| Index: tools/testing/dart/utils.dart |
| diff --git a/tools/testing/dart/utils.dart b/tools/testing/dart/utils.dart |
| index 3ddf330b75bf8e91b047e74abf5d4fa9c9310037..908013ae71235f91c0af588a861d10c59e872a26 100644 |
| --- a/tools/testing/dart/utils.dart |
| +++ b/tools/testing/dart/utils.dart |
| @@ -64,6 +64,54 @@ class DebugLogger { |
| static String get _datetime => "${new DateTime.now()}"; |
| } |
| + |
| +/** |
| + * [areByteArraysEqual] compares a range of bytes from [buffer1] with a |
| + * range of bytes from [buffer2]. |
| + * |
| + * Returns [true] if the [count] bytes in [buffer1] (starting at |
| + * [offset1]) match the [count] bytes in [buffer2] (starting at |
| + * [offset2]). |
| + * Otherwise [false] is returned. |
| + */ |
| +bool areByteArraysEqual(List<int> buffer1, int offset1, |
| + List<int> buffer2, int offset2, |
| + int count) { |
| + if ((offset1 + count) > buffer1.length || |
| + (offset2 + count) > buffer2.length) { |
| + return false; |
| + } |
| + |
| + for (var i = 0; i < count; i++) { |
| + if (buffer1[offset1 + i] != buffer2[offset2 + i]) { |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +/** |
| + * [findBytes] searches for [pattern] in [data] beginning at [startPos]. |
| + * |
| + * Returns [true] if [pattern] was found in [data]. |
| + * Otherwise [false] is returned. |
| + */ |
| +int findBytes(List<int> data, List<int> pattern, [int startPos=0]) { |
| + // TODO(kustermann): Use one of the fast string-matching algorithms! |
| + for (int i=startPos; i < (data.length-pattern.length); i++) { |
|
ricow1
2013/07/30 09:30:11
space around =
I would remove parenthesis around d
kustermann
2013/07/31 15:53:54
Done. Kept the parenthesis (it's not that obvious
ricow1
2013/08/01 13:26:21
OH, as discussed offline I though length-pattern w
|
| + bool found = true; |
| + for (int j=0; j<pattern.length; j++) { |
|
ricow1
2013/07/30 09:30:11
space around = and <
kustermann
2013/07/31 15:53:54
Done.
|
| + if (data[i+j] != pattern[j]) { |
| + found = false; |
|
ricow1
2013/07/30 09:30:11
break here?
kustermann
2013/07/31 15:53:54
Done.
|
| + } |
| + } |
| + if (found) { |
| + return i; |
| + } |
| + } |
| + return -1; |
| +} |
| + |
| List<int> encodeUtf8(String string) { |
| return utf.encodeUtf8(string); |
| } |
| @@ -84,3 +132,22 @@ String escapeCommandLineArgument(String argument) { |
| return argument; |
| } |
| +class HashCodeBuilder { |
| + int _value = 0; |
| + |
| + void add(Object object) { |
| + _value = ((_value * 31) ^ object.hashCode) & 0x3FFFFFFF; |
| + } |
| + |
| + int get value => _value; |
| +} |
| + |
| +class UniqueObject { |
| + static int _nextId = 1; |
| + final int _hashCode; |
| + |
| + int get hashCode => _hashCode; |
| + operator==(other) => other is UniqueObject && _hashCode == other._hashCode; |
| + |
| + UniqueObject() : _hashCode = ++_nextId; |
| +} |